Spring Cloud Config 基礎(chǔ)示例

Spring Cloud Config 簡(jiǎn)介

什么是Srping Cloud Config?

  • Spring Cloud Config 是一種分布式配置中心框架敲茄, 為分布式系統(tǒng)中的外部化配置提供服務(wù)器和客戶(hù)端支持。(同類(lèi)技術(shù)還有vault讨勤,zookeeper订框,Consul)
  • 使用Config Server析苫,可以在所有環(huán)境中管理應(yīng)用程序的外部屬性〈┌猓客戶(hù)端和服務(wù)器上的概念映射與Spring Environment和PropertySource抽象衩侥,因此它們非常適合Spring應(yīng)用程序,但可以與任何語(yǔ)言運(yùn)行的任何應(yīng)用程序一起使用矛物。當(dāng)應(yīng)用程序通過(guò)部署管道從開(kāi)發(fā)到測(cè)試并進(jìn)入生產(chǎn)時(shí)茫死,您可以管理這些環(huán)境之間的配置,并確保應(yīng)用程序具有遷移時(shí)需要運(yùn)行的所有內(nèi)容履羞。服務(wù)器存儲(chǔ)后端的默認(rèn)實(shí)現(xiàn)使用git峦萎,因此它可以輕松支持配置環(huán)境的標(biāo)記版本屡久,以及可用于管理內(nèi)容的各種工具。
  • Spring Cloud Config也主要由兩部分組成:
  • Config-Server: 用于配置外部的資源文件,支持對(duì)屬性值進(jìn)行加解密
  • Config-client:綁定到config server使用遠(yuǎn)程配置文件初始化spring爱榔,支持對(duì)屬性值進(jìn)行加解密

本文示例說(shuō)明

  • 為了更直觀(guān)的理解spring cloud config涂身,本文通過(guò)server獲取整個(gè)配置,效果上與通過(guò)配置springboot的active 來(lái)切換環(huán)境一致搓蚪;通過(guò)接口查詢(xún)數(shù)據(jù)庫(kù)以查看效果蛤售;
  • 采用高可用架構(gòu);此處使用eureka去做服務(wù)注冊(cè)發(fā)現(xiàn)(暫時(shí)也只會(huì)這個(gè)妒潭。悴能。),eureka配置說(shuō)明可以查看springCloud之eureka

新建ConfigServer

為了減少文章長(zhǎng)度雳灾,盡量干貨漠酿,創(chuàng)建過(guò)程就不截說(shuō)明了,建議使用Intellij idea進(jìn)行創(chuàng)建谎亩;

  • 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.lc.springcloud</groupId>
 <artifactId>config-server</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>config-server</name>
 <description>Demo project for Spring Boot</description>

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

 <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     <java.version>1.8</java.version>
     <spring-cloud.version>Greenwich.M2</spring-cloud.version>
 </properties>

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

     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </dependency>

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

 <dependencyManagement>
     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-dependencies</artifactId>
             <version>${spring-cloud.version}</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </dependencies>
 </dependencyManagement>

 <build>
     <plugins>
         <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
     </plugins>
 </build>

 <repositories>
     <repository>
         <id>spring-milestones</id>
         <name>Spring Milestones</name>
         <url>https://repo.spring.io/milestone</url>
         <snapshots>
             <enabled>false</enabled>
         </snapshots>
     </repository>
 </repositories>
</project>
  • application.yml配置如
server:
    port: 8100
spring:
    application:
        name: config-server
    cloud:
        config:
            server:
                git:
                 uri: https://github.com/lvchaogit/SpringCloud
eureka:
   client:
    service-url:
      defaultZone: http://localhost:8081/eureka/ # 服務(wù)注冊(cè)中心地址

通過(guò)配置不難理解炒嘲,該配置是從git上獲取配置,更多配置后續(xù)詳解匈庭;

  • application.java
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {

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

主要是加上@EnableConfigServer注解夫凸,開(kāi)啟configserver功能

搭建 Config Client

  • 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.lc.springcloud</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-client</name>
    <description>Demo project for Spring Boot</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.M2</spring-cloud.version>
    </properties>

    <dependencies>
        <!-- config starter begin-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!-- config starter end-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!-- mybatis-plus begin -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>2.1.9</version>
            <exclusions>
                <exclusion>
                    <artifactId>tomcat-jdbc</artifactId>
                    <groupId>org.apache.tomcat</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--config監(jiān)控模塊  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.13</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>
  • bootstrap.yml
spring:
  application:
    name: config-client
  cloud:
    config:
      profile: dev
      label: master
      discovery:
        enabled: true
        service-id: config-server
server:
  port: 2001
eureka:
  client:
      register-with-eureka: false     #因此處只是消費(fèi),不提供服務(wù)阱持,所以不需要向eureka server注冊(cè)
      service-url:
        defaultZone: http://localhost:8081/eureka/ # 服務(wù)注冊(cè)中心地址
  • 首先注意配置文件名稱(chēng)為:bootstartp.yml夭拌,并不是application.yml
  • 通過(guò)配置discovery,并設(shè)置enabled為true衷咽,使client通過(guò)服務(wù)發(fā)現(xiàn)去獲取server鸽扁,server-id為注冊(cè)中心里配置的服務(wù)名稱(chēng)
  • label=git的標(biāo)簽;profile=配置文件版本(類(lèi)似于spring boot中的active)

常用configserver配置

采用URI占位符

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/lvchaogit/{application}
  • 使用{application} 和 {profile}(如果使用{label}镶骗,請(qǐng)記住它是使用在git標(biāo)簽中的)桶现。因此你可以輕松的支持“一個(gè)應(yīng)用一個(gè)倉(cāng)庫(kù)”的原則

模式匹配和多倉(cāng)庫(kù)

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/lvchaogit/SpringCloud
          repos:
            simple: https://github.com/simple/config-repo
            special:
              pattern: special*/dev*,*special*/dev*
              uri: https://github.com/special/config-repo
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo
  • 在{application}和{profile}參數(shù)中使用模式匹配可以支持更多復(fù)雜的需求。模式的格式是一組逗號(hào)分隔的{application}/{profile}鼎姊,其中的參數(shù)可以使用通配符.
  • 如果{application}/{profile}沒(méi)有匹配到任何模式骡和,它將使用默認(rèn)的倉(cāng)庫(kù)地址:spring.cloud.config.server.git.uri。上面的例子中此蜈,"simple"倉(cāng)庫(kù)匹配的是“simple/”(它僅僅匹配一個(gè)倉(cāng)庫(kù)simple即横,在所有的環(huán)境下)。"local"倉(cāng)庫(kù)將匹配所有{application}的名字以“l(fā)ocal”開(kāi)頭的裆赵,并且也是在所有的環(huán)境下东囚。“/”前綴自動(dòng)添加到所有沒(méi)有設(shè)置{profile}的模式中战授。

匹配倉(cāng)庫(kù)子目錄

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          searchPaths: foo,bar*
  • 在foo和以bar開(kāi)頭的目錄中页藻,搜索配置文件桨嫁。

克隆遠(yuǎn)程的倉(cāng)庫(kù)

spring:
  cloud:
    config:
      server:
        git:
          uri: https://git/common/config-repo.git
          repos:
            team-a:
                pattern: team-a-*
                cloneOnStart: true
                uri: http://git/team-a/config-repo.git
            team-b:
                pattern: team-b-*
                cloneOnStart: false
                uri: http://git/team-b/config-repo.git
            team-c:
                pattern: team-c-*
                uri: http://git/team-a/config-repo.git
  • 服務(wù)器默認(rèn)在第一次請(qǐng)求配置文件時(shí)克隆遠(yuǎn)程的倉(cāng)庫(kù),也可以配置在啟動(dòng)的時(shí)候克隆倉(cāng)庫(kù)
  • team-a的倉(cāng)庫(kù)將在服務(wù)端啟動(dòng)時(shí)進(jìn)行克隆份帐,其他的倉(cāng)庫(kù)將在第一次請(qǐng)求時(shí)克隆璃吧。

倉(cāng)庫(kù)認(rèn)證

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          username: trolley
          password: strongpassword
  • 輸入用戶(hù)名密碼

常用配置參考:configServer常用配置

文中示例代碼:SpringCloudConfig 示例

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市废境,隨后出現(xiàn)的幾起案子畜挨,更是在濱河造成了極大的恐慌,老刑警劉巖噩凹,帶你破解...
    沈念sama閱讀 222,807評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件巴元,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡驮宴,警方通過(guò)查閱死者的電腦和手機(jī)逮刨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)堵泽,“玉大人修己,你說(shuō)我怎么就攤上這事∮蓿” “怎么了睬愤?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,589評(píng)論 0 363
  • 文/不壞的土叔 我叫張陵,是天一觀(guān)的道長(zhǎng)佳谦。 經(jīng)常有香客問(wèn)我戴涝,道長(zhǎng),這世上最難降的妖魔是什么钻蔑? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,188評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮奸鸯,結(jié)果婚禮上咪笑,老公的妹妹穿的比我還像新娘。我一直安慰自己娄涩,他們只是感情好窗怒,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,185評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著蓄拣,像睡著了一般扬虚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上球恤,一...
    開(kāi)封第一講書(shū)人閱讀 52,785評(píng)論 1 314
  • 那天辜昵,我揣著相機(jī)與錄音,去河邊找鬼咽斧。 笑死堪置,一個(gè)胖子當(dāng)著我的面吹牛躬存,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播舀锨,決...
    沈念sama閱讀 41,220評(píng)論 3 423
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼岭洲,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了坎匿?” 一聲冷哼從身側(cè)響起盾剩,我...
    開(kāi)封第一講書(shū)人閱讀 40,167評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎替蔬,沒(méi)想到半個(gè)月后告私,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,698評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡进栽,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,767評(píng)論 3 343
  • 正文 我和宋清朗相戀三年德挣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片快毛。...
    茶點(diǎn)故事閱讀 40,912評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡格嗅,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出唠帝,到底是詐尸還是另有隱情屯掖,我是刑警寧澤,帶...
    沈念sama閱讀 36,572評(píng)論 5 351
  • 正文 年R本政府宣布襟衰,位于F島的核電站贴铜,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏瀑晒。R本人自食惡果不足惜绍坝,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,254評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望苔悦。 院中可真熱鬧轩褐,春花似錦、人聲如沸玖详。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,746評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蟋座。三九已至拗踢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間向臀,已是汗流浹背巢墅。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,859評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人砂缩。 一個(gè)月前我還...
    沈念sama閱讀 49,359評(píng)論 3 379
  • 正文 我出身青樓作谚,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親庵芭。 傳聞我的和親對(duì)象是個(gè)殘疾皇子妹懒,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,922評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容