解決SpringCloud Config結(jié)合github無法讀取配置的問題
前言
配置中心存放文件在github是讀取過程,可能你會(huì)出現(xiàn)讀取不到配置信息。本次筆者將這一過程進(jìn)行詳細(xì)介紹。
準(zhǔn)備
父工程
由于筆者是使用聚合工程,所以這次也是把相關(guān)的工程創(chuàng)建說明寫上。當(dāng)然你也可以完全創(chuàng)建兩個(gè)獨(dú)立的工程來引用。
創(chuàng)建父工程時(shí)直接只有一個(gè)pom文件,以下是這個(gè)文件的依賴信息
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <packaging>pom</packaging> <modules> <module>ch2-2-eureka-client</module> <module>ch2-3-config-server</module> <module>ch4-1-feign</module> <module>ch5-1-zuul</module> <module>config-client</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <groupId>com.example</groupId> <artifactId>ch2-1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ch2-1</name> <description>eureka</description> <properties> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
子工程-config-server
這個(gè)工程內(nèi)容目錄如下圖
依賴
由于在父工程已經(jīng)引入了WEB,所以這里只引入spring-cloud-config-server,另外一個(gè)spring-boot-starter-actuator主要是用來查看端點(diǎn)信息的,可以不引用,后續(xù)手機(jī)刷新時(shí)需要這個(gè)開啟相關(guān)的訪問端點(diǎn)(好像是,具體后續(xù)可能有相關(guān)文章再細(xì)說)
<?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"> <parent> <artifactId>ch2-1</artifactId> <groupId>com.example</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>ch2-3-config-server</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
啟動(dòng)主類
package springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication //只加這個(gè)即可,表示這個(gè)啟動(dòng)的是config的服務(wù)中心 @EnableConfigServer public class Ch21ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(Ch21ConfigServerApplication.class, args); } }
配置文件
服務(wù)端的配置不需要注意什么的,按下面的配置即可,因?yàn)槟J(rèn)是git的,所以不需要寫profiles 指向git了
server: port: 9090 spring: application: name: config-server cloud: config: server: git: uri: https://github.com/xxx1/xxx2.git #xxx2 是指存放當(dāng)前配置文件的倉庫名 username: 寫自己的github賬號(hào) password: 寫自己的github密碼
啟動(dòng)項(xiàng)目后,訪問http://localhost:9090/config/dev/main 即可以看到配置信息 這個(gè)地址要注意的是 http://localhost:9090/{配置文件名前綴}/{環(huán)境類型}/{倉庫分支標(biāo)簽}
如在倉庫創(chuàng)建的文件名為config-dev.yml,那么配置文件名前綴就是config,環(huán)境就是指文件名后綴 dev,倉庫標(biāo)簽就是當(dāng)前存放文件的倉庫分支名
看到以下信息說明啟動(dòng)項(xiàng)目且配置獲取成功
子工程-config-client
子工程就是個(gè)問題了,當(dāng)時(shí)創(chuàng)建時(shí)一直無法讀取配置信息就是子工程這里出現(xiàn)的問題,有兩個(gè)問題覺得要說明的,先看下子工程創(chuàng)建過程。
pom文件
```xml <?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"> <parent> <artifactId>ch2-1</artifactId> <groupId>com.example</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>ch2.springcloud</groupId> <artifactId>config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> </dependencies> </project>
啟動(dòng)類
package cn.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class,args); } }
配置類或者通過@Value獲取配置信息說明
package cn.springcloud.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "cn.springcloud.book") public class ConfigInfoProperties { private String config; public String getConfig() { return config; } public void setConfig(String config) { this.config = config; } }
測(cè)試類Controller
這個(gè)類寫了兩個(gè)獲取配置信息的方式,一個(gè)是通過@Autowired注入配置類,一個(gè)是通過@Value來獲取
package cn.springcloud.controller; import cn.springcloud.config.ConfigInfoProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigController { @Autowired private ConfigInfoProperties configInfoProperties; @Value("${demo.value}") private String value; @GetMapping("/getmsg") public String getMsg(){ System.out.println("value: "+value); System.out.println("this config is: "+configInfoProperties.getConfig()); return configInfoProperties.getConfig(); } }
配置文件
這里的配置文件就是一個(gè)問題點(diǎn)了,配置如下啟動(dòng)時(shí)會(huì)先摘取配置信息再啟動(dòng),所以把配置中心的配置放到bootstrap.yml中
問題:label: main 這個(gè)是指配置指向當(dāng)前創(chuàng)建的分支,如果沒有則默認(rèn)是master,網(wǎng)上就是這樣說的,后來發(fā)現(xiàn),現(xiàn)在的直接在github創(chuàng)建倉庫后,顯示的是main,所以當(dāng)時(shí)我沒有配置或者配置成master時(shí)一直獲取不了配置信息,所以重新查看了倉庫信息,如下圖:
bootstrap.yml說明:
spring: cloud: config: label: main uri: http://localhost:9090 name: config profile: dev
application.yml說明
server: port: 9000 spring: application: name: config-client
另一個(gè)問題
配置了配置中心的URL,即上面bootstrap.yml中的 uri: http://localhost:9090,但是項(xiàng)目一直啟動(dòng)的是訪問 uri: http://localhost:8888,當(dāng)時(shí)就納悶,找了很久都沒有找到在哪里配置了8888,后來又是清緩存,還是不行,最后在client添加server依賴包,再重啟,結(jié)果發(fā)現(xiàn)正常了,正常后又把它刪除,也正常了。
org.springframework.cloud spring-cloud-config-server
從github拉取的文件在本地存放的目錄
如果你不知道文件在哪里,在啟動(dòng)server時(shí)又顯示了以下信息,則說明文件是拉取到本地了:
當(dāng)前你也可以直接在電腦上查找文件名,看到以下類似目錄即可以找到
不知道能不能指定目錄的,這個(gè)讀者可以試下
測(cè)試結(jié)果
啟動(dòng)server client,訪問client提供的接口localhost:9000/getmsg
結(jié)果如圖,說明正常獲取信息了
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
@Conditional注解的使用場(chǎng)景和源碼解析
這篇文章主要介紹了@Conditional注解的使用場(chǎng)景和源碼解析,@Conditional是一個(gè)條件注解,它的作用是判斷Bean是否滿足條件,如果滿足條件,則將Bean注冊(cè)進(jìn)IOC中,如果不滿足條件,則不進(jìn)行注冊(cè),需要的朋友可以參考下2023-11-11Spring集成MyBatis?及Aop分頁的實(shí)現(xiàn)代碼
這篇文章主要介紹了Spring集成MyBatis?及Aop分頁的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04淺談SpringBoot如何封裝統(tǒng)一響應(yīng)體
今天帶各位小伙伴學(xué)習(xí)SpringBoot如何封裝統(tǒng)一響應(yīng)體,文中有非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05Springboot 使用具體化類和配置來縮短單元測(cè)試時(shí)間
這篇文章主要介紹了Springboot 使用具體化類和配置來縮短單元測(cè)試時(shí)間,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11SpringCloud如何利用Feign訪問外部http請(qǐng)求
這篇文章主要介紹了SpringCloud如何利用Feign訪問外部http請(qǐng)求,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03