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

隨着線上項目變的日益龐大,每一個項目都散落着各類配置文件,若是採用分佈式的開發模式,須要的配置文件隨着服務增長而不斷增多。某一個基礎服務信息變動,都會引發一系列的更新和重啓,運維苦不堪言也容易出錯。配置中心即是解決此類問題的靈丹妙藥。git

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

Spring Cloud Config

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

  • 提供服務端和客戶端支持spring

  • 集中管理各環境的配置文件bootstrap

  • 配置文件修改以後,能夠快速的生效瀏覽器

  • 能夠進行版本管理併發

  • 支持大的併發查詢app

  • 支持各類語言運維

Spring Cloud Config能夠完美的支持以上全部的需求。分佈式

Spring Cloud Config項目是一個解決分佈式系統的配置管理方案。它包含了Client和Server兩個部分,server提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client經過接口獲取數據、並依據此數據初始化本身的應用。Spring cloud使用git或svn存放配置文件,默認狀況下使用git,咱們先以git爲例作一套示例。

首先在github上面建立了一個文件夾config-repo用來存放配置文件,爲了模擬生產環境,咱們建立如下三個配置文件:

 
  1. // 開發環境

  2. neo-config-dev.properties

  3. // 測試環境

  4. neo-config-test.properties

  5. // 生產環境

  6. neo-config-pro.properties

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

server 端

一、添加依賴

 
  1. <dependencies>

  2.    <dependency>

  3.        <groupId>org.springframework.cloud</groupId>

  4.        <artifactId>spring-cloud-config-server</artifactId>

  5.    </dependency>

  6. </dependencies>

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

二、配置文件

 
  1. server:

  2.  port: 8040

  3. spring:

  4.  application:

  5.    name: spring-cloud-config-server

  6.  cloud:

  7.    config:

  8.      server:

  9.        git:

  10.          uri: https://github.com/ityouknow/spring-cloud-starter/     # 配置git倉庫的地址

  11.          search-paths: config-repo                             # git倉庫地址下的相對地址,能夠配置多個,用,分割。

  12.          username:                                             # git倉庫的帳號

  13.          password:                                             # git倉庫的密碼

Spring Cloud Config也提供本地存儲配置的方式。咱們只須要設置屬性 spring.profiles.active=native,Config Server會默認從應用的 src/main/resource目錄下檢索配置文件。也能夠經過 spring.cloud.config.server.native.searchLocations=file:E:/properties/屬性來指定配置文件的位置。雖然Spring Cloud Config提供了這樣的功能,可是爲了支持更好的管理內容和版本控制的功能,仍是推薦使用git的方式。

三、啓動類

啓動類添加 @EnableConfigServer,激活對配置中心的支持

 
  1. @EnableConfigServer

  2. @SpringBootApplication

  3. public class ConfigServerApplication {

  4.  

  5.    public static void main(String[] args) {

  6.        SpringApplication.run(ConfigServerApplication.class, args);

  7.    }

  8. }

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

四、測試

首先咱們先要測試server端是否能夠讀取到github上面的配置信息,直接訪問: http://localhost:8001/neo-config/dev

返回信息以下:

 
  1. {

  2.    "name": "neo-config",

  3.    "profiles": [

  4.        "dev"

  5.    ],

  6.    "label": null,

  7.    "version": null,

  8.    "state": null,

  9.    "propertySources": [

  10.        {

  11.            "name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties",

  12.            "source": {

  13.                "neo.hello": "hello im dev"

  14.            }

  15.        }

  16.    ]

  17. }

上述的返回的信息包含了配置文件的位置、版本、配置文件的名稱以及配置文件中的具體內容,說明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>

  2.    <dependency>

  3.        <groupId>org.springframework.cloud</groupId>

  4.        <artifactId>spring-cloud-starter-config</artifactId>

  5.    </dependency>

  6.    <dependency>

  7.        <groupId>org.springframework.boot</groupId>

  8.        <artifactId>spring-boot-starter-web</artifactId>

  9.    </dependency>

  10.    <dependency>

  11.        <groupId>org.springframework.boot</groupId>

  12.        <artifactId>spring-boot-starter-test</artifactId>

  13.        <scope>test</scope>

  14.    </dependency>

  15. </dependencies>

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

二、配置文件

須要配置兩個配置文件,application.properties和bootstrap.properties

application.properties以下:

 
  1. spring.application.name=spring-cloud-config-client

  2. server.port=8002

bootstrap.properties以下:

 
  1. spring.cloud.config.name=neo-config

  2. spring.cloud.config.profile=dev

  3. spring.cloud.config.uri=http://localhost:8001/

  4. 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。

三、啓動類

啓動類添加 @EnableConfigServer,激活對配置中心的支持

 
  1. @SpringBootApplication

  2. public class ConfigClientApplication {

  3.  

  4.    public static void main(String[] args) {

  5.        SpringApplication.run(ConfigClientApplication.class, args);

  6.    }

  7. }

啓動類只須要 @SpringBootApplication註解就能夠

四、web測試

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

 
  1. @RestController

  2. class HelloController {

  3.    @Value("${neo.hello}")

  4.    private String hello;

  5.  

  6.    @RequestMapping("/hello")

  7.    public String from() {

  8.        return this.hello;

  9.    }

  10. }

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

相關文章
相關標籤/搜索