spring-boot版本: 2.1.2.RELEASE
spring-cloud版本 : Greenwich.SR1
参考: https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/multi/multi_spring-cloud.html
1. 搭建Eureka工程
可以参考本人搭建的eureka-server、config-server工程见文末[1],这里采用的是通过注册中心获取配置(注册中心可以采用git,我这里采用的是local模式,即项目配置文件在resource/config下)
2. 在线扩容
Eureka采用Peer to Peer架构,在线扩容非常容易,也感谢Netflix给开发者提供了如此简单的平台。
Spring-boot2.0以上默认隐藏关键mapping,需要开启刷新配置的mapping,提供刷新配置功能,各个服务都需要加上:
management:
endpoints:
web:
exposure:
include: refresh
1.单节点
Eureka:19955
Client:19960
项目初期可能只需要一台Eureka服务就足矣了,甚至可以直接采用Spring-cloud官网生成的Eureka代码就可以解决问题了,并且都不用自我注册.
- [config-server]下resource/config/eureka-server.yml配置:
management:
endpoints:
web:
exposure:
include: refresh
server:
port: 19955
spring:
application:
name: eureka-server
security:
basic:
enabled: true
user:
name: admin
password: 123
eureka:
instance:
prefer-ip-address: true
hostname: localhost
lease-renewal-interval-in-seconds: 10
client:
fetch-registry: false #是否从Eureka Server获取注册信息,单节点不需要
register-with-eureka: false #是否注册eureka server
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:19955/eureka/
server:
eviction-interval-timer-in-ms: 5000
response-cache-auto-expiration-in-seconds: 60
#自我保护
enable-self-preservation: true
#在Eureka服务器获取不到集群里对等服务器上的实例时,需要等待的时间,单位为毫秒,
wait-time-in-ms-when-sync-empty: 0
- [config-server]下resource/config/t-client.yml配置:
server:
port: 19960
spring:
application:
name: t-client
eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://admin:123@localhost:19955/eureka/
registry-fetch-interval-seconds: 5
2.两个节点
Eureka:19955,Eureka:19956
Client:19960
项目业务扩大,服务太多,单节点不足以满足现有的服务数量,增加一个节点,两个Eureka相互注册:
- [config-server]下resource/config/eureka-server-peer1.yml配置(只列举了部分改变的配置):
server:
port: 19956
eureka.client.service-url.defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:19955/eureka/
- 单节点的[config-server]下resource/config/eureka-server.yml也需要修改注册地址(只列举了部分改变的配置):
eureka.client.service-url.defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:19956/eureka/
- 客户端[config-server]下resource/config/t-client.yml配置,需要修改注册Eureka地址为两个Eureka(只列举了扩容相关部分配置):
eureka.client.service-url.defaultZone: http://admin:123@${eureka.instance.hostname}:19955/eureka/,http://admin:123@${eureka.instance.hostname}:19956/eureka/
接下来,
1.启动新增加的Eureka:19956(使用idea->Edit Configureation->右上角Allow parallel run打勾-> 添加Name:spring.profiles.active,Value: peer1),重启config-server;
2.使用postman请求 POST http://localhost:19955/actuator/refresh刷新配置,添加Basic Auth ,Eureka账号密码admin:123(也可关闭,去掉Security依赖);
- 同样方法刷新client:19960配置.
动态扩容结束
3.三个节点
Eureka:19955,Eureka:19956,Eureka:19957
Client:19960
同理:启动新加的19957 Eureka服务,重启config-server,
刷新eureka-server,eureka-peer1,t-client配置.
后续,如果需要更大的集群可以采用region内的Zone之间Peer to Peer复制
-
最终配置: Eureka工程 ↩