SpringCloud學習第七篇:Config學習(Greenwich-SR1版本)

一、Config簡介

在分佈式系統中,由於服務數量巨多,爲了方便服務配置文件統一管理,實時更新,所以需要分佈式配置中心組件。市面上開源的配置中心有很多。如:360的QConf、淘寶的diamond、百度的disconf都是解決這類問題。國外也有很多開源的配置中心Apache的Apache Commons Configuration、owner、cfg4j等等。在Spring Cloud中,有分佈式配置中心組件spring cloud config ,它支持配置服務放在配置服務的內存中(即本地),也支持放在遠程Git倉庫中。在spring cloud config 組件中,分兩個角色,一是config server,二是config client。

二、ConfigServer入門

  • Maven
 <parent>
        <artifactId>cloud-parent</artifactId>
        <groupId>com.yk</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-config-server</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>
  • 啓動入口類
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableConfigServer

public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class,args);
    }
}
  • application.properties
spring.application.name=spring-cloud-config-server
server.port=901

#配置Git 倉庫位置
spring.cloud.config.server.git.uri=https://github.com/xxxxx/configDemo.git
#配置倉庫路徑下的相對搜索位置, 可以配置多個
spring.cloud.config.server.git.searchPaths=config
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password

到這裏就簡單的完成了使用Git完成分佈式配置中心。

  • 配置規則詳解
    • 驗證分佈式配置中心,根據配置的git倉庫,在configDemo.git下面創建config目錄作爲配置倉庫。
      在這裏插入圖片描述

    • 並創建四個不同環境的配置文件

didispace.properties
didispace-dev.properties
didispace-test.properties
didispace-prod.properties
- 四個配置文件均設置一個from屬性,並設置不同的值
from =git-default-1.0
from =git-dev-1.0
from =git-test-1.0
frorn=git-prod-1.0

在這裏插入圖片描述

- 訪問配置信息的Url與配置文件映射關係
/{application}/{profile} [/{label}]
/{application}-{profile}. yrnl
/{label}/{applica七ion}-{profile}.yrnl
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

上面的url會映射{application}-{profile} .properties 對應的配置文件, 其中{label}對應Git上不同的分支,默認爲 master。
在這裏插入圖片描述

在這裏插入圖片描述

三、ConfigClient入門

  • maven
 <parent>
        <artifactId>cloud-parent</artifactId>
        <groupId>com.yk</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-config-client</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
</dependencies>

  • bootstrap.properties
spring.application.name=spring-cloud-config-client
server.port=903
#對應配置規則的{application}
spring.cloud.config.name=didispace
#對應配置規則的{profile}
spring.cloud.config.profile=dev 
#對應配置規則的{label}
spring.cloud.config.label=master 
#config-server 服務地址
spring.cloud.config.uri=http://localhost:901/    

這裏的配置必須配置在bootstrap.properties裏面,對於本應用jar包之外的配置文件加載會優先於應用jar包內的配置內容,而通過bootstrap.properties對config-server 的配置,使得該應用會從config-server中獲取一些外部配置信息,這些信息的優先級比本地的內容要高, 從而實現了外部化配置

  • Controleer
@RestController
public class ConfigController {
    //獲取配置文件裏面的from屬性
    @Value("${from}")
    private String from;

    @RequestMapping("/from")
    public String from() {
        return this.from;
    }
}

  • 啓動
    在這裏插入圖片描述
    在這裏插入圖片描述

四、動態刷新配置

客戶端可以從服務端REST接口獲取配置。但客戶端並不能主動感知到配置的變化,從而主動去獲取新的配置。客戶端如何去主動獲取新的配置信息呢,springcloud已經給我們提供瞭解決方案,每個客戶端通過POST方法觸發各自的/refresh。

  • 在Client增加依賴
<!-- 是一套監控的功能,可以監控程序在運行時狀態,其中就包括/refresh的功能。-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

  • 配置文件增加
management.endpoints.web.exposure.include=*

  • 開啓更新機制
//在之前Client的Controller類上增加
@RefreshScope

  • 重啓Client服務
  • 修改倉庫配置文件的參數
    修改didispace.dev.properties裏面from的參數
    通過postman訪問http://localhost:903/actuator/refresh
    在這裏插入圖片描述
    在這裏插入圖片描述

這樣就實現了動態獲取配置

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