1. 程式人生 > >Spring Cloud(八):配置中心(服務化與高可用)【Finchley 版】

Spring Cloud(八):配置中心(服務化與高可用)【Finchley 版】

outer get btn discovery ofo DC master 配置 兩個

Spring Cloud(八):配置中心(服務化與高可用)【Finchley 版】

本文接之前的《Spring Cloud(七):配置中心(Git、Refresh)》,繼續來說說 Spring Cloud Config 的使用。

先來回顧一下,在前文中我們完成了什麽:

  • 構建了 config-server,連接到 Git 倉庫
  • 在 Git 上創建了一個 config-repo 目錄,用來存儲配置信息
  • 構建了 config-client,來獲取 Git 中的配置信息
  • 在 config-client 中開啟了 Refresh,動態刷新配置信息

在本文中,我們繼續來看看 Spring Cloud Config 的一些其他能力。

高可用問題

傳統作法

通常在生產環境,Config Server 與服務註冊中心一樣,我們也需要將其擴展為高可用的集群。在之前實現的 config-server 基礎上來實現高可用非常簡單,不需要我們為這些服務端做任何額外的配置,只需要遵守一個配置規則:將所有的 Config Server 都指向同一個 Git 倉庫,這樣所有的配置內容就通過統一的共享文件系統來維護,而客戶端在指定 Config Server 位置時,只要配置 Config Server 外的均衡負載即可,就像如下圖所示的結構:
技術分享圖片

註冊為服務

雖然通過服務端負載均衡已經能夠實現,但是作為架構內的配置管理,本身其實也是可以看作架構中的一個微服務。所以,另外一種方式更為簡單的方法就是把 config-server 也註冊為服務,這樣所有客戶端就能以服務的方式進行訪問。通過這種方法,只需要啟動多個指向同一 Git 倉庫位置的 config-server 就能實現高可用了。

配置過程也非常簡單,我們基於配置中心 Git 版本的內容來改造。

代碼改造

服務端改造

添加依賴

在 pom.xml 裏邊添加以下依賴

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

配置文件

在 application.yml 裏新增 Eureka 的配置

1
2
3
4
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/

這樣 Server 端的改造就完成了。先啟動 Eureka 註冊中心,在啟動 Server 端,在瀏覽器中訪問:http://localhost:7000/ 就會看到 Server 端已經註冊了到註冊中心了。
技術分享圖片

客戶端改造

添加依賴

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

配置文件

bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
cloud:
config:
name: config-client
profile: dev
label: master
discovery:
enabled: true
service-id: config-server
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/

主要是去掉了spring.cloud.config.uri直接指向 Server 端地址的配置,增加了最後的三個配置:

  • spring.cloud.config.discovery.enabled:開啟 Config 服務發現支持
  • spring.cloud.config.discovery.serviceId:指定 Server 端的 name, 也就是 Server 端spring.application.name的值
  • eureka.client.service-url.defaultZone:指向配置中心的地址

這三個配置文件都需要放到bootstrap.yml的配置中。

啟動 Client 端,在瀏覽器中訪問:http://localhost:7000/ 就會看到 Server 端和 Client 端都已經註冊了到註冊中心了。
技術分享圖片

高可用

為了模擬生產集群環境,我們啟動兩個 Server 端,端口分別為 12000 和 12001,提供高可用的 Server 端支持。

1
2
3
4
5
6
// 打包
./mvnw clean package -Dmaven.test.skip=true

// 啟動兩個 Server
java -jar target/spring-cloud-config-server-0.0.1-SNAPSHOT.jar --server.port=12000
java -jar target/spring-cloud-config-server-0.0.1-SNAPSHOT.jar --server.port=12001

技術分享圖片

如上圖就可發現會有兩個 Server 端同時提供配置中心的服務,防止某一臺 down 掉之後影響整個系統的使用。

我們先單獨測試服務端,分別訪問:http://localhost:12000/config-client/dev 和 http://localhost:12001/config-client/dev 返回信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name": "config-client",
"profiles": [
"dev"
],
"label": null,
"version": "90dd76966da0eed967a0cbce3320f0f7ff63eb6b",
"state": null,
"propertySources": [
{
"name": "https://github.com/zhaoyibo/spring-cloud-study/config-repo/config-client-dev.yml",
"source": {
"info.profile": "dev update"
}
}
]
}

說明兩個 Server 端都正常讀取到了配置信息。

再次訪問 http://localhost:13000/info 返回dev update。說明客戶端已經讀取到了 Server 端的內容,我們隨機停掉一臺 Server 端的服務,再次訪問 http://localhost:13000/info 依然返回dev update,說明達到了高可用的目的。

相關閱讀

Spring Cloud(一):服務治理技術概覽
Spring Cloud(二):服務註冊與發現 Eureka
Spring Cloud(三):服務提供與調用 Eureka
Spring Cloud(四):服務容錯保護 Hystrix
Spring Cloud(五):Hystrix 監控面板
Spring Cloud(六):Hystrix 監控數據聚合 Turbine
Spring Cloud(七):配置中心(Git 版與動態刷新)
Spring Cloud(八):配置中心(服務化與高可用)
Spring Cloud(九):配置中心(消息總線)
Spring Cloud(十):服務網關 Zuul(路由)
Spring Cloud(十一):服務網關 Zuul(過濾器)
Spring Cloud(十二):分布式鏈路跟蹤(Sleuth 與 Zipkin)

示例代碼:GitHub

參考

springcloud(八):配置中心服務化和高可用
Spring Cloud 構建微服務架構(四)分布式配置中心(續)

  • 本文作者: Yibo
  • 本文鏈接: https://windmt.com/2018/04/19/spring-cloud-8-config-with-eureka/
  • 版權聲明: 本博客所有文章除特別聲明外,均采用 CC BY-NC-SA 4.0 許可協議。轉載請註明出處!

Spring Cloud(八):配置中心(服務化與高可用)【Finchley 版】