業餘草 SpringCloud教程 | 第十篇: 高可用的服務註冊中心(Finchley版本)

前面第一篇文章:業餘草 SpringCloud 教程 | 第一篇: 服務的註冊與發現Eureka(Finchley版本)html

介紹了服務註冊與發現,其中服務註冊中心Eureka Server,是一個實例,當成千上萬個服務向它註冊的時候,它的負載是很是高的,這在生產環境上是不太合適的,這篇文章主要介紹怎麼將Eureka Server集羣化。java

1、準備工做

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.linux

摘自官網git

Eureka經過運行多個實例,使其更具備高可用性。事實上,這是它默認的熟性,你須要作的就是給對等的實例一個合法的關聯serviceurl。github

這篇文章咱們基於第一篇文章的工程,來作修改。spring

2、改造工做

在eureka-server工程中resources文件夾下,建立配置文件application-peer1.yml:vim

 1 server:
 2   port: 8761
 3  
 4 spring:
 5   profiles: peer1
 6 eureka:
 7   instance:
 8     hostname: peer1
 9   client:
10     serviceUrl:
11       defaultZone: http://peer2:8769/eureka/

而且建立另一個配置文件application-peer2.yml:windows

 1  server:
 2   port: 8769
 3  
 4 spring:
 5   profiles: peer2
 6 eureka:
 7   instance:
 8     hostname: peer2
 9   client:
10     serviceUrl:
11       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 ,加上:

1 127.0.0.1 peer1
2 127.0.0.1 peer2

windows電腦,在c:/windows/systems/drivers/etc/hosts 修改。

這時須要改造下service-hi:

1 eureka:
2   client:
3     serviceUrl:
4       defaultZone: http://peer1:8761/eureka/
5 server:
6   port: 8762
7 spring:
8   application:
9     name: service-hi

3、啓動工程

啓動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,如圖:

Paste_Image.png

你會發現註冊了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的方式。

此時的架構圖:

有點醜e.png

Eureka-eserver peer1 8761,Eureka-eserver peer2 8769相互感應,當有服務註冊時,兩個Eureka-eserver是對等的,它們都存有相同的信息,這就是經過服務器的冗餘來增長可靠性,當有一臺服務器宕機了,服務並不會終止,由於另外一臺服務存有相同的數據。

本文源碼下載: 
https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter10

4、參考文獻

high_availability_zones

業餘草微信公衆號

感謝您的關注!可加QQ1羣:135430763,QQ2羣:454796847,QQ3羣:187424846。QQ羣進羣密碼:xttblog,想加微信羣的朋友,能夠微信搜索:xmtxtt,備註:「xttblog」,添加助理微信拉你進羣。備註錯誤不會贊成好友申請。再次感謝您的關注!後續有精彩內容會第一時間發給您!原創文章投稿請發送至532009913@qq.com郵箱。商務合做可添加助理微信進行溝通!

相關文章
相關標籤/搜索