分佈式配置中心之spring-cloud-config

spring-cloud-config 配置中心實現

Spring Cloud Config 用於爲分佈式系統中的基礎設施和微服務應用提供集中化的外部配置支持,分爲server端和client端。 server端爲分佈式配置中心,是一個獨立的微服務應用;client端爲分佈式系統中的基礎設置或微服務應用,經過指定配置中心來管理相關的配置。 Spring Cloud Config 構建的配置中心,除了適用於 Spring 構建的應用外,也能夠在任何其餘語言構建的應用中使用。 Spring Cloud Config 默認採用 Git 存儲配置信息,支持對配置信息的版本管理。html

本示例主要內容

  • 配置中心演示client端和server端實現
  • 配置文件放在git(因github有時候不太穩定,我放到了國內服務器)
  • 版本切換(test、pro、dev)

Spring Cloud Config 特色

  • 提供server端和client端支持(Spring Cloud Config Server和Spring Cloud Config Client);
  • 集中式管理分佈式環境下的應用配置;
  • 基於Spring環境,實現了與Spring應用無縫集成;
  • 可用於任何語言開發的程序;
  • 默認實現基於Git倉庫(也支持SVN),從而能夠進行配置的版本管理;同時也支持配置從本地文件或數據庫讀取。

代碼構建

server端實現

1.pom.xml添加maven依賴

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

2.application.yml配置

server:
  port: 8001
spring:
  application:
    name: cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/tqlin/spring-boot-demo.git #由於github有時候不穩定,我這裏改到了碼雲倉
          searchPaths: /cloud-config/config-repo/           #配置文件目錄
          force-pull: true

3.CloudConfigServerApplication.java啓動類

@EnableConfigServer
@SpringBootApplication
public class CloudConfigServerApplication {

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

client端實現

1.pom.xml添加maven依賴

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

2.bootstrap.properties配置文件

spring.cloud.config.name=easy-config
spring.cloud.config.profile=test
spring.cloud.config.uri=http://localhost:8001/
spring.cloud.config.label=master
  • spring.application.name:對應{application}部分java

  • spring.cloud.config.profile:對應{profile}部分git

  • spring.cloud.config.label:對應git的分支。若是配置中心使用的是本地存儲,則該參數無用github

  • spring.cloud.config.uri:配置中心的具體地址(sever端地址)web

  • spring.cloud.config.discovery.service-id:指定配置中心的service-id,便於擴展爲高可用配置集羣。spring

    特別注意:Spring Cloud 構建於 Spring Boot 之上,在 Spring Boot 中有兩種上下文,一種是 bootstrap, 另一種是 application, bootstrap 是應用程序的父上下文,也就是說 bootstrap 加載優先於 applicaton。bootstrap 主要用於從額外的資源來加載配置信息,還能夠在本地外部配置文件中解密屬性。這兩個上下文共用一個環境,它是任何Spring應用程序的外部屬性的來源。bootstrap 裏面的屬性會優先加載,它們默認也不能被本地相同配置覆蓋。數據庫

3.application.properties配置文件

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

運行示例

1.首先在碼雲上面建立一個文件夾config-repo用來存放配置文件,咱們建立如下三個配置文件:

// 開發環境
  easy-config-dev.properties    內容爲:easy.hello=dev config
  // 測試環境
  easy-config-test.properties   內容爲:easy.hello=test config
  // 生產環境
  easy-config-pro.properties    內容爲:easy.hello=pro config

根據上面構建的代碼指定的項目地址爲:https://gitee.com/tqlin/spring-boot-demo.git 目錄爲: /cloud-config/config-repo/json

2.分別運行server端和client端

找到CloudConfigServerApplication.java、CloudConfigClientApplication.java分別運行bootstrap

3.測試server端

直接訪問:http://localhost:8001/easy-config/dev服務器

咱們看到成功返回了開發配置文件信息

{
name: "easy-config",
profiles: [
"dev"
],
label: null,
version: "6053b4c1c2343ac27e822b2a9b60c6343be72f96",
state: null,
propertySources: [
{
name: "https://gitee.com/tqlin/spring-boot-demo.git/cloud-config/config-repo/easy-config-dev.properties",
source: {
easy.hello: "dev config"
}
}
]
}

訪問:http://localhost:8001/easy-config/test、http://localhost:8001/easy-config/pro,相應的會返回測試及正式環境的配置

倉庫中的配置文件會被轉換成web接口,訪問能夠參照如下的規則:

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

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

4.測試client端

訪問:http://localhost:8002/hello 咱們發現界面成功返回了 test config,說明測試配置文件client端讀取成功了

咱們修改bootstrap.properties配置的spring.cloud.config.profile的值爲dev,重啓client端,訪問:http://localhost:8002/hello 這時候界面返回 dev config,表示開發配置訪問成功。

資料

原文出處:https://www.cnblogs.com/tqlin/p/11401870.html

相關文章
相關標籤/搜索