本文接之前的《Spring Cloud構建微服務架構(四)分布式配置中心》,繼續來說說Spring Cloud Config的使用。
先來回顧一下,在前文中我們完成了什么:
- 構建了config-server,連接到Git倉庫
- 在Git上創建了一個config-repo目錄,用來存儲配置信息
- 構建了config-client,來獲取Git中的配置信息
在本文中,我們繼續來看看Spring Cloud Config的一些其他能力。
高可用問題
傳統作法
通常在生產環境,Config Server與服務注冊中心一樣,我們也需要將其擴展為高可用的集群。在之前實現的config-server基礎上來實現高可用非常簡單,不需要我們為這些服務端做任何額外的配置,只需要遵守一個配置規則:將所有的Config Server都指向同一個Git倉庫,這樣所有的配置內容就通過統一的共享文件系統來維護,而客戶端在指定Config Server位置時,只要配置Config Server外的均衡負載即可,就像如下圖所示的結構:
注冊為服務
雖然通過服務端負載均衡已經能夠實現,但是作為架構內的配置管理,本身其實也是可以看作架構中的一個微服務。所以,另外一種方式更為簡單的方法就是把config-server也注冊為服務,這樣所有客戶端就能以服務的方式進行訪問。通過這種方法,只需要啟動多個指向同一Git倉庫位置的config-server就能實現高可用了。
配置過程也非常簡單,具體如下:
config-server配置
- 在
pom.xml
的dependencies節點中引入如下依賴,相比之前的config-server就,加入了spring-cloud-starter-eureka
,用來注冊服務
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
- 在
application.properties
中配置參數eureka.client.serviceUrl.defaultZone
以指定服務注冊中心的位置,詳細內容如下:
spring.application.name=config-server
server.port=7001
# 配置服務注冊中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
# git倉庫配置
spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/SpringCloud-Learning/
spring.cloud.config.server.git.searchPaths=Chapter1-1-8/config-repo
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password
- 在應用主類中,新增
@EnableDiscoveryClient
注解,用來將config-server注冊到上面配置的服務注冊中心上去。
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
- 啟動該應用,并訪問
http://localhost:1111/
,可以在Eureka Server的信息面板中看到config-server已經被注冊了。
config-client配置
- 同config-server一樣,在
pom.xml
的dependencies節點中新增spring-cloud-starter-eureka
依賴,用來注冊服務:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
- 在
bootstrap.properties
中,按如下配置:
spring.application.name=didispace
server.port=7002
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.profile=dev
其中,通過eureka.client.serviceUrl.defaultZone
參數指定服務注冊中心,用于服務的注冊與發現,再將spring.cloud.config.discovery.enabled
參數設置為true,開啟通過服務來訪問Config Server的功能,最后利用spring.cloud.config.discovery.serviceId
參數來指定Config Server注冊的服務名。這里的spring.application.name
和spring.cloud.config.profile
如之前通過URI的方式訪問時候一樣,用來定位Git中的資源。
- 在應用主類中,增加
@EnableDiscoveryClient
注解,用來發現config-server服務,利用其來加載應用配置
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
- 沿用之前我們創建的Controller來加載Git中的配置信息
@RefreshScope
@RestController
public class TestController {
@Value("${from}")
private String from;
@RequestMapping("/from")
public String from() {
return this.from;
}
}
- 完成了上述配置之后,我們啟動該客戶端應用。若啟動成功,訪問
http://localhost:1111/
,可以在Eureka Server的信息面板中看到該應用已經被注冊成功了。
- 訪問客戶端應用提供的服務:
http://localhost:7002/from
,此時,我們會返回在Git倉庫中didispace-dev.properties
文件配置的from屬性內容:"git-dev-1.0"。
配置刷新
有時候,我們需要對配置內容做一些實時更新的場景,那么Spring Cloud Config是否可以實現呢?答案顯然是可以的。下面,我們看看如何進行改造來實現配置內容的實時更新。
在改造程序之前,我們先將config-server和config-client都啟動起來,并訪問客戶端提供的REST APIhttp://localhost:7002/from
來獲取配置信息,可以獲得返回內容為:git-dev-1.0
。接著,我們可以嘗試使用Git工具修改當前配置的內容,比如,將config-repo/didispace-dev.properties
中的from的值從from=git-dev-1.0
修改為from=git-dev-2.0
,再訪問http://localhost:7002/from
,可以看到其返回內容還是git-dev-1.0
。
下面,我們將在config-client端增加一些內容和操作以實現配置的刷新:
- 在config-clinet的
pom.xml
中新增spring-boot-starter-actuator
監控模塊,其中包含了/refresh
刷新API。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 重新啟動config-clinet,訪問一次
http://localhost:7002/from
,可以看到當前的配置值 - 修改Git倉庫
config-repo/didispace-dev.properties
文件中from
的值 - 再次訪問一次
http://localhost:7002/from
,可以看到配置值沒有改變 - 通過POST請求發送到
http://localhost:7002/refresh
,我們可以看到返回內容如下,代表from
參數的配置內容被更新了
[
"from"
]
- 再次訪問一次
http://localhost:7002/from
,可以看到配置值已經是更新后的值了
通過上面的介紹,大家不難想到,該功能還可以同Git倉庫的Web Hook功能進行關聯,當有Git提交變化時,就給對應的配置主機發送/refresh
請求來實現配置信息的實時更新。但是,當我們的系統發展壯大之后,維護這樣的刷新清單也將成為一個非常大的負擔,而且很容易犯錯,那么有什么辦法可以解決這個復雜度呢?后續我們將繼續介紹如何通過Spring Cloud Bus來實現以消息總線的方式進行通知配置信息的變化,完成集群上的自動化更新。
本文完整示例: