1. 程式人生 > >【SpringCloud】04——Eureka高可用叢集

【SpringCloud】04——Eureka高可用叢集

1.為什麼需要Eureka叢集?
Eureka作為服務註冊中心,讓服務作為客戶端註冊進來,但是假如Eureka壞了的話,所有的服務都將不能進行互相呼叫,為了高可用,所以需要搭建Eureka叢集來解決該問題。

2.搭建叢集我們需要做的幾步工作:
2.1.新增工程,配置pom ,yml
在這裡插入圖片描述
新增跟7001一樣的springboot 專案7002,7003,為7002,7003配置pom檔案:

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <!--修正後立即生效-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

修改7001的yml檔案,7002,7003原理一樣,注意與上一篇單機版進行對比.

#叢集
server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com  #eureka服務端的例項名稱
  client:
    register-with-eureka: false  #不向註冊中心註冊自己
    fetch-registry: false  #false 表示自己就是註冊中心,我的職責就是維護服務例項,並不需要去檢索服務
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

2.2修改本地的C:\Windows\System32\drivers\etc\host檔案進行域名對映
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com

2.3修該微服務yml檔案為叢集式將該服務註冊進入Eureka 叢集

eureka:
  client:
    service-url:
         #客戶端註冊進eureka服務的地址,同eureka暴露給外部的地址
       # 單機版 defaultZone: http://localhost:7001/eureka
       #叢集
       defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

  instance:
      #註冊到eureka後,status的名字(服務在eureka的唯一標誌)
      instance-id: ${spring.application.name}
      #訪問路徑可以顯示IP地址
      prefer-ip-address: true

info:
  app.name: dmsdbj_microservicecloud
  company_name: www.dmsdbj.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

然後啟動Eureka 和微服務,會發現7003中多了eureka7001.com和eureka7002.com,同樣7001中會有eureka7002.com和eureka7003.com.並且在每一個Eureka中都有微服務MICROSERVICECLOUD-DEPT8001.
在這裡插入圖片描述

學習參考:
https://blog.csdn.net/yjclsx/article/details/81484522
https://blog.csdn.net/j080624/article/details/81112809