註冊中心高可用
理論上來講,服務消費者本地緩存了服務提供者的地址。即使 Eureka Server 宕機,也不會影響服務之間的調用,但是一旦涉及到服務的上下線,本地的緩存信息將會出現偏差,從而影響到了整個微服務架構的穩定性,因此搭建 Eureka Server 集羣來提高整個架構的高可用性,是非常有必要的。這樣就可以使註冊中心高可用。
搭建Eureka集羣
開啓 Eureka 集羣配置後,服務啓動時 Eureka Server 會將註冊信息向其它 Eureka Server 進行同步,因此搭建高可用架構只需要將 Eureke Server 配置指向其它可用的 serviceUrl 即可。
我們在上面 Eureka 單個示例的基礎上,複製出三份來分別命名爲:eureka7001、eureka7002、eureka7003 等多個示例項目,使用多個示例項目搭建 Eureka Server 的集羣。
接下來需要分別修改 eureka7001、eureka7002 的配置信息
eureka7001主程序配置:
@SpringBootApplication
@EnableEurekaServer
public class Eureka7001 {
public static void main(String[] args) {
SpringApplication.run(Eureka7001.class,args);
}
}
eureka7001的application.yam配置
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7002.com:7002/eureka
server:
# 禁用自我保護模式
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
eureka7001的pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
eureka7002主程序配置:
@SpringBootApplication
@EnableEurekaServer
public class Eureka7002 {
public static void main(String[] args) {
SpringApplication.run(Eureka7002.class,args);
}
}
eureka7002的application.yml配置:
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka
server:
# 禁用自我保護模式
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
eureka7002的pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
eureka.client.register-with-eureka和eureka.client.fetch-registry配置爲 true ,表示將自己註冊到註冊中心,並且從註冊中心獲取註冊信息
eureka.client.serviceUrl.defaultZone,指向其它兩個註冊中心,重點配置內容
測試:依次啓動 eureka7001、eureka7002項目,等待全部啓動完成之後,瀏覽器訪問地址:eureka7001.com:7001和eureka7002.com:7002
總結:
eureka集羣註冊就是---》 相互守望、相互註冊