1. 程式人生 > >Spring Cloud Config 配置中心 從git獲取配置 (沒結合註冊中心 eureka)

Spring Cloud Config 配置中心 從git獲取配置 (沒結合註冊中心 eureka)

配置中心的作用:1)集中管理公共配置   2)方便切換配置的版本  例如dev  test proted等

配置中心從git或者本地讀取配置,其它服務只要引入配置中心地址,配置好需要的"檔名"+"版本"即可

git裡的配置檔案 可以是 yml和properties形式,就是書寫規則不一樣而已

配置檔案優先順序 bootstrap.yml>application.yml>git 檔案裡的 yml

在相同優先順序位置同時有application.properties和application.yml,那麼application.yml裡面的屬性就會覆蓋application.properties裡的屬性。

配置yml檔案

user:
    username: admin
    password: 123

或者properties檔案

login.username=admin
login.password=123
  • 假設我們讀取配置中心的應用名為config-client,那麼我們可以在git倉庫中該專案的預設配置檔案config-client.yml

info:
  profile: default
  • 為了演示載入不同環境的配置,我們可以在git倉庫中再建立一個針對dev環境的配置檔案config-client-dev.yml
info:
  profile: dev

構建配置中心

通過Spring Cloud Config來構建一個分散式配置中心非常簡單,只需要三步:

  • 建立一個基礎的Spring Boot工程,命名為:config-server-git,並在pom.xml中引入下面的依賴(省略了parent和dependencyManagement部分):
<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
	</dependency>
</dependencies>
  • 建立Spring Boot的程式主類,並新增@EnableConfigServer註解,開啟Spring Cloud Config的服務端功能。
@EnableConfigServer
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class).web(true).run(args);
	}

}
  • application.yml中新增配置服務的基本資訊以及Git倉庫的相關資訊,例如:
spring
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: http://git.oschina.net/didispace/config-repo-demo/
server:
  port: 1201

到這裡,使用一個通過Spring Cloud Config實現,並使用Git管理配置內容的分散式配置中心就已經完成了。我們可以將該應用先啟動起來,確保沒有錯誤產生,然後再嘗試下面的內容。

如果我們的Git倉庫需要許可權訪問,那麼可以通過配置下面的兩個屬性來實現;
spring.cloud.config.server.git.username:訪問Git倉庫的使用者名稱
spring.cloud.config.server.git.password:訪問Git倉庫的使用者密碼

完成了這些準備工作之後,我們就可以通過瀏覽器、POSTMAN或CURL等工具直接來訪問到我們的配置內容了。訪問配置資訊的URL與配置檔案(本地或者git裡的配置檔案  不是專案裡的配置檔案的內容)的對映關係如下:application 就是檔名的前半部分,profile就是開發版本 label就是git分支

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

上面的url會對映{application}-{profile}.properties對應的配置檔案,其中{label}對應Git上不同的分支,預設為master。我們可以嘗試構造不同的url來訪問不同的配置內容,比如,要訪問master分支,config-client應用的dev環境,就可以訪問這個url:http://localhost:1201/config-client/dev/master,並獲得如下返回:  就是根據git裡的配置檔名字解析出來的

(這裡就是配置檔案規則,客戶端藥參考這裡)

{
    "name": "config-client",
    "profiles": [
        "dev"
    ],
    "label": "master",
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "http://git.oschina.net/didispace/config-repo-demo/config-client-dev.yml",
            "source": {
                "info.profile": "dev"
            }
        },
        {
            "name": "http://git.oschina.net/didispace/config-repo-demo/config-client.yml",
            "source": {
                "info.profile": "default"
            }
        }
    ]
}

我們可以看到該Json中返回了應用名:config-client,環境名:dev,分支名:master,以及default環境和dev環境的配置內容。

構建客戶端

在完成了上述驗證之後,確定配置服務中心已經正常運作,下面我們嘗試如何在微服務應用中獲取上述的配置資訊。

  • 建立一個Spring Boot應用,命名為config-client,並在pom.xml中引入下述依賴:
<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>
</dependencies>
  • 建立Spring Boot的應用主類,具體如下:
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class).web(true).run(args);
	}

}
  • 建立bootstrap.yml配置,來指定獲取配置檔案的config-server-git位置,例如:

這裡需要格外注意:上面這些屬性必須配置在bootstrap.properties中,config部分內容才能被正確載入。因為config的相關配置會先於application.properties,而bootstrap.properties的載入也是先於application.properties

上述配置引數與Git中儲存的配置檔案中各個部分的對應關係如下:

  • spring.application.name:對應配置檔案規則中的{application}部分  也就是git裡檔名字的前半部分
  • spring.cloud.config.profile:對應配置檔案規則中的{profile}部分  也就是git裡檔名字的後半
  • spring.cloud.config.label:對應配置檔案規則中的{label}部分  也就是git的分支
  • spring.cloud.config.uri:配置中心config-server的地址

這裡需要格外注意:上面這些屬性必須配置在bootstrap.properties中,這樣config-server中的配置資訊才能被正確載入。

在完成了上面你的程式碼編寫之後,讀者可以將config-server-git、config-client都啟動起來,然後訪問http://localhost:2001/info ,我們可以看到該端點將會返回從git倉庫中獲取的配置資訊:

{
    "profile": "default"
}

另外,我們也可以修改config-client的profile為dev來觀察載入配置的變化。