这是我参与更文挑战的第14天,活动详情查看: 更文挑战
今天看看eureka的使用吧 SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。 Eureka包含两个组件:Eureka Server和Eureka Client。为了方便测试,在studycloud工程下建立了三个模块,netflix-eureka-consumer,netflix-eureka-provider,netflix-eureka-server
建立netflix-eureka-server模块
修改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">
<parent>
<artifactId>study</artifactId>
<groupId>brief.talk.spring.cloud</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>netflix-eureka-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
新增主程序
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {
public static void main (String[] args){
SpringApplication.run(EurekaServer.class, args);
}
}
新增配置文件application.yml
spring:
application:
name: eureka-server
server:
port: 8080
eureka:
client:
#表示不将将自己注册进EurekaServer,默认为true。
register-with-eureka: false
#表示不从EurekaServer抓取已有的注册信息,默认为true。
fetch-registry: false
然后启动EurekaServer,可以看到这个界面,就表示eureka server启动成功
netflix-eureka-consumer,netflix-eureka-provider这两个模块与netflix-eureka-server一样的操作步骤,不同点是pom文件中引入的是eureka-client的依赖,而netflix-eureka-server引入的是eureka-server的依赖
<?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">
<parent>
<artifactId>study</artifactId>
<groupId>brief.talk.spring.cloud</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>netflix-eureka-provider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
还有就是yml配置文件不一样
netflix-eureka-provider
spring:
application:
name: eureka-provider
server:
port: 8100
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8080/eureka
netflix-eureka-consumer
spring:
application:
name: eureka-consumer
server:
port: 8101
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8080/eureka
注意点
在eureka client中,建议使用的是@EnableDiscoveryClient的注解 @EnableEurekaClient,只适用于注册中心是eureka;
@EnableDiscoveryClient, 适用范围广,支持多。
Eureka Server:注册中心服务端
注册中心服务端主要对外提供了三个功能:
服务注册
服务提供者启动时,会通过 Eureka Client 向 Eureka Server 注册信息,Eureka Server 会存储该服务的信息,Eureka Server 内部有二层缓存机制来维护整个注册表
提供注册表
服务消费者在调用服务时,如果 Eureka Client 没有缓存注册表的话,会从 Eureka Server 获取最新的注册表
同步状态
Eureka Client 通过注册、心跳机制和 Eureka Server 同步当前客户端的状态。
Eureka Client:注册中心客户端
Eureka Client 是一个 Java 客户端,用于简化与 Eureka Server 的交互。Eureka Client 会拉取、更新和缓存 Eureka Server 中的信息。因此当所有的 Eureka Server 节点都宕掉,服务消费者依然可以使用缓存中的信息找到服务提供者,但是当服务有更改的时候会出现信息不一致。
Register: 服务注册
服务的提供者,将自身注册到注册中心,服务提供者也是一个 Eureka Client。当 Eureka Client 向 Eureka Server 注册时,它提供自身的元数据,比如 IP 地址、端口,运行状况指示符 URL,主页等。
Renew: 服务续约
Eureka Client 会每隔 30 秒发送一次心跳来续约。 通过续约来告知 Eureka Server 该 Eureka Client 运行正常,没有出现问题。 默认情况下,如果 Eureka Server 在 90 秒内没有收到 Eureka Client 的续约,Server 端会将实例从其注册表中删除,此时间可配置,一般情况不建议更改。
今日小结 今天恰逢端午佳节,吃吃喝喝,玩玩乐乐了一天,还是把今天的任务写好,哈哈,虽然技术点有点简单。