springcloud(六):配置中心git示例

純潔的微笑發表於2017-05-23

隨著線上專案變的日益龐大,每個專案都散落著各種配置檔案,如果採用分散式的開發模式,需要的配置檔案隨著服務增加而不斷增多。某一個基礎服務資訊變更,都會引起一系列的更新和重啟,運維苦不堪言也容易出錯。配置中心便是解決此類問題的靈丹妙藥。

市面上開源的配置中心有很多,BAT每家都出過,360的QConf、淘寶的diamond、百度的disconf都是解決這類問題。國外也有很多開源的配置中心Apache的Apache Commons Configuration、owner、cfg4j等等。這些開源的軟體以及解決方案都很優秀,但是我最鍾愛的卻是Spring Cloud Config,因為它功能全面強大,可以無縫的和spring體系相結合,夠方便夠簡單顏值高我喜歡。


Spring Cloud Config

在我們瞭解spring cloud config之前,我可以想想一個配置中心提供的核心功能應該有什麼

  • 提供服務端和客戶端支援
  • 集中管理各環境的配置檔案
  • 配置檔案修改之後,可以快速的生效
  • 可以進行版本管理
  • 支援大的併發查詢
  • 支援各種語言

Spring Cloud Config可以完美的支援以上所有的需求。

Spring Cloud Config專案是一個解決分散式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置檔案的儲存、以介面的形式將配置檔案的內容提供出去,client通過介面獲取資料、並依據此資料初始化自己的應用。Spring cloud使用git或svn存放配置檔案,預設情況下使用git,我們先以git為例做一套示例。

首先在github上面建立了一個資料夾config-repo用來存放配置檔案,為了模擬生產環境,我們建立以下三個配置檔案:

// 開發環境
neo-config-dev.properties
// 測試環境
neo-config-test.properties
// 生產環境
neo-config-pro.properties

每個配置檔案中都寫一個屬性neo.hello,屬性值分別是 hello im dev/test/pro 。下面我們開始配置server端


server 端

1、新增依賴

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>

只需要加入spring-cloud-config-server包引用既可。

2、配置檔案

server:
  port: 8040
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/ityouknow/spring-cloud-starter/     # 配置git倉庫的地址
          search-paths: config-repo                             # git倉庫地址下的相對地址,可以配置多個,用,分割。
          username:                                             # git倉庫的賬號
          password:                                             # git倉庫的密碼

3、啟動類

啟動類新增@EnableConfigServer,啟用對配置中心的支援

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

到此server端相關配置已經完成

4、測試

首先我們先要測試server端是否可以讀取到github上面的配置資訊,直接訪問:http://localhost:8001/neo-config/dev

返回資訊如下:

{
    "name": "neo-config", 
    "profiles": [
        "dev"
    ], 
    "label": null, 
    "version": null, 
    "state": null, 
    "propertySources": [
        {
            "name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties", 
            "source": {
                "neo.hello": "hello im dev"
            }
        }
    ]
}

上述的返回的資訊包含了配置檔案的位置、版本、配置檔案的名稱以及配置檔案中的具體內容,說明server端已經成功獲取了git倉庫的配置資訊。

如果直接檢視配置檔案中的配置資訊可訪問:http://localhost:8001/neo-config-dev.properties,
返回:neo.hello: hello im dev

修改配置檔案neo-config-dev.properties中配置資訊為:neo.hello=hello im dev update,再次在瀏覽器訪問http://localhost:8001/neo-config-dev.properties,返回:neo.hello: hello im dev update。說明server端會自動讀取最新提交的內容

倉庫中的配置檔案會被轉換成web介面,訪問可以參照以下的規則:

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

以neo-config-dev.properties為例子,它的application是neo-config,profile是dev。client會根據填寫的引數來選擇讀取對應的配置。

client 端

主要展示如何在業務專案中去獲取server端的配置資訊

1、新增依賴

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

引入spring-boot-starter-web包方便web測試

2、配置檔案

需要配置兩個配置檔案,application.properties和bootstrap.properties

application.properties如下:

spring.application.name=spring-cloud-config-client
server.port=8002

bootstrap.properties如下:

spring.cloud.config.name=neo-config
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8001/
spring.cloud.config.label=master
  • spring.application.name:對應{application}部分
  • spring.cloud.config.profile:對應{profile}部分
  • spring.cloud.config.label:對應git的分支。如果配置中心使用的是本地儲存,則該引數無用
  • spring.cloud.config.uri:配置中心的具體地址
  • spring.cloud.config.discovery.service-id:指定配置中心的service-id,便於擴充套件為高可用配置叢集。

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

3、啟動類

啟動類新增@EnableConfigServer,啟用對配置中心的支援

@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

啟動類只需要@SpringBootApplication註解就可以

4、web測試

使用@Value註解來獲取server端引數的值

@RestController
class HelloController {
    @Value("${neo.hello}")
    private String hello;

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

啟動專案後訪問:http://localhost:8002/hello,返回:hello im dev update說明已經正確的從server端獲取到了引數。到此一個完整的服務端提供配置服務,客戶端獲取配置引數的例子就完成了。


我們在進行一些小實驗,手動修改neo-config-dev.properties中配置資訊為:neo.hello=hello im dev update1提交到github,再次在瀏覽器訪問http://localhost:8002/hello,返回:neo.hello: hello im dev update,說明獲取的資訊還是舊的引數,這是為什麼呢?因為spirngboot專案只有在啟動的時候才會獲取配置檔案的值,修改github資訊後,client端並沒有在次去獲取,所以導致這個問題。如何去解決這個問題呢?留到下一章我們在介紹。

示例程式碼


作者:純潔的微笑
出處:http://www.ityouknow.com/
版權歸作者所有,轉載請註明出處

相關文章