文章的SpringCloud 教程 | 第一篇: 服務的註冊與發現(Eureka) 介紹了服務註冊與發現,其中服務註冊中心Eureka Server,是一個實例,當成千上萬個服務向它註冊的時候,它的負載是很是高的,這在生產環境上是不太合適的,這篇文章主要介紹怎麼將Eureka Server集羣化。java
1、準備工做linux
Eureka can be made even more resilient and available by running multiple instances and asking them to register with each other. Infact, this is the default behaviour, so all you need to do to make it work is add a valid serviceUrl to a peer, e.g.——摘自官網spring
Eureka經過運行多個實例,使其更具備高可用性。瞭解springcloud架構能夠加求求:三五三六二四七二五九,事實上,這是它默認的熟性,你須要作的就是給對等的實例一個合法的關聯serviceurl。vim
這篇文章咱們基於第一篇文章的工程,來作修改。windows
2、改造工做
在eureka-server工程中resources文件夾下,建立配置文件application-peer1.yml:服務器
server: port: 8761 spring: profiles: peer1 eureka: instance: hostname: peer1 client: serviceUrl: defaultZone: http://peer2:8769/eureka/
而且建立另一個配置文件application-peer2.yml:架構
server: port: 8769 spring: profiles: peer2 eureka: instance: hostname: peer2 client: serviceUrl: defaultZone: http://peer1:8761/eureka/
這時eureka-server就已經改造完畢。app
ou could use this configuration to test the peer awareness on a single host (there’s not much value in doing that in production) by manipulating /etc/hosts to resolve the host names.分佈式
按照官方文檔的指示,須要改變etc/hosts,linux系統經過vim /etc/hosts ,加上:ide
127.0.0.1 peer1 127.0.0.1 peer2
windows電腦,在c:/windows/systems/drivers/etc/hosts 修改。
這時須要改造下service-hi:
eureka: client: serviceUrl: defaultZone: http://peer1:8761/eureka/ server: port: 8762 spring: application: name: service-hi
3、啓動工程
啓動eureka-server:java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
啓動service-hi:
java -jar service-hi-0.0.1-SNAPSHOT.jar
訪問:localhost:8761,如圖:
你會發現註冊了service-hi,而且有個peer2節點,同理訪問localhost:8769你會發現有個peer1節點。
client只向8761註冊,可是你打開8769,你也會發現,8769也有 client的註冊信息。
我的感覺:這是經過看官方文檔的寫的demo ,可是須要手動改host是否是不符合Spring Cloud 的高上大?
Prefer IP Address
In some cases, it is preferable for Eureka to advertise the IP Adresses of services rather than the hostname. Set eureka.instance.preferIpAddress to true and when the application hostname.——摘自官網
eureka.instance.preferIpAddress=true是經過設置ip讓eureka讓其餘服務註冊它。也許能經過去改變去經過改變host的方式。
此時的架構圖:Eureka-eserver peer1 8761,Eureka-eserver peer2 8769相互感應,當有服務註冊時,兩個Eureka-eserver是對等的,它們都存有相同的信息,這就是經過服務器的冗餘來增長可靠性,當有一臺服務器宕機了,服務並不會終止,由於另外一臺服務存有相同的數據。