SpringCloud配置客戶端ConfigClient接入服務(wù)端
一、大致介紹
1、有配置服務(wù)端,那么勢(shì)必就會(huì)有與之對(duì)應(yīng)的客戶端,SpringCloud 文檔中集成也非常簡(jiǎn)單;
2、但是這里有點(diǎn)需要注意,就是 bootstrap 配置文件,官方建議我們?cè)赽ootstrap中放置不更改的屬性,我們同樣也需要在這里做一些簡(jiǎn)單不易于改變的配置;
3、這里還順便列舉下配置路徑的規(guī)則:
/****************************************************************************************
* 配置服務(wù)的路勁規(guī)則:
*
* /{application}/{profile}[/{label}]
* /{application}-{profile}.yml
* /{label}/{application}-{profile}.yml
* /{application}-{profile}.properties
* /{label}/{application}-{profile}.properties
****************************************************************************************/
二、實(shí)現(xiàn)步驟
2.1 添加 maven 引用包
<?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> <artifactId>springms-config-client</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>com.springms.cloud</groupId> <artifactId>springms-spring-cloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <!-- 客戶端配置模塊 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- web模塊 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
2.2 添加應(yīng)用配置文件
(springms-config-clientsrcmainresourcesapplication.yml)
server: port: 8225 ##################################################################################################### # 測(cè)試一:配置服務(wù)客戶端Client應(yīng)用入口(正常測(cè)試 ConfigClient ) profile: profile-dev(local) ##################################################################################################### ##################################################################################################### # 測(cè)試二:配置服務(wù)客戶端Client應(yīng)用入口(鏈接 ClientServer 測(cè)試) #spring: # cloud: # config: # uri: http://localhost:8220 # profile: dev # label: master #當(dāng) ConfigServer 的后端存儲(chǔ)的是 Git 的時(shí)候,默認(rèn)就是 master # # application: # name: foobar #取 foobar-dev.yml 這個(gè)文件的 application 名字,即為 foobar 名稱 ##################################################################################################### ##################################################################################################### # 測(cè)試四:配置服務(wù)客戶端Client應(yīng)用入口(鏈接 ClientServer 測(cè)試,同時(shí)本地也有一份配置文件,那么該如何抉擇呢?) #profile: profile-local-dev #####################################################################################################
2.3 添加 bootstrap.yml 應(yīng)用配置文件
(springms-config-clientsrcmainresourcesbootstrap.yml)
測(cè)試三:配置服務(wù)客戶端Client應(yīng)用入口(鏈接 ClientServer 測(cè)試)
#spring: # cloud: # config: # uri: http://localhost:8220 # profile: dev # label: master #當(dāng) ConfigServer 的后端存儲(chǔ)的是 Git 的時(shí)候,默認(rèn)就是 master # # application: # name: foobar #取 foobar-dev.yml 這個(gè)文件的 application 名字,即為 foobar 名稱
2.4 添加Web控制層類(lèi)
(springms-config-clientsrcmainjavacomspringmscloudcontrollerConfigClientController.java)
package com.springms.cloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * 配置客戶端Controller。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/10/15 * */ @RestController public class ConfigClientController { @Value("${profile}") private String profile; @GetMapping("/profile") public String getProfile(){ return this.profile; } }
2.5 添加應(yīng)用啟動(dòng)類(lèi)
(springms-config-clientsrcmainjavacomspringmscloudMsConfigClientApplication.java)
package com.springms.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 配置客戶端ConfigClient接入配置服務(wù)端。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/10/15 * */ @SpringBootApplication public class MsConfigClientApplication { public static void main(String[] args) { SpringApplication.run(MsConfigClientApplication.class, args); System.out.println("【【【【【【 ConfigClient微服務(wù) 】】】】】】已啟動(dòng)."); } }
三、測(cè)試
一、配置客戶端ConfigClient接入配置服務(wù)端(正常測(cè)試 ConfigClient ):
- 1、注解:pom.xml 先刪除 configclient 的引用模塊,以便測(cè)試正常情況 ConfigClientController 接口是否暢通;
- 2、編輯 application.yml 文件,注意添加 profile: profile-dev(local) 屬性;
- 3、啟動(dòng) springms-config-client 模塊服務(wù),啟動(dòng)1個(gè)端口; 4、在瀏覽器輸入地址 http://localhost:8225/profile 正常情況下會(huì)輸出配置文件的內(nèi)容(內(nèi)容為:profile-dev(local));
注意:這里還暫時(shí)不需要 bootstrap.yml 配置文件,所以測(cè)試一是不需要添加 bootstrap.yml 文件的;
二、配置客戶端ConfigClient接入配置服務(wù)端(鏈接 ClientServer 測(cè)試遇到挫折):
- 1、注解:pom.xml 先添加 configclient 的引用模;
- 2、編輯 application.yml 文件,注意注釋 profile 屬性,然后添加相關(guān)客戶端配置;
spring: cloud: config: uri: http://localhost:8220 profile: dev label: master #當(dāng) ConfigServer 的后端存儲(chǔ)的是 Git 的時(shí)候,默認(rèn)就是 master application: name: foobar #取 foobar-dev.yml 這個(gè)文件的 application 名字,即為 foobar 名稱
- 3、啟動(dòng) springms-config-server 模塊服務(wù),啟動(dòng)1個(gè)端口;
- 4、啟動(dòng) springms-config-client 模塊服務(wù),啟動(dòng)1個(gè)端口;
- 5、然后發(fā)現(xiàn)啟動(dòng) springms-config-client 模塊出現(xiàn)錯(cuò)誤,報(bào)錯(cuò)信息為:
Fetching config from server at: http://localhost:8888, Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/foobar/dev/master": Connection refused;
- 6、發(fā)現(xiàn)錯(cuò)誤信息中,為什么鏈接的是遠(yuǎn)端的 8888 端口呢?百思不得其解,難道是默認(rèn)加載的配置 8888 端口???
- 7、SpringCloud里面有個(gè)“啟動(dòng)上下文”,主要是用于加載遠(yuǎn)端的配置,也就是加載ConfigServer里面的配置,默認(rèn)加載順序?yàn)椋杭虞dbootstrap.*里面的配置 --> 鏈接configserver,加載遠(yuǎn)程配置 --> 加載application.*里面的配置;
總結(jié):這里需要借助于“啟動(dòng)上下文”來(lái)處理加載遠(yuǎn)程配置,請(qǐng)看下面環(huán)節(jié)測(cè)試三。
三、配置客戶端ConfigClient接入配置服務(wù)端(鏈接 ClientServer 測(cè)試遇到挫折):
- 1、注解:pom.xml 先添加 configclient 的引用模;
- 2、編輯 application.yml 文件,注釋'測(cè)試二'的屬性配置;
- 3、新建一個(gè) bootstrap.yml 文件,將相關(guān)客戶端配置挪到 bootstrap.yml 文件即可;
- 4、啟動(dòng) springms-config-server 模塊服務(wù),啟動(dòng)1個(gè)端口;
- 5、啟動(dòng) springms-config-client 模塊服務(wù),啟動(dòng)1個(gè)端口;
- 6、在瀏覽器輸入地址 http://localhost:8225/profile 正常情況下會(huì)輸出配置文件的內(nèi)容(內(nèi)容為:profile-dev);
總結(jié):這里成功獲取了遠(yuǎn)端配置,并成功打印了屬性值出來(lái),說(shuō)明添加 bootstrap.yml 配置文件對(duì)我們項(xiàng)目的順利進(jìn)行起到了有效的作用。
四、配置客戶端ConfigClient接入配置服務(wù)端(鏈接 ClientServer 測(cè)試,同時(shí)本地也有一份配置文件,那么該如何抉擇呢?):
- 1、在測(cè)試三的基礎(chǔ)上,咱們?cè)僮鳇c(diǎn)其它配置測(cè)試;
- 2、在 application.yml 文件,再次添加 profile 屬性,看看加載的是本地配置還是遠(yuǎn)端配置?
- 3、停止并重新啟動(dòng) springms-config-client 模塊服務(wù),啟動(dòng)1個(gè)端口;
- 4、在瀏覽器輸入地址 http://localhost:8225/profile 正常情況下會(huì)輸出遠(yuǎn)端服務(wù)的配置內(nèi)容;
總結(jié):在ConfigServer服務(wù)啟動(dòng)的時(shí)候,bootstrap 拿到遠(yuǎn)端配置注入到 profile 的屬性中的話,那么就不會(huì)再次覆蓋這個(gè)屬性了,所以只會(huì)選擇遠(yuǎn)端配置的內(nèi)容。
那是不是會(huì)有人認(rèn)為把ConfigServer再重啟一下就行了呢?答案是不行的,因?yàn)槭走x的是遠(yuǎn)端配置內(nèi)容;
下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
以上就是SpringCloud配置客戶端ConfigClient接入服務(wù)端的詳細(xì)內(nèi)容,更多關(guān)于SpringCloud ConfigClient接入配置的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringCloud之Admin服務(wù)監(jiān)控實(shí)現(xiàn)流程示例詳解
- SpringCloud使用AOP統(tǒng)一處理Web請(qǐng)求日志實(shí)現(xiàn)步驟
- SpringCloud配置服務(wù)端的ConfigServer設(shè)置安全認(rèn)證
- SpringCloud聲明式Feign客戶端調(diào)用工具使用
- spring?cloud?gateway中netty線程池小優(yōu)化
- Spring?Cloud?Gateway中netty線程池優(yōu)化示例詳解
- Spring Cloud 2023 新特性支持同步網(wǎng)關(guān)
相關(guān)文章
java讀取配置文件(properties)的時(shí)候,unicode碼轉(zhuǎn)utf-8方式
這篇文章主要介紹了java讀取配置文件(properties)的時(shí)候,unicode碼轉(zhuǎn)utf-8方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02mybatis分割字符串并循環(huán),實(shí)現(xiàn)in多個(gè)參數(shù)的操作
這篇文章主要介紹了mybatis分割字符串并循環(huán),實(shí)現(xiàn)in多個(gè)參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Java強(qiáng)制類(lèi)型轉(zhuǎn)換的所有規(guī)則及說(shuō)明
這篇文章主要介紹了Java強(qiáng)制類(lèi)型轉(zhuǎn)換的所有規(guī)則及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件 FTP上傳下載隊(duì)列窗口實(shí)現(xiàn)(7)
這篇文章主要為大家詳細(xì)介紹了Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單FTP軟件,F(xiàn)TP上傳下載隊(duì)列窗口的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04java設(shè)置session過(guò)期時(shí)間的實(shí)現(xiàn)方法
這篇文章主要介紹了java設(shè)置session過(guò)期時(shí)間的實(shí)現(xiàn)方法,以實(shí)例形式詳細(xì)講述了具體實(shí)現(xiàn)過(guò)程,非常具有參考借鑒價(jià)值,需要的朋友可以參考下2014-10-10mybatis錯(cuò)誤之in查詢?<foreach>循環(huán)問(wèn)題
這篇文章主要介紹了mybatis錯(cuò)誤之in查詢?<foreach>循環(huán)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01透明化Sharding-JDBC數(shù)據(jù)庫(kù)字段加解密方案
這篇文章主要為大家介紹了透明化Sharding-JDBC數(shù)據(jù)庫(kù)字段加解密方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-02-02