搭建 Eureka 集群主要是提高集群的高可用性,防止当其中某一个 Eurera 注册机器宕机时不能够对外提供服务。
<br/>
搭建步骤如下:
**1. 搭建思路**
先搭建三个 Eureka 注册中心,然后分别往这三个 Eureka 中注册 payment8001 微服务。
```
三个 Eureka 服务注册中心我已经提前搭建好了,模块名称如下:
assemb-server-eureka7001
assemb-server-eureka7002
assemb-server-eureka7003
```
**2. 三个 Eureka 注册中心的配置**
1. eureka7001注册中心配置。
```yml
server:
port: 7001
spring:
application:
name: assemb-server-eureka
eureka:
instance:
hostname: www.eureka7001.com
client:
fetch-registry: false
register-with-eureka: false
service-url:
#将其他eureka注册地址写到这里,但是当前eureka模块的注册地址不要写
defaultZone: http://www.eureka7002.com:7002/eureka/,http://www.eureka7003.com:7003/eureka/
server:
enable-self-preservation: true
eviction-interval-timer-in-ms: 2000
```
2. eureka7002 注册中心配置。
```yml
server:
port: 7002
spring:
application:
name: assemb-server-eureka
eureka:
instance:
hostname: www.eureka7002.com
client:
fetch-registry: false
register-with-eureka: false
service-url:
#将其他eureka注册地址写到这里,但是当前eureka模块的注册地址不要写
defaultZone: http://www.eureka7001.com:7001/eureka/,http://www.eureka7003.com:7003/eureka/
server:
enable-self-preservation: true
eviction-interval-timer-in-ms: 2000
```
3. eureka7003 注册中心配置。
```yml
server:
port: 7003
spring:
application:
name: assemb-server-eureka
eureka:
instance:
hostname: www.eureka7003.com
client:
fetch-registry: false
register-with-eureka: false
service-url:
#将其他eureka注册地址写到这里,但是当前eureka模块的注册地址不要写
defaultZone: http://www.eureka7001.com:7001/eureka/,http://www.eureka7002.com:7002/eureka/
server:
enable-self-preservation: true
eviction-interval-timer-in-ms: 2000
```
>[info]注意:他们的`spring.application.name`要一致。
**3. 将 payment8001 微服务注册到三个 Eureka 注册中心中,他的 Eureka 配置如下**
```yml
server:
port: 8001
spring:
application:
name: assemb-server-payment
eureka:
client:
service-url:
#写上所以注册中心的地址
defaultZone: http://www.eureka7001.com:7001/eureka/,http://www.eureka7002.com:7002/eureka/,http://www.eureka7003.com:7003/eureka/
instance:
instance-id: ${spring.application.name}${server.port}
prefer-ip-address: true
lease-renewal-interval-in-seconds: 1
lease-expiration-duration-in-seconds: 2
```
**4. 验证集群是否搭建成功**
1. 先分别启动三个 Eureka 注册中心,然后再启动 payment8001微服务。
2. 分别访问三个 Eureka 注册中心,可以看到每个 Eureka 除了自己外,还有两个备用的,并且 payment8001 微服务都入住了三个 Eureka 注册中心。
:-: ![](https://img.kancloud.cn/aa/f7/aaf7e2156867aa5c70dca5d06278b7d2_2249x468.png)
http://www.eureka7001.com:7001/
<br/>
:-: ![](https://img.kancloud.cn/56/c6/56c60487b21eedff0932661cf73962ad_2013x444.png)
http://www.eureka7002.com:7002/
<br/>
:-: ![](https://img.kancloud.cn/7d/54/7d540aaed73c34dba624832a1e899d9a_2175x465.png)
http://www.eureka7003.com:7003/