Spring Cloud【Finchley】-13 Eureka Server HA高可用 2個/3個節點的搭建及服務註冊呼叫
文章目錄
- 導讀
- 官方文件
- Eureka Server高可用叢集概述
- 2個Eureka Server節點高可用叢集搭建步驟
- Step1. 新建子模組 microservice-discovery-eureka-ha
- Step2. 配置hosts
- Step3. application.yml註冊兩個Eureka Server
- Step4. 啟動測試
- Step5. 檢視服務註冊中心
- 3個Eureka Server節點高可用叢集搭建步驟
- 將服務註冊到Eureka Server叢集上及服務呼叫
- 程式碼
導讀
Spring Cloud【Finchley】-02服務發現與服務註冊Eureka + Eureka Server的搭建中我們搭建了Stand Alone版本的Eureka Server ,本片我們來搭建2個Eureka Server節點組成的叢集 和 3個Eureka Server節點組成的叢集 ,並將微服務註冊到Eureka Server叢集上。
官方文件
Eureka Server高可用叢集概述
我們知道Eureka Client會定時連線Eureka Server,獲取服務登錄檔中的資訊並快取到本地,微服務在消費遠端API的時候不用每次去Server端查詢,而是使用本地快取的資料,這樣的話,一般來講即使Server宕機,也不會影響微服務之間的呼叫,但是肯定會影響Client端服務的更新,所以生產環境中,高可用的Eureka Server是必不可少的。
Eureka Server可以通過執行多個例項並相互註冊的方式來實現高可用。 Eureka Server例項會彼此增量的同步資訊,確保所有節點資料一致。
來看下Stand Alone模式的配置
registerWithEureka: false
fetchRegistry: false
所以叢集環境下,需要保持預設值,即 true . 詳見原始碼 EurekaClientConfigBean
2個Eureka Server節點高可用叢集搭建步驟
Step1. 新建子模組 microservice-discovery-eureka-ha
關鍵pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Step2. 配置hosts
linux : /etc/hosts
windows : C:\Windows\System32\drivers\etc\hosts
Step3. application.yml註冊兩個Eureka Server
spring:
# 註冊到eureka上的微服務名稱
application:
name: microservice-discovery-eureka-ha
---
spring:
# 指定profiles為peer1
profiles: peer1
server:
port: 8761
eureka:
instance:
# 當profiles為peer1,hostname是peer1 ,以jar包的形式啟動的時候通過spring.profiles.active引數指定使用哪個配置檔案啟動
hostname: peer1
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
# 將自己註冊到peer2這個Eureka上
defaultZone: http://peer2:8762/eureka/
---
spring:
# 指定profiles為peer2,以jar包的形式啟動的時候通過spring.profiles.active引數指定使用哪個配置檔案啟動
profiles: peer2
# 埠
server:
port: 8762
# Eureka
eureka:
instance:
# 當profiles為peer2,hostname是peer2
hostname: peer2
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
# 將自己註冊到peer1這個Eureka上
defaultZone: http://peer1:8761/eureka/
application.yml中使用連字元 ---
將配置檔案分為三段,第二段和第三段分別為spring. profiles指定了名稱,該名稱表示它所在的那段內容應該在哪個profile中。
第一段未指定spring. profiles,即對所有的profile生效
上述配置檔案,指定了hostname ,與上一步配置的hostname保持一致,同時讓peer1 和 peer2 相互註冊,即peer1註冊到peer2上
Step4. 啟動測試
方法一: 在STS中配置Run Configurations
主類右鍵 Run As — Run Configurations --Spring Boot App
同理 peer2
方法二: 打包成jar,執行jar
主類右鍵 Run As — Run Configurations – Maven Build ,通過maven clean package 組合命令,打成jar包
然後通過
java -jar microservice-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar microservice-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
通過spring.profiles.active指定使用哪個profile啟動。
分別啟動peer1 和 peer2 , 首先啟動peer1會報錯java.net.ConnectException: Connection refused: connect, 因為peer2 還未啟動,等一會即可。
Step5. 檢視服務註冊中心
3個Eureka Server節點高可用叢集搭建步驟
Eureka Server不向ZK必須奇數個節點,便於選舉。 Eureka Server對節點的個數只要2個以上即可。
步驟同上,主要看下application.yml
方式一
spring:
# 註冊到eureka上的微服務名稱
application:
name: microservice-discovery-eureka-ha-3nodes
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8787/eureka/,http://peer2:8788/eureka/,http://peer3:8789/eureka/
---
spring:
# 指定profiles為peer1
profiles: peer1
server:
port: 8787
eureka:
instance:
# 當profiles為peer1,hostname是peer1 ,以jar包的形式啟動的時候通過spring.profiles.active引數指定使用哪個配置檔案啟動
hostname: peer1
---
spring:
# 指定profiles為peer2,以jar包的形式啟動的時候通過spring.profiles.active引數指定使用哪個配置檔案啟動
profiles: peer2
# 埠
server:
port: 8788
# Eureka
eureka:
instance:
# 當profiles為peer2,hostname是peer2
hostname: peer2
---
spring:
# 指定profiles為peer3,以jar包的形式啟動的時候通過spring.profiles.active引數指定使用哪個配置檔案啟動
profiles: peer3
# 埠
server:
port: 8789
# Eureka
eureka:
instance:
# 當profiles為peer3,hostname是peer3
hostname: peer3
在公共的配置中指定defaultZone,配置三個 ,peer1 peer2 peer3中不相互註冊
方式二
spring:
# 註冊到eureka上的微服務名稱
application:
name: microservice-discovery-eureka-ha-3nodes
---
spring:
# 指定profiles為peer1
profiles: peer1
server:
port: 8787
eureka:
instance:
# 當profiles為peer1,hostname是peer1 ,以jar包的形式啟動的時候通過spring.profiles.active引數指定使用哪個配置檔案啟動
hostname: peer1
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
# 將自己註冊到peer2這個Eureka上
defaultZone: http://peer2:8788/eureka/,http://peer3:8789/eureka/
---
spring:
# 指定profiles為peer2,以jar包的形式啟動的時候通過spring.profiles.active引數指定使用哪個配置檔案啟動
profiles: peer2
# 埠
server:
port: 8788
# Eureka
eureka:
instance:
# 當profiles為peer2,hostname是peer2
hostname: peer2
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
# 將自己註冊到peer1這個Eureka上
defaultZone: http://peer1:8787/eureka/,http://peer3:8789/eureka/
---
spring:
# 指定profiles為peer3,以jar包的形式啟動的時候通過spring.profiles.active引數指定使用哪個配置檔案啟動
profiles: peer3
# 埠
server:
port: 8789
# Eureka
eureka:
instance:
# 當profiles為peer3,hostname是peer3
hostname: peer3
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
# 將自己註冊到peer1這個Eureka上
defaultZone: http://peer1:8787/eureka/,http://peer2:8788/eureka/
不在公共的配置中指定defaultZone,在peer1 peer2 peer3中相互註冊
都可以,均驗證通過。
將服務註冊到Eureka Server叢集上及服務呼叫
這裡我們使用micorservice-provider-user作為演示,修改下defaultZone的地址
啟動micorservice-provider-user,
同時也修改下 消費者工程 micorservice-consumer-movie-fegin
啟動 micorservice-consumer-movie-fegin
檢視Eureka Server http://peer1:8761/
訪問 http://localhost:7901/movie/1
驗證通過
程式碼
2個Eureka Server節點
https://github.com/yangshangwei/SpringCloudMaster/tree/master/microservice-discovery-eureka-ha
3個Eureka Server節點
https://github.com/yangshangwei/SpringCloudMaster/tree/master/microservice-discovery-eureka-ha3