SpringCloud-day04-Eureka高可用集羣配置

5.4Eureka高可用集羣配置

在高併發的狀況下一個註冊中心難以知足,所以通常須要集羣配置多臺。java

 

咱們再新建兩個module  microservice-eureka-server-2002,  microservice-eureka-server-2003,而後配置,最終的配置結果結構如圖:mysql

  

 

具體步驟:git

第一步:每一個模塊的pom.xml添加以下依賴:github

 1 <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.cloud</groupId>
 4             <artifactId>spring-cloud-starter-eureka-server</artifactId>
 5         </dependency>
 6         <!-- 修改後當即生效,熱部署 -->
 7         <dependency>
 8             <groupId>org.springframework</groupId>
 9             <artifactId>springloaded</artifactId>
10         </dependency>
11         <dependency>
12             <groupId>org.springframework.boot</groupId>
13             <artifactId>spring-boot-devtools</artifactId>
14         </dependency>
15     </dependencies>

 

 

第二步:2002  2003的主啓動類EurekaServerApplication_2002,EurekaServerApplication_2003複製EurekaServerApplication_2001的修更名稱;spring

 

第三步:前面單機的時候 eureka註冊中心實例名稱 是localhost,如今是集羣,不能三個實例都是localhost,這裏複雜的辦法是搞三個虛擬機,麻煩,這裏有簡單辦法,直接配置本機hosts,來實現本機域名映射;sql

找到 C:\Windows\System32\drivers\etc  打開hosts,加配置 瀏覽器

127.0.0.1  eureka2001.wfd360.com網絡

127.0.0.1  eureka2002.wfd360.com併發

127.0.0.1  eureka2003.wfd360.comapp

注意:在修改hosts文件時,建議先拷貝出來,修改好後再替換原來的hosts文件。

 

第四步:修改三個項目的application.yml文件,主要是修改 hostname和defaultZone,

2001 的 application.yml文件

 1 server:
 2   port: 2001
 3   context-path: /
 4 
 5 eureka:
 6   instance:
 7     #hostname: localhost #eureka註冊中心實例名稱 (單機版)
 8     hostname: eureka2001.wfd360.com # 集羣
 9   client:
10     register-with-eureka: false     #false 因爲該應用爲註冊中心,因此設置爲false,表明不向註冊中心註冊本身。
11     fetch-registry: false     #false 因爲註冊中心的職責就是維護服務實例,它並不須要去檢索服務,因此也設置爲false
12     service-url:
13        #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #(單機)設置與Eureka註冊中心交互的地址,查詢服務和註冊服務用到
14        defaultZone: http://eureka2002.wfd360.com:2002/eureka/,http://eureka2003.wfd360.com:2003/eureka/ # 集羣
View Code

 

2002 的 application.yml文件

 1 server:
 2   port: 2002
 3   context-path: /
 4 
 5 eureka:
 6   instance:
 7     #hostname: localhost #eureka註冊中心實例名稱 (單機版)
 8     hostname: eureka2002.wfd360.com # 集羣
 9   client:
10     register-with-eureka: false     #false 因爲該應用爲註冊中心,因此設置爲false,表明不向註冊中心註冊本身。
11     fetch-registry: false     #false 因爲註冊中心的職責就是維護服務實例,它並不須要去檢索服務,因此也設置爲false
12     service-url:
13        #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #(單機)設置與Eureka註冊中心交互的地址,查詢服務和註冊服務用到
14        defaultZone: http://eureka2001.wfd360.com:2001/eureka/,http://eureka2003.wfd360.com:2003/eureka/ # 集羣
View Code

 

2003 的 application.yml文件

 1 server:
 2   port: 2003
 3   context-path: /
 4 
 5 eureka:
 6   instance:
 7     #hostname: localhost #eureka註冊中心實例名稱 (單機版)
 8     hostname: eureka2003.wfd360.com # 集羣
 9   client:
10     register-with-eureka: false     #false 因爲該應用爲註冊中心,因此設置爲false,表明不向註冊中心註冊本身。
11     fetch-registry: false     #false 因爲註冊中心的職責就是維護服務實例,它並不須要去檢索服務,因此也設置爲false
12     service-url:
13        #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #(單機)設置與Eureka註冊中心交互的地址,查詢服務和註冊服務用到
14        defaultZone: http://eureka2001.wfd360.com:2001/eureka/,http://eureka2002.wfd360.com:2002/eureka/ # 集羣
View Code

 

第五步:修改服務提供者項目的application.yml,主要修改eureka.client.service-url.defaultZone,修改後的文件以下

 1 server:
 2   port: 1001
 3   context-path: /
 4 
 5 # 數據源配置
 6 spring:
 7   datasource:
 8     type: com.alibaba.druid.pool.DruidDataSource
 9     driver-class-name: com.mysql.jdbc.Driver
10     url: jdbc:mysql://localhost:3306/db_station
11     username: root
12     password: admin
13   jpa:
14     hibernate:
15       ddl-auto: update
16     show-sql: true
17   thymeleaf:
18     cache: false
19 # eureka 註冊中心配置
20 eureka:
21   instance:
22     hostname: localhost  #eureka客戶端主機實例名稱
23     appname: microservice-ticket  #客戶端服務名
24     instance-id: microservice-ticket:1001 #客戶端實例名稱
25     prefer-ip-address: true #顯示IP
26   client:
27     service-url:
28       #defaultZone: http://localhost:2001/eureka #(單機eureka)把服務註冊到eureka註冊中心,要和前面服務註冊中心(microservice-eureka-server-2001)的defaultZone暴露地址一致
29       defaultZone: http://eureka2001.wfd360.com:2001/eureka/,http://eureka2002.wfd360.com:2002/eureka/,http://eureka2003.wfd360.com:2003/eureka/ # 集羣
30 
31 # 服務提供者聯繫信息
32 info:
33    version: v2
34    負責人: 姿式帝 - 博客園
35    微  信: 851298348
View Code

 

 

第六步:測試

啓動三個註冊中心,以及服務提供者項目;

而後瀏覽器地址欄輸入:http://eureka2001.wfd360.com:2001/ 

或者  http://eureka2002.wfd360.com:2002/ 

或者 http://eureka2003.wfd360.com:2003/ 

界面以下,則集羣成功

  

      這裏本質是三個服務註冊中心都有咱們服務提供者的信息,等後面講到服務發現和服務調用,咱們經過一些策略(默認輪詢),會去找對應的服務註冊中心;經過集羣,能減輕每一個服務註冊中心的壓力;

      

值得注意的是,有時候會出現以下狀況,這是  Eureka自我保護機制  

當咱們長時間爲訪問服務以及變動服務實例名稱的時候,就會出現這個紅色警告;

默認狀況,若是服務註冊中心再一段時間內沒有接收到某個微服務實例的心跳,服務註冊中心會註銷該實例(默認90秒)。

因爲正式環境,常常會有網絡故障(若是想模擬該現象,能夠先開啓服務提供者,再關閉服務提供者,最後再訪問註冊中心),網絡延遲問題發生,服務和註冊中心沒法正常通訊,此時服務是正常的,不該該註銷該服務,

Eureka這時候,就經過「自我保護模式」來解決問題,當短期和服務失去通訊時,保留服務信息,當恢復網絡和通訊時候,退出「自我保護模式」;

經過「自我保護模式」,使Eureka集羣更加的健壯和穩定;

 

到這裏eureka的服務端基本搞定,代碼能夠在github上下載,版本V4

相關文章
相關標籤/搜索