Spring cloud 2.0 瞭解(四) Eureka高可用

Eureka高可用實際上將自己作爲服務向其他服務註冊中心註冊自己,這樣就可以形成一組相互註冊的服務註冊中心,從而實現服務清單的互相同步,達到高可用效果。

視頻:https://www.majiaxueyuan.com/

 

1.修改註冊中心的配置信息

首先將註冊中心的註冊返回值進行修改。

###因爲該應用爲註冊中心,不會註冊自己
    register-with-eureka: true
###不需要去註冊中心上檢索服務
    fetch-registry: true

 

2.添加相互註冊的eureka地址

配置相同的eureka註冊中心,(端口不一致)

修改配置文件

###服務端口號
server:
  port: 8100
###eureka 基本信息配置

spring:
 application:
  name: eureka-server

eureka:
  instance:
    ###註冊到eurekaip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8101/eureka/
###因爲自己是爲註冊中心,不需要自己註冊自己
    register-with-eureka: true
###因爲自己是爲註冊中心,不需要檢索服務
    fetch-registry: true
###服務端口號
server:
  port: 8101

spring:
   application:
    name: eureka-server

###eureka 基本信息配置
eureka:
  instance:
    ###註冊到eurekaip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8100/eureka/

###因爲自己是爲註冊中心,不需要自己註冊自己
    register-with-eureka: true
###因爲自己是爲註冊中心,不需要檢索服務
    fetch-registry: true

兩個服務端的相互註冊。

8100端口註冊

8101端口

可以看見相互註冊成功了。

關於爲什麼兩個註冊中心服務都註冊了。是因爲 消費者和生產者都配置了兩個註冊中心。

只需要修改application.yml即可

生產者yml:

server:
  port: 8000
spring:
  application:
    name: appname-member
#eureka:
#  client:
#    service-url:
#      defaultZone: http://localhost:8100/eureka
###集羣地址
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8100/eureka,http://localhost:8200/eureka    
    register-with-eureka: true
    fetch-registry: true

消費者yml:

server:
  port: 9001
spring:
  application:
    name: appname-order
#eureka:
#  client:
#    service-url:
#      defaultZone: http://localhost:8100/eureka
###集羣地址
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8100/eureka,http://localhost:8200/eureka    
    register-with-eureka: true
    fetch-registry: true

以上

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