SpringCloud配置客戶端ConfigClient接入服務端
一、大致介紹
1、有配置服務端,那么勢必就會有與之對應的客戶端,SpringCloud 文檔中集成也非常簡單;
2、但是這里有點需要注意,就是 bootstrap 配置文件,官方建議我們在bootstrap中放置不更改的屬性,我們同樣也需要在這里做一些簡單不易于改變的配置;
3、這里還順便列舉下配置路徑的規(guī)則:
/****************************************************************************************
* 配置服務的路勁規(guī)則:
*
* /{application}/{profile}[/{label}]
* /{application}-{profile}.yml
* /{label}/{application}-{profile}.yml
* /{application}-{profile}.properties
* /{label}/{application}-{profile}.properties
****************************************************************************************/
二、實現(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 添加應用配置文件
(springms-config-clientsrcmainresourcesapplication.yml)
server: port: 8225 ##################################################################################################### # 測試一:配置服務客戶端Client應用入口(正常測試 ConfigClient ) profile: profile-dev(local) ##################################################################################################### ##################################################################################################### # 測試二:配置服務客戶端Client應用入口(鏈接 ClientServer 測試) #spring: # cloud: # config: # uri: http://localhost:8220 # profile: dev # label: master #當 ConfigServer 的后端存儲的是 Git 的時候,默認就是 master # # application: # name: foobar #取 foobar-dev.yml 這個文件的 application 名字,即為 foobar 名稱 ##################################################################################################### ##################################################################################################### # 測試四:配置服務客戶端Client應用入口(鏈接 ClientServer 測試,同時本地也有一份配置文件,那么該如何抉擇呢?) #profile: profile-local-dev #####################################################################################################
2.3 添加 bootstrap.yml 應用配置文件
(springms-config-clientsrcmainresourcesbootstrap.yml)
測試三:配置服務客戶端Client應用入口(鏈接 ClientServer 測試)
#spring: # cloud: # config: # uri: http://localhost:8220 # profile: dev # label: master #當 ConfigServer 的后端存儲的是 Git 的時候,默認就是 master # # application: # name: foobar #取 foobar-dev.yml 這個文件的 application 名字,即為 foobar 名稱
2.4 添加Web控制層類
(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 添加應用啟動類
(springms-config-clientsrcmainjavacomspringmscloudMsConfigClientApplication.java)
package com.springms.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 配置客戶端ConfigClient接入配置服務端。 * * @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微服務 】】】】】】已啟動."); } }
三、測試
一、配置客戶端ConfigClient接入配置服務端(正常測試 ConfigClient ):
- 1、注解:pom.xml 先刪除 configclient 的引用模塊,以便測試正常情況 ConfigClientController 接口是否暢通;
- 2、編輯 application.yml 文件,注意添加 profile: profile-dev(local) 屬性;
- 3、啟動 springms-config-client 模塊服務,啟動1個端口; 4、在瀏覽器輸入地址 http://localhost:8225/profile 正常情況下會輸出配置文件的內(nèi)容(內(nèi)容為:profile-dev(local));
注意:這里還暫時不需要 bootstrap.yml 配置文件,所以測試一是不需要添加 bootstrap.yml 文件的;
二、配置客戶端ConfigClient接入配置服務端(鏈接 ClientServer 測試遇到挫折):
- 1、注解:pom.xml 先添加 configclient 的引用模;
- 2、編輯 application.yml 文件,注意注釋 profile 屬性,然后添加相關客戶端配置;
spring: cloud: config: uri: http://localhost:8220 profile: dev label: master #當 ConfigServer 的后端存儲的是 Git 的時候,默認就是 master application: name: foobar #取 foobar-dev.yml 這個文件的 application 名字,即為 foobar 名稱
- 3、啟動 springms-config-server 模塊服務,啟動1個端口;
- 4、啟動 springms-config-client 模塊服務,啟動1個端口;
- 5、然后發(fā)現(xiàn)啟動 springms-config-client 模塊出現(xiàn)錯誤,報錯信息為:
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)錯誤信息中,為什么鏈接的是遠端的 8888 端口呢?百思不得其解,難道是默認加載的配置 8888 端口???
- 7、SpringCloud里面有個“啟動上下文”,主要是用于加載遠端的配置,也就是加載ConfigServer里面的配置,默認加載順序為:加載bootstrap.*里面的配置 --> 鏈接configserver,加載遠程配置 --> 加載application.*里面的配置;
總結:這里需要借助于“啟動上下文”來處理加載遠程配置,請看下面環(huán)節(jié)測試三。
三、配置客戶端ConfigClient接入配置服務端(鏈接 ClientServer 測試遇到挫折):
- 1、注解:pom.xml 先添加 configclient 的引用模;
- 2、編輯 application.yml 文件,注釋'測試二'的屬性配置;
- 3、新建一個 bootstrap.yml 文件,將相關客戶端配置挪到 bootstrap.yml 文件即可;
- 4、啟動 springms-config-server 模塊服務,啟動1個端口;
- 5、啟動 springms-config-client 模塊服務,啟動1個端口;
- 6、在瀏覽器輸入地址 http://localhost:8225/profile 正常情況下會輸出配置文件的內(nèi)容(內(nèi)容為:profile-dev);
總結:這里成功獲取了遠端配置,并成功打印了屬性值出來,說明添加 bootstrap.yml 配置文件對我們項目的順利進行起到了有效的作用。
四、配置客戶端ConfigClient接入配置服務端(鏈接 ClientServer 測試,同時本地也有一份配置文件,那么該如何抉擇呢?):
- 1、在測試三的基礎上,咱們再做點其它配置測試;
- 2、在 application.yml 文件,再次添加 profile 屬性,看看加載的是本地配置還是遠端配置?
- 3、停止并重新啟動 springms-config-client 模塊服務,啟動1個端口;
- 4、在瀏覽器輸入地址 http://localhost:8225/profile 正常情況下會輸出遠端服務的配置內(nèi)容;
總結:在ConfigServer服務啟動的時候,bootstrap 拿到遠端配置注入到 profile 的屬性中的話,那么就不會再次覆蓋這個屬性了,所以只會選擇遠端配置的內(nèi)容。
那是不是會有人認為把ConfigServer再重啟一下就行了呢?答案是不行的,因為首選的是遠端配置內(nèi)容;
下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
以上就是SpringCloud配置客戶端ConfigClient接入服務端的詳細內(nèi)容,更多關于SpringCloud ConfigClient接入配置的資料請關注腳本之家其它相關文章!
相關文章
java讀取配置文件(properties)的時候,unicode碼轉utf-8方式
這篇文章主要介紹了java讀取配置文件(properties)的時候,unicode碼轉utf-8方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02mybatis分割字符串并循環(huán),實現(xiàn)in多個參數(shù)的操作
這篇文章主要介紹了mybatis分割字符串并循環(huán),實現(xiàn)in多個參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Java語言實現(xiàn)簡單FTP軟件 FTP上傳下載隊列窗口實現(xiàn)(7)
這篇文章主要為大家詳細介紹了Java語言實現(xiàn)簡單FTP軟件,F(xiàn)TP上傳下載隊列窗口的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04mybatis錯誤之in查詢?<foreach>循環(huán)問題
這篇文章主要介紹了mybatis錯誤之in查詢?<foreach>循環(huán)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01透明化Sharding-JDBC數(shù)據(jù)庫字段加解密方案
這篇文章主要為大家介紹了透明化Sharding-JDBC數(shù)據(jù)庫字段加解密方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-02-02