SpringCloud分佈式微服務雲架構 第十篇: 高可用的服務註冊中心(Finchley版本)

文章的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. 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.——摘自官網

Eureka通過運行多個實例,使其更具有高可用性。瞭解springcloud架構可以加求求:三五三六二四七二五九,事實上,這是它默認的熟性,你需要做的就是給對等的實例一個合法的關聯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
SpringCloud分佈式微服務雲架構 第十篇: 高可用的服務註冊中心(Finchley版本)
啓動service-hi:

java -jar service-hi-0.0.1-SNAPSHOT.jar

訪問:localhost:8761,如圖:
SpringCloud分佈式微服務雲架構 第十篇: 高可用的服務註冊中心(Finchley版本)
你會發現註冊了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的方式。

此時的架構圖:SpringCloud分佈式微服務雲架構 第十篇: 高可用的服務註冊中心(Finchley版本)
Eureka-eserver peer1 8761,Eureka-eserver peer2 8769相互感應,當有服務註冊時,兩個Eureka-eserver是對等的,它們都存有相同的信息,這就是通過服務器的冗餘來增加可靠性,當有一臺服務器宕機了,服務並不會終止,因爲另一臺服務存有相同的數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章