前言:
spring cloud eureka的高可用之在线扩容,只要结合上一节Config Bus热刷新,便轻松实现。先奉上源码
实现步骤:
1. 一个eureka server 和一个eureka client
- 启动good-config-server模块,更改配置文件
eureka-client.yml
的配置设置如下
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # one eureka server
eureka-server-peer1.yml
配置如下
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # one eureka server
- 执行
curl -X POST http://localhost:8888/actuator/bus-refresh
使上面更改生效 - 启动good-eureka-server,profile=peer1
- 启动good-eureka-client
- 验证:
good-spring-cloud git:(master) ✗ curl -i http://127.0.0.1:8761/query/eureka-server
{"defaultZone":"http://localhost:8761/eureka/"}
good-spring-cloud git:(master) ✗ curl -i http://127.0.0.1:8081/query/eureka-server
{"defaultZone":"http://localhost:8761/eureka/"}
2. 添加第二个eureka server
- 更改配置文件
eureka-client.yml
的配置设置如下
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/ # two eureka server
eureka-server-peer1.yml
配置如下
server:
port: 8761
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8762/eureka/ # two eureka server
eureka-server-peer2.yml
配置如下
server:
port: 8762
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # two eureka server
- 执行
curl -X POST http://localhost:8888/actuator/bus-refresh
使上面更改生效 - 启动good-eureka-server,profile=peer2,这里面peer1不用动
- 验证:
➜ good-spring-cloud git:(master) ✗ curl -i http://127.0.0.1:8081/query/eureka-server
{"defaultZone":"http://localhost:8761/eureka/,http://localhost:8762/eureka/"}%
➜ good-spring-cloud git:(master) ✗ curl -i http://127.0.0.1:8761/query/eureka-server
{"defaultZone":"http://localhost:8762/eureka/"}%
➜ good-spring-cloud git:(master) ✗ curl -i http://127.0.0.1:8762/query/eureka-server
{"defaultZone":"http://localhost:8761/eureka/"}%
2. 添加第三个eureka server
- 更改配置文件
eureka-client.yml
的配置设置如下
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/ # three eureka server
eureka-server-peer1.yml
配置如下
server:
port: 8761
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/ # three eureka server
eureka-server-peer2.yml
配置如下
server:
port: 8762
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/ # three eureka server
eureka-server-peer3.yml
配置如下
server:
port: 8763
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/ # three eureka server
- 执行
curl -X POST http://localhost:8888/actuator/bus-refresh
使上面更改生效 - 启动good-eureka-server,profile=peer3,这里面peer1,peer2不用动
- 验证:
➜ good-spring-cloud git:(master) curl http://127.0.0.1:8081/query/eureka-server
{"defaultZone":"http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/"}%
➜ good-spring-cloud git:(master) curl http://127.0.0.1:8761/query/eureka-server
{"defaultZone":"http://localhost:8762/eureka/,http://localhost:8763/eureka/"}%
➜ good-spring-cloud git:(master) curl -i http://127.0.0.1:8762/query/eureka-server
{"defaultZone":"http://localhost:8761/eureka/,http://localhost:8763/eureka/"}%
➜ good-spring-cloud git:(master) curl -i http://127.0.0.1:8763/query/eureka-server
{"defaultZone":"http://localhost:8761/eureka/,http://localhost:8762/eureka/"}%
结束语
通过学习提高工作效率和质量,在学习中留点记录,方便日后回忆,记录之处过于随意,如有错误或不当之处,还请客官指正,相互学习!
参考书籍:重新定义springcloud实战