Spring Cloud第十篇:高可用的服務注冊中心
文章 史上最簡單的 springCloud 教程 | 第一篇: 服務的注冊與發現(Eureka) 介紹了服務注冊與發現,其中服務注冊中心Eureka Server,是一個實例,當成千上萬個服務向它注冊的時候,它的負載是非常高的,這在生產環境上是不太合適的,這篇文章主要介紹怎么將Eureka Server集群化。
一、準備工作
Eureka can be made even more resilient and available by running multiple instances and asking them to register with each other. In fact, 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.
摘自官網
Eureka通過運行多個實例,使其更具有高可用性。事實上,這是它默認的熟性,你需要做的就是給對等的實例一個合法的關聯serviceurl。
這篇文章我們基于第一篇文章的工程,來做修改。
二、改造工作
在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就已經改造完畢。
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 ,加上:
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
三、啟動工程
啟動eureka-server:
java -jar eureka-server-0.0.1-SNAPSHOT.jar - -spring.profiles.active=peer1
java -jar eureka-server-0.0.1-SNAPSHOT.jar - -spring.profiles.active=peer2
>
啟動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 registers with eureka, it will use its IP Address rather than its hostname.
摘自官網
eureka.instance.preferIpAddress=true是通過設置ip讓eureka讓其他服務注冊它。也許能通過去改變去通過改變host的方式。
此時的架構圖:
Eureka-eserver peer1 8761,Eureka-eserver peer2 8769相互感應,當有服務注冊時,兩個Eureka-eserver是對等的,它們都存有相同的信息,這就是通過服務器的冗余來增加可靠性,當有一臺服務器宕機了,服務并不會終止,因為另一臺服務存有相同的數據。
本文源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter10
https://mp.weixin.qq.com/s?__biz=MzAxNjk4ODE4OQ==&mid=2247483883&idx=1&sn=97d982af9abfdc18b28bb88aedf430e6&chksm=9bed2099ac9aa98f95e079fd3a8d1e739f76e3e1f003b587ce8f23297471ec1b62c02514bd54&scene=21#wechat_redirect
注冊 Spring Cloud Spring
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。