Spring-Cloud-Config快速開始 原

系列文章

Spring-Cloud-Config快速開始

Spring-Cloud-Config消息總線和高可用

Spring-Cloud-Config簡介

Spring-Cloud-Config是Sping-Cloud下用於分佈式配置管理的組件,分成了兩個角色Config-Server和Config-Client;Config-Server端集中式存儲/管理配置文件,並對外提供接口方便Config-Client訪問,接口使用HTTP的方式對外提供訪問;Config-Client通過接口獲取配置文件,然後可以在應用中使用;Config-Server存儲/管理的配置文件可以來自本地文件,遠程Git倉庫以及遠程Svn倉庫;

Config-Server端

1.Config-Server依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

注:2.0以後的版本需要jdk1.8及以上版本

2.準備被管理的配置文件

Spring-Cloud-Config提供了對多種環境配置文件的支持,比如:開發環境,測試環境,生產環境等;爲了更加全面的模擬,準備三個配置分別如下:

config-dev.properties
config-test.properties
config-pro.properties

分別是開發,測試以及生產的配置文件,內容也比較簡單如下所示:

foo=hello dev/test/pro

3.準備啓動配置文件

被管理的配置文件可以來自多個地方,包括:本地文件,遠程Git倉庫以及遠程Svn倉庫,下面分別在resources/application.properties中做配置;

3.1本地文件

spring.application.name=config-server
server.port=8888
spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=file:E:/github/spring-cloud-config-repo

指定了server端啓動端口爲8888,文件來自E:/github/spring-cloud-config-repo,以上三個文件放在此目錄下

3.2遠程Git倉庫

spring.application.name=config-server
server.port=8888
spring.profiles.active=git
spring.cloud.config.server.git.uri=https://github.com/ksfzhaohui/spring-cloud-config-repo
spring.cloud.config.server.git.default-label=master

spring.profiles.active默認值是git,git.uri指定地址,git倉庫default-label默認值是master;

3.3遠程svn倉庫

spring.profiles.active=subversion
spring.cloud.config.server.svn.uri=https://NJD9YZGJ2-PC.99bill.com:8443/svn/spring-cloud-config-repo
spring.cloud.config.server.svn.username=root
spring.cloud.config.server.svn.password=root
spring.cloud.config.server.svn.default-label=

配置了svn的用戶名和密碼,svn倉庫default-label默認值是trunk,因爲此處自建的svn服務器default-label爲空,所以設置爲空值即可;

4.準備啓動類

@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServer.class, args);
    }
}

@EnableConfigServer啓動配置服務器;

5.測試

不管使用以上的哪種方式配置,都可以通過使用http的方式訪問,http可以有以下幾種方式請求資源:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

application本實例中對應config;profile表示使用哪種環境的配置文件,這裏可以是dev,test,pro;label可選的標籤,git倉庫默認值master,svn倉庫默認值是trunk;

5.1請求http://localhost:8888/config/dev/master,結果如下:

{"name":"config","profiles":["dev"],"label":"master","version":"e9884489051c3b962840ac0a710f0f949a82d0ea","state":null,"propertySources":[{"name":"https://github.com/ksfzhaohui/spring-cloud-config-repo/config-dev.properties","source":{"foo":"hello dev"}}]}

返回結果包含了詳細的信息,最後的source裏面是配置文件內容;

5.2請求http://localhost:8888/config-dev.yml,結果如下:

foo: hello dev

此種方式訪問僅顯示配置文件內容,同樣properties後綴的也僅顯示配置文件內容,只是顯示的格式不一樣;

5.3更新git上文件內容,請求http://localhost:8888/config-dev.yml,結果如下:

foo: hello dev update

獲取到了最新的內容,其實每次在請求的時候都會去遠程倉庫中更新一下數據,日誌如下:

2018-07-13 09:43:07.606  INFO 14040 --- [nio-8888-exec-7] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@34107ea3: startup date [Fri Jul 13 09:43:07 CST 2018]; root of context hierarchy
2018-07-13 09:43:07.610  INFO 14040 --- [nio-8888-exec-7] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/HUIZHA~1.CFS/AppData/Local/Temp/config-repo-1042810186024067185/config-dev.properties
2018-07-13 09:43:07.611  INFO 14040 --- [nio-8888-exec-7] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@34107ea3: startup date [Fri Jul 13 09:43:07 CST 2018]; root of context hierarchy

把數據更新到本地的Temp路徑下;

Config-Client端

1.Config-Client依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

2.啓動配置文件

在配置文件resources/bootstrap.properties中做如下配置:

spring.application.name=config
spring.cloud.config.label=master
spring.cloud.config.profile=test
spring.cloud.config.uri= http://localhost:8888/
server.port=8889

spring.application.name:對應{application},本實例中是config;
spring.cloud.config.label:對應{label},指定server端配置的分支,此處填master即可;
spring.cloud.config.profile:對應{profile},指定client當前的環境,可選值:dev,test,pro;
spring.cloud.config.uri:server端地址;
server.port:client啓動端口;

3.準備測試類

@SpringBootApplication
public class ConfigClient {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClient.class, args);
    }
}
 
@RestController
public class HelloController {
 
    @Value("${foo}")
    String foo;
 
    @RequestMapping(value = "/hello")
    public String hello() {
        return foo;
    }
 
}

訪問地址:http://localhost:8889/hello,返回結果如下:

hello test

關於Spring-Cloud-Config配置的更新

1.Client端初始化配置文件

Client端在啓動的時候,可以發現Server端有拉取配置文件的日誌:

2018-07-13 12:47:36.330  INFO 13884 --- [nio-8888-exec-1] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1917a8ad: startup date [Fri Jul 13 12:47:36 CST 2018]; root of context hierarchy
2018-07-13 12:47:36.399  INFO 13884 --- [nio-8888-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/HUIZHA~1.CFS/AppData/Local/Temp/config-repo-1261377317774171312/config-test.properties
2018-07-13 12:47:36.400  INFO 13884 --- [nio-8888-exec-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1917a8ad: startup date [Fri Jul 13 12:47:36 CST 2018]; root of context hierarchy

2.Server端數據更新,Client如何更新

更新git中config-test.properties,請求http://localhost:8888/config-test.yml,結果如下:

foo: hello test update

Client請求http://localhost:8889/hello,結果如下:

hello test

可以發現Server端已經更新,但是Client端沒有獲取到最新的數據,還是使用的緩存的老數據;
Spring-Cloud-Config提供了多種刷新機制,下面看一下最簡單手動刷新:

2.1引入依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

2.2暴露全部endpoints

在bootstrap.properties中添加

management.endpoints.web.exposure.include=*

2.3.修改HelloController

@RefreshScope
@RestController
public class HelloController {
 
    @Value("${foo}")
    String foo;
 
    @RequestMapping(value = "/hello")
    public String hello() {
        return foo;
    }
}

@RefreshScope在手動執行刷新的時候會更新此變量

2.4.啓動

觀察啓動日誌,其中有一條映射如下:

2018-07-13 15:54:16.959 INFO 11372 --- [ main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/refresh],methods=[POST],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Objectorg.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)

/actuator/refresh提供了手動刷新的功能,並且必須使用POST方式;

2.5.測試

訪問地址:http://localhost:8889/hello,返回結果如下:

hello test

更新git上的配置文件,配置值爲foo=hello test update;

訪問地址:http://localhost:8889/hello,返回結果如下:

hello test

執行手動刷新操作:

C:\curl-7.61.0\I386>curl -X POST http://localhost:8889/actuator/refresh
["config.client.version","foo"]

訪問地址:http://localhost:8889/hello,返回結果如下:

hello test update

3.如何自動更新

在生產環境下不可能每次都去手動觸發refresh,github提供了webhook功能,當某個事件發生時,通過發送http的方式告訴接收方,這樣就可以在接收到事件的時候觸發refresh請求;

幾個待分析問題

1.多個Client節點如何更新

正常情況下Client會有很多個節點,而且節點會出現上線和下線,如何同時通知每個節點,Spring-Cloud-Config提供了Spring Cloud Bus來批量處理;

2.更新機制

在執行refresh的時候,只會把變動的參數發送給Client端,沒有變動的不會發送,節約了流量;但是如果配置文件被多個不同的Client使用,是否會出現不相干的參數會發送給每個Client;

3.多配置文件的支持

Server可以同時加載多個配置文件,Client也可以支持多個配置文件;

4.Server端如何保證數據的可靠性

Server端集中管理配置,所以服務的可靠性很重要;

總結

本文分別從Server端和Client端結合實例來介紹Spring-Cloud-Config如何使用,然後大體介紹瞭如何使用更新功能,最後留了幾個待分析的問題,後續進行分析。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章