企業級 SpringCloud 教程 (十) 高可用的服務註冊中心

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

摘自官網java

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

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

2、改造工做

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

server:
  port: 8761

spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2:8769/eureka/

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

server:
  port: 8769

spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/

這時eureka-server就已經改造完畢。vim

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

按照官方文檔的指示,須要改變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

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是對等的,它們都存有相同的信息,這就是經過服務器的冗餘來增長可靠性,當有一臺服務器宕機了,服務並不會終止,由於另外一臺服務存有相同的數據。

源碼來源

相關文章
相關標籤/搜索