Spring Cloud Config分布式配置中心的使用和遇到的坑


分布式配置中心

為什么要有用分布式配置中心這玩意兒?現在這微服務大軍已經覆蓋了各種大小型企業,每個服務的粒度相對較小,因此系統中會出現大量的服務,每個服務都要有自己都一些配置信息,或者相同的配置信息,可能不同環境每個服務也有單獨的一套配置,這種情況配置文件數量比較龐大,維護起來相當費勁,舉個栗子:
在開發的過程中,一般數據庫是開發環境數據庫,所有服務DB的IP配置為:92.168.0.1,突然老大說,開發環境換了,DB的IP要修改,這下可不好受了,所有模塊挨個修改DB的配置,就問你難受不難受?
這個時候分布式配置中心就發揮了很大的優勢,只需要修改配置中心配置,所有服務即可自動生效,爽不爽!

Spring Cloud Config

官網地址:http://cloud.spring.io/spring-cloud-config/

簡介

Spring Cloud Config為服務端和客戶端提供了分布式系統的外部化配置支持。配置服務器為各應用的所有環境提供了一個中心化的外部配置。它實現了對服務端和客戶端對Spring EnvironmentPropertySource抽象的映射,所以它除了適用於Spring構建的應用程序,也可以在任何其他語言運行的應用程序中使用。作為一個應用可以通過部署管道來進行測試或者投入生產,我們可以分別為這些環境創建配置,並且在需要遷移環境的時候獲取對應環境的配置來運行。

置服務器默認采用git來存儲配置信息,這樣就有助於對環境配置進行版本管理,並且可以通過git客戶端工具來方便的管理和訪問配置內容。當然他也提供本地化文件系統的存儲方式。

使用 spring Cloud 進行集中式配置管理,將以往的配置文件從項目中摘除后放到Git 或svn中集中管理,並在需要變更的時候,可以通知到各應用程序,應用程序刷新配置不需要重啟。

實現原理

其實這個實現原理相對比較簡單一些,基於git的交互操作。

  1. 我們把配置文件存放到git上面
  2. Spring Cloud Config配置中心服務連接git
  3. 客戶端需要配置配置信息從配置中心服務獲取
  4. 當客戶端啟動,會從配置中心獲取git上面的配置信息

配置中心服務端

pom.xml添加依賴

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

<dependencyManagement>
    <dependencies>
        <!-- spring cloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Application啟動類添加注解

添加@EnableConfigServer注解,啟用配置中心:

package com.qianxunclub;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;


/**
* @author chihiro.zhang
*/
@SpringBootApplication
@EnableConfigServer
public class Application {

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

}

配置文件

application.yml或者application.properties添加配置信息:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/qianxunclub/spring-boot-config-repo
          default-label: master
          search-paths: /**
          basedir: target/config
  • spring.cloud.config.server.git.uri:配置git倉庫地址
  • spring.cloud.config.server.git.search-paths:倉庫文件夾目錄,如果是/**,就是所有目錄所有文件
  • spring.cloud.config.server.git.default-label:配置倉庫的分支
  • spring.cloud.config.server.git.basedir:配置文件拉去到本地的目錄位置

啟動測試

首先在git里面添加一個application-dev.yml配置文件,內容如此下:

test: 我是配置中心配置信息

已經配置完成了,啟動一波試試,看效果咋樣,正常情況下是可以正常啟動的,然后獲取配置文件試試
訪問地址:http://localhost:8888/test/dev
如果返回如下,就是成功了:

{
    "name":"test",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"64e7882a8f280641724e454a2db5a3da7b44d3d4",
    "state":null,
    "propertySources":[
        {
            "name":"https://gitee.com/qianxunclub/spring-boot-config-repo/application-dev.yml",
            "source":{
                "test":"配置中心的配置信息"
            }
        }
    ]
}

http請求地址和資源文件映射如下:

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

配置中心客戶端使用

pom.xml添加依賴

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

<dependencyManagement>
    <dependencies>
        <!-- spring cloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

配置文件

創建bootstrap.yml文件,切記,是bootstrap.yml文件bootstrap.yml文件,我就因為寫到了application.yml這個里面,各種出現問題啊,添加如下配置:

spring:
  cloud:
    config:
      name: application
      profile: dev
      label: master
      uri: http://localhost:8888/
  • spring.cloud.config.label:指明遠程倉庫的分支
  • spring.cloud.config.profile:指定不同環境配置文件,和git倉庫的 application-dev.yml對應
  • spring.cloud.config.name:配置名稱,一般和git倉庫的application-dev.yml對應
  • spring.cloud.config.uri:上面的配置中心服務地址

啟動測試

先添加一個獲取配置信息的類:



/**
 * @author chihiro.zhang
 */
@Configuration
@EnableAutoConfiguration
public class DemoConfiguration {

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

找個地方隨便調用一下,輸出這個test,就會打印上面git里面配置的信息了,爽不!

說說中間遇到的坑

  1. 服務端git配置死活獲取不了git倉庫配置文件
spring:
    cloud:
        config:
        server:
            git:
                uri: https://gitee.com/qianxunclub/spring-boot-config-repo
                default-label: master
                search-paths: /**
                basedir: target/config

當時這個uri配置的是公司的git倉庫,公司的git倉庫訪問是需要開代理才能有權限訪問的,代理也開了,可是一直報錯:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Jun 06 11:10:56 CST 2018
There was an unexpected error (type=Not Found, status=404).
Cannot clone or checkout repository: http://xxx.com:5080/framework/config-repo

很郁悶,不知道為啥,可是就在剛剛,就剛剛,寫博客的時候,有測試了一下,通了。。。。日了狗了,不知道啥原因,等研究出來了再來補充。

  1. 客戶端配置一定要配置在bootstrap.yml里面
    uri默認會調用端口為8888的地址http://localhost:8888/
    啟動的時候,會加載labeluri,profile配置,profile可以在啟動參數添加,profile也可以加在application.yml添加
    name也可以加在application.yml添加

demo

配置中心服務端:https://gitee.com/qianxunclub/qianxunclub-springboot-config
配置git倉庫:https://gitee.com/qianxunclub/qianxunclub-springboot-config
配置客戶端使用:https://gitee.com/qianxunclub/qianxunclub-starter-demo
客戶端主要配置在:https://gitee.com/qianxunclub/qianxunclub-starter-parent/tree/master/qianxunclub-starter-config


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM