使用配置中心config client,不拉取配置問題
config client沒有進行fetch操作bug
***@TOC
你好!在學習和使用ConfigServer進行配置管理時,在ConfigServer一切正常的情況下,發現ConfigClient沒有進行fetch操作,埠號依舊是預設的8080,於是查閱了一些資料,解決方法如下。
##關於bootstrap.yml和application.yml
在網上查閱資料,大部分會說優先載入bootstarp.yml,其實不然,springboot專案根本不會識別bootstarp.yml,還有網上說加入cloud-context依賴就可以識別,嘗試無果。
其實,springCloud專案才會優先載入bootstrap.yml。所以,當你的專案是springboot專案,然後將application修改為bootstrap,會以預設埠進行啟動。如下所示:
圖中顯示沒有找到配置檔案,使用預設配置。
加上cloud-context依賴之後,依然如此,無解。
其實,springCloud專案優先載入bootstrap.yml是毋庸置疑的,而關鍵點是,你的專案是否編譯為springCloud。如果你的專案是通過idea的搭建的,就要看看你的pom檔案的parent,是springBoot專案,還是springCloud專案。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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--這裡標識著你的專案,是springboot--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.css</groupId> <artifactId>productor</artifactId> <version>0.0.1-SNAPSHOT</version> <name>productor</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties>
於是,原因找到了,於是將其修改springCloud專案,如下:
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Dalston.SR5</version>
<relativePath/>
</parent>
於是,正常啟動專案即可。
##如果此時依然無法正常啟動
那就要注意看你的Cloud版本和boot版本,是否相容。例如,上述Dalston,只相容boot版本1.5.X,而現在的springboot的版本已經上升至2.6.x,所以注意看相容性。相容性對比如下:
Release Train | Boot Version |
---|---|
Hoxton | 2.2.x |
Greenwich | 2.1.x |
Finchley | 2.0.x |
Edgware | 1.5.x |
Dalston | 1.5.x |
Camden | 1.4.x |
Bixton | 1.3.x |
Angle | 1.2.x |
具體情況還是以官網為準:
https://spring.io/projects/spring-cloud#overview.
##如果此時依然沒有走到你的配置檔案
那至少,你會看到
圖中顯示,你的專案已經從ConfigServer進行了拉取配置檔案,如果依然沒有用你的配置檔案,那就說明,你的配置是有問題的。
具體的配置如下(記得用bootstrap.yml)
#註冊中心
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8760/eureka
spring:
application:
name: productor-service
#配置中心
cloud:
config:
#字尾
profile: dev
label: master
#通過ip直接訪問配置中心
# uri: http://localhost:9001/
#通過eurka訪問配置中心
discovery:
#配置中心服務
service-id: config-server
enabled: true
至此,便解決了問題,專案會載入Server上的對應配置檔案,埠成為遠端配置檔案的埠。
如果本次文章對您有幫助,希望您點個贊!