Eureka 是Netflix 开源的服务发现组件,本身是一个基于REST的服务。它包含了Server和Client两部分。Spring cloud 将它集成在即项目Spring Cloud Netflix中,从而实现微服务的注册和发现
接下来的代码是基于SpringBoot2.0.3基础上实现的,分Server和Client两部分来说明
==========Server部分==========
启动类
@SpringBootApplication
//启动一个服务注册中心提供给其他应用进行对话
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
关键部分的依赖
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<!--引入spring cloud依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--添加spring-boot 依赖-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.yml部分
server:
port: 8761
spring:
application:
name: server
eureka:
instance:
hostname: localhost
# 使用IP注册
preferIpAddress: true
# 心跳间隔
lease-renewal-interval-in-seconds: 3
# 服务失效时间: 如果多久没有收到请求,则可以删除服务
lease-expiration-duration-in-seconds: 7
client:
# 关闭eureka client
# enabled: false
# 注册自身到eureka服务器
register-with-eureka: true
# 表示是否从eureka服务器获取注册信息
fetch-registry: false
# 客户端从Eureka Server集群里更新Eureka Server信息的频率
eureka-service-url-poll-interval-seconds: 60
# 定义从注册中心获取注册服务的信息
registry-fetch-interval-seconds: 5
# 设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址
service-url:
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
server:
# renewal-percent-threshold: 0.1
# 关闭自我保护模式
enable-self-preservation: false
# Eureka Server 自我保护系数,当enable-self-preservation=true时,启作用
# renewal-percent-threshold:
# 设置清理间隔,单位为毫秒,默认为0
eviction-interval-timer-in-ms: 3000
# 设置如果Eureka Server启动时无法从临近Eureka Server节点获取注册信息,它多久不对外提供注册服务
wait-time-in-ms-when-sync-empty: 6000000
# 集群之间相互更新节点信息的时间频率
peer-eureka-nodes-update-interval-ms: 6000
启动Eureka Server
可以从控制台看到 Eureka 服务器已经建立成功,接下来Eureka Client会定时向EurekaServer端进行服务续约(心跳) :
访问 http://localhost:8761/ 可以看到以下图片,
==========Client部分==========
启动类
@SpringBootApplication
//声明这是一个Eureka Client
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
server:
port: 9092
spring:
application:
name: eureka-client
eureka:
instance:
hostname: localhost
# 使用IP注册
preferIpAddress: true
# 心跳间隔
lease-renewal-interval-in-seconds: 3
# 服务失效时间: 如果多久没有收到请求,则可以删除服务
lease-expiration-duration-in-seconds: 7
client:
# 关闭eureka client
# enabled: false
# 注册自身到eureka服务器
register-with-eureka: true
# 表示是否从eureka服务器获取注册信息
fetch-registry: false
# 客户端从Eureka Server集群里更新Eureka Server信息的频率
eureka-service-url-poll-interval-seconds: 60
# 定义从注册中心获取注册服务的信息
registry-fetch-interval-seconds: 5
# 设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址
service-url:
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.basil.common</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!--提供springMvc支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
</dependencies>
<!--引入spring cloud依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动 Eureka-Client 后可以在 Eureka-Server 控制台中看到 ,Client 会通过启动时初始化的定时任务定时向EurekaServer端进行服务续约(心跳)
同时 访问http://localhost:8761/,可以看到如下图,可以知道 Eureka-Client服务已被注册到 Eureka-Server上