1. 程式人生 > >Spring Cloud Config配置中心使用及介紹

Spring Cloud Config配置中心使用及介紹

簡介:我們之前建立的微服務例項的配置檔案都是配置在應用程式裡面的,如Application.properties等,這麼做的第一個缺點是一旦修改配置檔案,必須要修改應用且必須重啟例項才能使配置生效,第二個缺點是加入我們要部署多個微服務例項的時候,假如使用的是同一個配置檔案,那麼有多少個節點我們就要修改且重新部署多少次,容易遺漏出錯,效率不高。此時,Spring Cloud Config應運而生,他是一個獨立部署的微服務,可以實現微服務配置檔案的動態獲取及重新整理,完美的解決了上述兩個問題,下面讓我們來實現一個簡單的Spring Cloud Config例項:

1、Config服務端:

第一步:引入pom以來:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.spring.cloud</groupId>
    <artifactId>config-server</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.7.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <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.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

第二步:建立啟動主類,並新增@EnableConfigServer註解:

package controller;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
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遠端倉庫地址,具體到專案
spring.cloud.config.server.git.uri=http://git.baozun.com/tanliwei/test.git
#配置倉庫下的相對搜尋路徑,可以配置多個
spring.cloud.config.server.git.searchPaths=config
#配置git倉庫的使用者名稱
spring.cloud.config.server.git.username=username
#配置git倉庫的密碼
spring.cloud.config.server.git.password=password

到這裡,使用一個通過Spring Cloud Config實現,並使用git管理配置內容的分散式配置中心就完成了。

我們可以通過瀏覽器、Postman、CURL工具直接訪問配置內容,訪問配置資訊的URL和配置檔案的對映關係如下所示:

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

/{application}-{profile}.yml

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

/{application}-{profile}.properties

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

lable:對應不同的分支,預設是master

config-server是先從git上clone一份配置檔案存在本地,然後讀取這些內容並返回給微服務應用進行載入,這樣做的好處是當無法連線git倉庫時,能直接從config-server本地獲取配置檔案返回給客戶端;

2、客戶端配置:

第一步:引入pom依賴:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.spring.cloud</groupId>
    <artifactId>config-client</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.7.RELEASE</version>
        <relativePath/>
    </parent>

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

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

</project>

第二步:建立應用主類:

package controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

第三步:resource目錄下面建立bootstrap.properties啟動配置檔案:

spring.application.name=didispace//對應配置檔案規則中的{application}部分
server.port=7002
spring.cloud.config.profile=dev//對應配置檔案規則中的{profile}部分
spring.cloud.config.label=master//對應配置檔案規則中的{label}部分
spring.cloud.config.uri=http://localhost:7001//配置中心config-server的地址

第四步:測試,新增一個介面返回配置檔案中的某個屬性:

package controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
public class TestController {

    @Value("${client.param}")
    private String form;

    @RequestMapping(value = "/form")
    public String form() {
        return this.form;
    }
}