eureka集群
Eureka集群概述
基本原理
不同节点的Eureka Server通过Replicate(复制)进行数据同步
- Application Service为服务提供者
- Application Client为服务消费者
- Make Remote Call完成一次服务调用
服务启动后向Eureka注册,Eureka Server会将注册信息向其他Eureka Server进行同步,当服务消费者要调用服务提供者,则向服务注册中心获取服务提供者地址(即:服务应用名,spring.application.name参数配置),然后会将服务提供者地址缓存在本地,下次再调用时,则直接从本地缓存中取,完成一次调用。
当服务注册中心Eureka Server检测到服务提供者因为宕机、网络原因不可用时,则在服务注册中心将服务置为DOWN状态,并把当前服务提供者状态向订阅者发布,订阅过的服务消费者更新本地缓存。
服务提供者在启动后,周期性(默认30秒)向Eureka Server发送心跳,以证明当前服务是可用状态。Eureka Server在一定的时间(默认90秒)未收到客户端的心跳,则认为服务宕机,注销该实例。
以具体示例说明Eureka集群及配置
创建一个microservice-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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.psy</groupId> <artifactId>springcloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>microservice-eureka-server</artifactId> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 修改后立即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml:
--- server: port: 2001 context-path: / eureka: instance: hostname: eureka2001.psy.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka2002.psy.com:2002/eureka/,http://eureka2003.psy.com:2003/eureka/ spring: profiles: eureka2001 --- server: port: 2002 context-path: / eureka: instance: hostname: eureka2002.psy.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka2001.psy.com:2001/eureka/,http://eureka2003.psy.com:2003/eureka/ spring: profiles: eureka2002 --- server: port: 2003 context-path: / eureka: instance: hostname: eureka2003.psy.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka2001.psy.com:2001/eureka/,http://eureka2002.psy.com:2002/eureka/ spring: profiles: eureka2003
在启动类加上:
@EnableEurekaServer
配置启动参数:
Eureka自我保护机制
开发环境,我们经常会遇到这个红色的警告;
当我们长时间为访问服务以及变更服务实例名称的时候,就会出现这个红色警告;
默认情况,如果服务注册中心再一段时间内没有接收到某个微服务实例的心跳,服务注册中心会注销该实例(默认90秒)。
由于正式环境,经常会有网络故障,网络延迟问题发生,服务和注册中心无法正常通信,此时服务是正常的,不应该注销该服务,Eureka这时候,就通过“自我保护模式”来解决问题,当短时间和服务失去通信时,保留服务信息,当恢复网络和通信时候,退出“自我保护模式”;
通过“自我保护模式”,使Eureka集群更加的健壮和稳定;
解决方式有三种:
- 关闭自我保护模式(
eureka.server.enable-self-preservation
设为false
),不推荐。 - 降低
renewalPercentThreshold
的比例(eureka.server.renewal-percent-threshold
设置为0.5
以下,比如0.49
),不推荐。 - 部署多个 Eureka Server 并开启其客户端行为(
eureka.client.register-with-eureka
不要设为false
,默认为true
),推荐。
Eureka 的自我保护模式是有意义的,该模式被激活后,它不会从注册列表中剔除因长时间没收到心跳导致租期过期的服务,而是等待修复,直到心跳恢复正常之后,它自动退出自我保护模式。这种模式旨在避免因网络分区故障导致服务不可用的问题。例如,两个客户端实例 C1 和 C2 的连通性是良好的,但是由于网络故障,C2 未能及时向 Eureka 发送心跳续约,这时候 Eureka 不能简单的将 C2 从注册表中剔除。因为如果剔除了,C1 就无法从 Eureka 服务器中获取 C2 注册的服务,但是这时候 C2 服务是可用的。
所以,Eureka 的自我保护模式最好还是开启它。