5--SpringCloud搭建分佈式配置中心

  Spring Cloud Config爲服務端和客戶端提供了分佈式系統的外部化配置支持。配置服務器爲各應用的全部環境提供了一箇中心化的外部配置。做爲一個應用能夠經過部署管道來進行測試或者投入生產,咱們能夠分別爲這些環境建立配置,而且在須要遷移環境的時候獲取對應環境的配置來運行。git

  配置服務器默認採用git來存儲配置信息,這樣就有助於對環境配置進行版本管理,而且能夠經過git客戶端工具來方便的管理和訪問配置內容。固然他也提供本地化文件系統的存儲方式,下面從這兩方面介紹如何使用分佈式配置來存儲微服務應用多環境的配置內容。web

 

構建Config Server

  建立一個簡單的springBoot項目 pom加上依賴spring

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

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

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Brixton.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

 

  建立Spring Boot的程序主類,並添加@EnableConfigServer註解,開啓Config Serverbootstrap

@EnableConfigServer
@SpringBootApplication
public class Application {

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

 

  建立application.properties配置文件服務器

spring.application.name=config-server
server.port=7001

# git配置
#所在項目根目錄  配置git倉庫位置
spring.cloud.config.server.git.uri=https://gitee.com/cengjiang/springcloud_learning/
#所在地址目錄 配置倉庫路徑下的相對搜索位置,能夠配置多個
spring.cloud.config.server.git.searchPaths=5--SpringCloud--Config
#若是是公開項目則不用寫用戶名密碼
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password

  到這裏,使用一個經過Spring Cloud Config實現,並使用git管理內容的配置中心已經完成了,啓動該應用,成功後開始下面的內容。app

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

 

服務器端驗證

  個人git倉庫是在碼雲上建立的,裏面存放在springCloud系列的示例代碼。你們不想建立git倉庫的也可使用我這個地址。spring-boot

  在倉庫地址https://gitee.com/cengjiang/springcloud_learning/下面建立了一個目錄5--SpringCloud--Confi存放配置文件做爲配置倉庫,並根據不一樣環境新建了下面四個配置文件: 微服務

    ghghspace-dev.properties工具

    ghghspace-prod.properties

    ghghspace-test.properties

    ghghspace.properties

  其中設置了一個from屬性,爲每一個配置文件分別設置了不一樣的值,如:

    from=git-default-1.0

    from=git-dev-1.0

    from=git-test-1.0

    from=git-prod-1.0

  爲了測試版本控制,在master中,咱們都加入1.0的後綴,同時建立一個config-label-test分支,並將各配置文件中的值用2.0做爲後綴。

 

  URL與配置文件的映射關係以下:     application(應用)、profile(環境)、label(分支)

       /{application}/{profile}[/{label}]

    yml文件:

      /{application}-{profile}.yml

      /{application}-{profile}/{label}.yml

    properties

      /{application}-{profile}.properties

      /{application}-{profile}/{label}.properties

  訪問:

  {label}對應git上不一樣的分支,默認爲master。

  咱們能夠嘗試構造不一樣的url來訪問不一樣的配置內容,好比:要訪問config-label-test分支,ghghspace應用的prod環境,能夠經過這個url:http://localhost:7001/ghghspace/prod/config-label-test

   

 

微服務端映射配置

   在完成並驗證了配置服務中心以後,下面看看咱們如何在微服務應用中獲取配置信息。

  建立一個簡單的springBoot項目config-client pom加上依賴

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <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>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Brixton.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

 

 

  建立啓動主類

@SpringBootApplication
public class Application {

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

}

 

  

  建立bootstrap.properties

#對應前配置文件中的{application}部分
spring.application.name=ghghspace
#對應前配置文件中的{profile}部分
spring.cloud.config.profile=dev
#對應前配置文件的git分支
spring.cloud.config.label=master
#配置中心的訪問地址
spring.cloud.config.uri=http://localhost:7001/

server.port=7002

 

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

   

  建立一個Rest Api來返回配置中心的from屬性,具體以下:

@RefreshScope
@RestController
public class TestController {

    @Value("${from}")
    private String from;

    @RequestMapping("/from")
    public String from() {

        return this.from;
    }

}

 

  經過@Value("${from}")綁定配置服務中配置的from屬性。

  啓動該應用,並訪問:http://localhost:7002/from ,咱們就能夠根據配置內容輸出對應環境的from內容了。

  

  源碼下載: 5--SpringCloud--Config

相關文章
相關標籤/搜索