高可用注冊中心
在微服務架構這樣的分布式環境中,我們需要充分考慮發生故障的情況,所以在生產環境中必須對各個組件進行高可用部署。在eureka-server中的application.yml中我們還記得兩段配置。讓服務注冊中心自己不注冊自己
eureka:
client:
#由於該應用為注冊中心,所以設置為false,代表不向注冊中心注冊自己
registerWithEureka: false
#由於注冊中心的職責就是維護服務實例,它並不需要去檢索服務,所以也設置為false
fetchRegistry: false
下面我們就要重新更改配置文件,讓eureke-server自己作為服務向其他服務注冊中心注冊自己。這樣就可以形成一組互相注冊的服務注冊中心,以實現服務清單的相互同步,達到高可用的效果。
根據(1-1)SpringCloud-Eureka:服務的注冊與發現 進行改造。
步驟一:在maven的resources文件夾下新建兩個配置文件application-peer1.properties 和 application-peer2.properties
application-peer1.properties
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
spring.application.name=eureka-ha-server
server.port=8751
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2:8752/eureka
application-peer2.properties
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
spring.application.name=eureka-ha-server
server.port=8752
eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer2:8751/eureka
步驟二:到C:\Windows\System32\drivers\etc下修改hosts文件,添加
127.0.0.1 peer1
127.0.0.1 peer2
步驟三:啟動服務
在項目的pom.xml目錄下運行mvn clean install。然后去maven倉庫下去找到生成的jar文件,然后運行
java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer1
java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer2
高可用注冊中心就配置好了
https://gitee.com/huayicompany/springcloud-learn/tree/master/lesson2/eureka-ha-server
參考:
[1] 《SpringCloud微服務實戰》,電子工業出版社,翟永超