高可用这册中心,就是将自己作为服务向其他注册中心注册自己,这样就形成了一套相互注册的注册中心,以实现服务实例清单同步,从而达到高可用状态,下面尝试搭建高可用注册中心。
1,在前面项目基础上,创建一个application-peer1.properties,作为peer1配置服务配置中心,并将service-url指向peer2,
spring.application.name=eureka-server
server.port=1111
eureka.instance.hostname=peer1
#eureka.client.register-with-eureka=false
##这时注册中心,只需要维护检索实例,并不需要检索服务
#eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://peer2:1112/eureka/
同理创建一个application-peer2.properties,作为peer2配置服务配置中心,并将service-url指向peer1,
##server.port=${random.int(1000,2000)}
spring.application.name=eureka-server
server.port=1112
eureka.instance.hostname=peer2
#eureka.client.register-with-eureka=false
###这时注册中心,只需要维护检索实例,并不需要检索服务
#eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
启动peer1和peer2,就可以形成高可用配置中心,此时访问localhost:1111和localhost:1112,就可以看到hello-service同时注册到了peer1和peer2,此时断开peer1或者peer2,均可以访问到hello-service,就行了高可用的注册中心