Eureka 的高可用
Eureka
通过将注册表信息存储在 Client
和 Server
,并使用心跳检测进行状态的检测和同步。这就使得单节点的 Eureka
服务具备了一定的可用性和故障恢复能力。即使在 Eureka Server
挂掉之后,因为 Client
本身存储了注册表信息,这里面就包含了其他服务的 Host
、Ip
等信息,这样就可以继续发起服务调用。
但是这种方式仍然存在一些问题。比较核心的问题就是新的服务无法在注册中心注册自己,其他的 Client
中注册表信息中还未包含这个服务信息,就导致了新的服务无法被其他服务调用。
为了实现 Eureka
的高可用,通过搭建 Eureka
集群便可以解决这种问题。在集群模式中,所有的 Eureka Server
会互相注册,实现注册表信息的同步和共享。
Eureka Server Cluster
1. Eureka-Server
1.1. 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.2. 主类
package com.mawen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author mw118
* @version 1.0
* @date 2021/1/10 22:44
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerPeerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerPeerApplication.class, args);
}
}
1.3. 配置
spring:
application:
name: eureka-server-peer
eureka:
client:
fetch-registry: false
register-with-eureka: true
serviceUrl:
defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/,http://peer3:8763/eureka/
---
spring:
profiles: peer1
eureka:
instance:
hostname: peer1
server:
port: 8761
---
spring:
profiles: peer2
eureka:
instance:
hostname: peer2
server:
port: 8762
---
spring:
profiles: peer3
eureka:
instance:
hostname: peer3
server:
port: 8763
2. Eureka-Client
2.1. 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.2. 主类
package com.mawen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author mw118
* @version 1.0
* @date 2021/1/11 8:09
*/
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientPeerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientPeerApplication.class, args);
}
}
2.3. 配置
spring:
application:
name: eureka-client-peer
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/,http://peer3:8763/eureka/
fetch-registry: false
server:
port: 8765
3. 配置Host
# peer
127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3
4. 启动服务
4.1. 启动server peer1
, peer2
, peer3
peer1
peer2
peer3
访问peer1:8761
4.2. 启动 Client