帶你入門SpringCloud統(tǒng)一配置 | SpringCloud Config

前言

在微服務(wù)中眾多服務(wù)的配置必然會出現(xiàn)相同的配置,如果配置發(fā)生變化需要修改,一個(gè)個(gè)去修改然后重啟項(xiàng)目的方案是絕對不可取的羹蚣。而 SpringCloud Config 就是一個(gè)可以幫助你實(shí)現(xiàn)統(tǒng)一配置選擇之一袁翁。

如果你不懂 SpringCloud Config 環(huán)境搭建,那么該篇博客將會幫助到你秒拔,文中通過具體操作帶你了解 SpringCloud Config 環(huán)境搭建的入門操作乓土。

閱讀本文需要你熟悉 SpringBoot 項(xiàng)目的基本使用即可,還有一點(diǎn)需要注意的是在操作過程中盡量和我本地環(huán)境一致溯警,因?yàn)榄h(huán)境不一致可能會帶來一些問題趣苏。我本地環(huán)境如下:

  • SpringBoot Version: 2.1.0.RELEASE
  • SpringCloud Version: Greenwich.RELEASE
  • Apache Maven Version: 3.6.0
  • Java Version: 1.8.0_144
  • IDEA:Spring Tools Suite (STS)

接下來就開始 SpringCloud Config 環(huán)境搭建操作介紹!

搭建 SpringCloud Config 環(huán)境

SpringCloud Config 環(huán)境搭建最小環(huán)境需要 3個(gè) SpringCloud 項(xiàng)目:一臺 Eureka Server 端梯轻、一臺 Config Server 端(也是Eureka Client 端)食磕、一臺普通服務(wù)端(即是 Config Client 也是 Eureka Client)。

關(guān)于Eureka環(huán)境的搭建請參考我的另一篇博客 帶你入門SpringCloud服務(wù)發(fā)現(xiàn) | Eurka搭建和使用

Config Server 端搭建

在 SpringBoot 項(xiàng)目中引入 spring-cloud-config-server 和 spring-cloud-starter-netflix-eureka-client 依賴喳挑,具體代碼如下:

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

在 SpringBoot Application 上聲明 @EnableDiscoveryClient 和 @EnableConfigServer彬伦,具體代碼如下:

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class SpringCloudConfigServerApplication {

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

}

在 GitHub上創(chuàng)建私有倉庫


在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

然后添加配置信息到 GitHub 上。


在這里插入圖片描述

在這里插入圖片描述

product.properties 配置可以添加一些公共的配置他會覆蓋到 product-dev.properties上

在 application.properties 添加配置信息

spring.application.name=CONFIGSERVER
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

spring.cloud.config.server.git.uri=https://github.com/zhuoqianmingyue/config-repo
spring.cloud.config.server.git.username=github帳號
spring.cloud.config.server.git.password=github帳號密碼
spring.cloud.config.server.git.basedir=config-repo/config-repo
  • spring.application.name:服務(wù)的名稱
  • eureka.client.service-url.defaultZone:Eureka Server 地址
  • spring.cloud.config.server.git.uri:配置GitHub 私有倉庫 HTTP 克隆地址
  • spring.cloud.config.server.git.username:配置你的 github帳號
  • spring.cloud.config.server.git.password:配置你的github帳號密碼
  • spring.cloud.config.server.git.basedir:克隆配置文件存儲地址

Config Server 端高可用只需要配置多臺Config Server注冊到Eureka 服務(wù)端即可伊诵。

測試

第一步啟動(dòng) Eurka Server 端(具體代碼請從我的GitHub項(xiàng)目獲取单绑,GitHub地址下面有介紹)

將 spring-cloud-config-eureka-service 進(jìn)行打包,通過 mvn clean package -Dmaven.test.skip=true曹宴。

在這里插入圖片描述

進(jìn)入 target 目錄 通過 java -jar 方式啟動(dòng)搂橙。
在這里插入圖片描述

第二步啟動(dòng) Config Server 端
在這里插入圖片描述

第三步最后訪問 Eurka Server 端,如下圖所示:

CONFIGSERVER 已經(jīng)注冊到 Eurka Server 服務(wù)端笛坦。

在這里插入圖片描述

訪問 Config Server 端獲取配置信息区转,具體訪問地址:http://localhost:8080/product-dev.properties苔巨。訪問結(jié)果如下圖所示:
在這里插入圖片描述

到這里 Config Server 端搭建介紹完畢!接下來開始 Config Client 端搭建介紹废离。

Config Client 端搭建(商品服務(wù))

在商品服務(wù) SpringBoot 項(xiàng)目中引入 spring-cloud-config-client 和 spring-cloud-starter-netflix-eureka-client 依賴侄泽。具體代碼如下:

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

在 SpringBoot Application上聲明 @EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudConfigProductServiceApplication {

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

}

在 src\main\resources 目錄下創(chuàng)建 bootstrap.properties, 具體代碼如下:

spring.cloud.config.discovery.service-id=CONFIGSERVER
spring.cloud.config.discovery.enabled=true
spring.cloud.config.profile=dev
spring.application.name=product

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
  • spring.cloud.config.discovery.service-id:Config Server 服務(wù)名
  • spring.cloud.config.discovery.enabled:是否開啟配置發(fā)現(xiàn)
  • spring.cloud.config.profile:啟用那個(gè)后綴的配置文件
  • eureka.client.service-url.defaultZone:Eureka Server 地址
  • spring.application.name:服務(wù)的名稱

連接接Config Server配置 spring.cloud.config.xx 和 eureka Server 端配置eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ 一定要配置到bootstrap.properties中蜻韭,否則根本獲取不到 Config Server 的配置信息悼尾。

測試

第一步啟動(dòng) Eurka Server端 和 Config Server 端。
第二步啟動(dòng) Config Client (商品服務(wù)) 端
第三步訪問 Eurka Server 端肖方,如下圖所示:

PRODUCT 已經(jīng)注冊到 Eurka Server 服務(wù)端诀豁。


在這里插入圖片描述

第四步:編寫獲取 Config Server 上配置信息的 Controller,具體代碼如下:

獲取的就是紅色框的 env 配置項(xiàng)的值窥妇。


在這里插入圖片描述
@RestController
public class EvnController {
    @Value("${env}")
    private String env;
    
    @RequestMapping("/env")
    public String evn() {
        return this.env;
    }
}

游覽器訪問 localhost:8763/product/env 進(jìn)行測試舷胜,具體結(jié)果如下:


在這里插入圖片描述

小結(jié)

SpringCloud Config 執(zhí)行流程是通用的配置添加配置倉庫中(默認(rèn)使用Git),在由 Config Server 讀取配置倉庫配置并對外提供接口活翩。其他服務(wù)(Config Client)可以通過調(diào)用Config Server 提供接口來獲取配置信息烹骨。

搭建過程也并不復(fù)雜還是SpringCloud 添加starter 依賴、添加EnableXX 注解材泄、最后在添加相關(guān)配置即可沮焕。沒有操作的最好操作一篇哈!

代碼示例

如果你按照上述方式搭建并未成功拉宗,可以參考我在GitHub 項(xiàng)目 spring-cloud-get-started 倉庫中模塊名為:

  • spring-cloud-config-eureka-service
  • spring-cloud-config-server
  • spring-cloud-config-product-service

進(jìn)行對比查看是否配置有誤峦树。

spring-cloud-get-started 項(xiàng)目地址:https://github.com/zhuoqianmingyue/spring-cloud-get-started

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市旦事,隨后出現(xiàn)的幾起案子魁巩,更是在濱河造成了極大的恐慌,老刑警劉巖姐浮,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谷遂,死亡現(xiàn)場離奇詭異,居然都是意外死亡卖鲤,警方通過查閱死者的電腦和手機(jī)肾扰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蛋逾,“玉大人集晚,你說我怎么就攤上這事∏唬” “怎么了偷拔?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我条摸,道長,這世上最難降的妖魔是什么铸屉? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任钉蒲,我火速辦了婚禮,結(jié)果婚禮上彻坛,老公的妹妹穿的比我還像新娘顷啼。我一直安慰自己,他們只是感情好昌屉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布涂圆。 她就那樣靜靜地躺著湃累,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上锚赤,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天,我揣著相機(jī)與錄音调违,去河邊找鬼汽烦。 笑死,一個(gè)胖子當(dāng)著我的面吹牛屹篓,可吹牛的內(nèi)容都是我干的疙渣。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼堆巧,長吁一口氣:“原來是場噩夢啊……” “哼妄荔!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起谍肤,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤啦租,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后荒揣,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體刷钢,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年乳附,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了内地。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,711評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡赋除,死狀恐怖阱缓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情举农,我是刑警寧澤荆针,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響航背,放射性物質(zhì)發(fā)生泄漏喉悴。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一玖媚、第九天 我趴在偏房一處隱蔽的房頂上張望箕肃。 院中可真熱鬧,春花似錦今魔、人聲如沸勺像。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽吟宦。三九已至,卻和暖如春涩维,著一層夾襖步出監(jiān)牢的瞬間殃姓,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工瓦阐, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留辰狡,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓垄分,卻偏偏與公主長得像宛篇,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子薄湿,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評論 2 353

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