elasticsearch入門系列">elasticsearch入門系列
889
2025-03-31
文章史上最簡單的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
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
方志朋簡介:SpringCloud中國社區聯合創始人,博客訪問量突破一千萬,愛好開源,熱愛分享,活躍于各大社區,保持著非常強的學習驅動力,終身學習踐行者,終身學習受益者。目前就職于國內某家知名互聯網保險公司,擔任DEVOPS工程師,對微服務領域和持續集成領域研究較深,精通微服務框架SpringCloud
Spring Spring Cloud
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。