1. 程式人生 > 其它 >spring cloud專案03:高可用註冊中心

spring cloud專案03:高可用註冊中心

JDK 8

spring boot 2.5.2

spring cloud 2020.0.3

---

目錄

高可用配置(一)

高可用配置(二)

參考文件

在生產環境下,如果只有一個 註冊中心服務,存在很大的安全風險——宕機會導致整個S.C.系統掛掉(雖然 Client的 服務資訊快取 能撐一段時間——具體多久?)。

因此,需要啟動多個註冊中心服務(分佈在不同的主機、機房等,還可以更大?),進而實現 系統高可用

高可用配置(一)

試驗:

三個 註冊中心服務:peer1、peer2、peer3,埠分別為 8771、8772、8773。

在一個application.properties中包含 三個註冊中心的配置。

建立服務,在 主程式類 新增 @EnableEurekaServer 即可:

@EnableEurekaServer
@SpringBootApplication
public class ServiceRegistrationAndDiscoveryServiceApplication {

	public static byte[] arr;
	
	public static void main(String[] args) {
		SpringApplication.run(ServiceRegistrationAndDiscoveryServiceApplication.class, args);
    }
}

配置檔案修改如下:

# application.properties 檔案
spring.application.name=eureka-server

# 預設啟動peer1
spring.profiles.active=peer1

eureka.instance.prefer-ip-address=true

#---

spring.config.activate.on-profile=peer1

eureka.instance.hostname=peer1

server.port=8771

eureka.client.service-url.defaultZone=http://localhost:8772/eureka/,http://localhost:8773/eureka/

#---

spring.config.activate.on-profile=peer2

eureka.instance.hostname=peer2

server.port=8772

eureka.client.service-url.defaultZone=http://localhost:8771/eureka/,http://localhost:8773/eureka/

#---

spring.config.activate.on-profile=peer3

eureka.instance.hostname=peer3

server.port=8773

eureka.client.service-url.defaultZone=http://localhost:8771/eureka/,http://localhost:8772/eureka/

配置檔案注意事項:

1、這是一個多文件檔案,,不同部分使用 “#---” 分隔

分隔行前後 不能直接有 註釋行,否則,配置無效

詳見 S.B. 官方文件 的 “Working with Multi-Document Files” 一節。

2、eureka.instance.prefer-ip-address=true

3、spring.config.activate.on-profile=peerN

不同profile,使用不同的配置。

# spring.config.activate.on-profile 官文解釋
Profile expressions that should match for the document to be included.

啟動時,新增引數 --spring.profiles.active=peerN (N=1、2、3),逐個啟動。

先啟動的服務的日誌 中可能存在錯誤;

三個服務都啟動後,依次訪問連結:

http://localhost:8771/

http://localhost:8772/

http://localhost:8773/

三個 EUREKA-SERVER 都註冊成功了,三個頁面不同的是,General Info 下的資訊 有差別。

疑問:unavailable-replicas 怎麼 有值?而 available-replicas 卻是空的呢?TODO

測試服務註冊

兩個Web服務:web-first, web-second,web-second會呼叫 web-first的服務

web-first配置:

eureka.instance.prefer-ip-address=true
# 僅配置1個 peer1
eureka.client.service-url.defaultZone=http://localhost:8771/eureka/

web-second配置:

eureka.instance.prefer-ip-address=true
# 僅配置1個 peer2
eureka.client.service-url.defaultZone=http://localhost:8772/eureka/

兩個服務配置的 eureka.client.service-url.defaultZone 不同,此時,三個 註冊中心服務上 是否會 都有兩者的資訊呢?

答案是 YES

下面是 peer3 的頁面:

測試結果,web-second 可以 成功呼叫 web-first 提供的服務。

當然,正確的 eureka.client.service-url.defaultZone 應該是下面的:

eureka.client.service-url.defaultZone=http://localhost:8771/eureka/,http://localhost:8772/eureka/,http://localhost:8773/eureka/

停掉一個 註冊中心服務——peer1,觀察其它兩個註冊中心的 日誌、頁面 的變化。

- 日誌

Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to localhost:8771 timed out

- 頁面

EUREKA-SERVER此時只有 2個了——8772、8773。

此時,web-second 能成功 呼叫 web-first 的服務嗎?

呼叫成功!

生產情況下,此時會報警——各種通知,再由運維、開發人員來解決此問題,保證 短時間內停掉的服務 能重啟,恢復。

重啟 peer1 後,其它兩個的日誌顯示下面的資訊:

Registered instance EUREKA-SERVER/DESKTOP-BDNTQQ3:eureka-server:8771 with status UP (replication=false)

高可用配置(二)

試驗:將各個profile的配置放到不同的 application-{profile}.properties 檔案中,其它條件不變。

application.properties 內容:

spring.application.name=eureka-server

# 預設啟動peer1
spring.profiles.active=peer1

# 預設false-需要在 註冊中心服務 的 hosts 中配置(跨網路)
# 設定為 true,則不需要 配置hosts——IP可以訪問成功的話
eureka.instance.prefer-ip-address=true

application-{profile}.properties 中:

eureka.instance.hostname=peer1
server.port=8771

eureka.client.service-url.defaultZone=http://localhost:8772/eureka/,http://localhost:8773/eureka/

注意,修改 前兩個配置的值。

啟動三個服務,正常。

啟動 web-first、web-second 測試,正常。

疑問

1、註冊中心服務 不會有配置幾十、幾百個的吧?

2、更多註冊中心的 更多配置 有哪些?

3、除了 Eureka的註冊中心,還有什麼註冊中心可以在 Spring Cloud中使用?

參考文件

1、書《Spring Cloud微服務實戰》 by 翟永超

第三章 服務治理:Spring Cloud Eureka

2、SpringCloud之Eureka註冊中心原理及其搭建

本文還涉及到了 安全認證 等,更深入。

3、