Spring Cloud Eureka 高可用注册中心

参考:<<spring cloud 微服务实战>>

     在微服务架构这样的分布式环境中,各个组件需要进行高可用部署。

     Eureka Server 高可用实际上就是将自己作为服务向其他服务注册中心注册自己,这样就形成一组相互注册的服务注册中心,以实现服务清单的相互同步,达到高可用的效果。

构建双节点服务注册中心集群

      构建节点服务注册中心集群:

      1.创建application-peer1.properties , 作为peer1服务中心的配置,并将serviceUrl指向peer2:

 

spring.application.name=eureka-server
server.port=1001

eureka.instance.hostname=peer1
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://192.168.1.2:1002/eureka/

 

     2.创建application-peer2.properties , 作为peer2服务中心的配置,并将serviceUrl指向peer1:

 

spring.application.name=eureka-server
server.port=1002

eureka.instance.hostname=peer2
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://192.168.1.2:1001/eureka/

 通过 spring.profiles.active属性分别来启动peer1和peer2

java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer1
java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer2

 

 启动两个项目后,访问peer1的注册中心http://localhost:1001/

服务提供方注册 

      设置多节点服务注册中心后,服务提供方修改配置文件:

     

spring.application.name=eureka-client
server.port=2001
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/,http://localhost:1002/eureka/

 

服务启动后同时注册到了peer1 和peer2 上,此时断开peer1,peer2上的服务依然能够访问到hello-server,从而实现了服务注册中心的高可用。

 

posted @ 2018-06-07 15:57  kangjie  阅读(323)  评论(0编辑  收藏  举报