SpringCloudConfig之client端報(bào)錯(cuò)Could?not?resolve?placeholder問(wèn)題
一、前言
環(huán)境:jdk 1.8,SpringCloud Greenwich.SR2。
如題,springcloud config-client啟動(dòng)報(bào)錯(cuò):Could not resolve placeholder 'from' in value "${from}",具體異常堆棧如下:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-10-02 11:33:10.394 ERROR 9204 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'scopedTarget.testController':
Injection of autowired dependencies failed;
nested exception is java.lang.IllegalArgumentException:
Could not resolve placeholder 'from' in value "${from}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:380) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
..
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'from' in value "${from}"
..
二、排查
很顯然,無(wú)法成功啟動(dòng)SpringCloudConfig客戶(hù)端的原因是不能從配置文件中解析并成功注入屬性值${from}。
回頭檢查SpringCloudConfig服務(wù)端的配置和運(yùn)行狀況均正常無(wú)誤,再查看上面異常之前的啟動(dòng)日志,也顯示了可以成功連接到config-server,如下:
2019-10-02 11:33:06.919 INFO 9204 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:7001/
2019-10-02 11:33:08.789 INFO 9204 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=spring-cloud-config-client, profiles=[dev], label=master, version=516591f9604c28b12cd5a65f9fb89806f2f1c602, state=null
2019-10-02 11:33:08.789 INFO 9204 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}]}
所以只可能是config-client自身的問(wèn)題,它的bootstrap.properties配置如下:

其中,testcloud為config-server中配置的Git倉(cāng)庫(kù)中存儲(chǔ)的配置文件名中的${application}部分。
三、調(diào)試
config-client要從config-server通過(guò)http的方式獲取到對(duì)應(yīng)分支和對(duì)應(yīng)環(huán)境的testcloud配置屬性,即http://localhost:7001/testcloud/dev/master/,因此在ConfigServicePropertySourceLocator中,打斷點(diǎn)看看具體是怎么獲取的。

查看http請(qǐng)求參數(shù)args的值,期待參數(shù)/{name}/{profile}/{label}中的name=testcloud,而非spring.cloud.config.name的值spring-cloud-config-client,因此要么刪掉屬性spring.cloud.config.name,使第一個(gè)參數(shù)為spring.application.name=testcloud;要么將其直接設(shè)置為testcloud即可。
下面列出正確的配置:
#對(duì)應(yīng)config-server中配置文件的{application}部分
spring.application.name=testcloud
server.port=7002
#注意此屬性將覆蓋spring.application.name,若不一致則會(huì)導(dǎo)致不能從config-server讀取PropertySources
spring.cloud.config.name=testcloud
#對(duì)應(yīng)config-server中配置文件的{profile}部分
spring.cloud.config.profile=dev
#對(duì)應(yīng)config-server中配置文件的{label}部分
spring.cloud.config.label=master
#配置中心config-server的地址
spring.cloud.config.uri=http://localhost:7001/四、測(cè)試
測(cè)試Controller類(lèi)如下:
@RefreshScope
@RestController
public class TestController {
@Value("${from}")
private String from;
@Autowired
private Environment env;
@RequestMapping("/from")
public String fetchFromVal() {
return from;
}
@RequestMapping("/from2")
public String fetchFromVal2() {
return env.getProperty("from", "undefined");
}
}瀏覽器或Postman或curl訪問(wèn)http://localhost:7002/from和http://localhost:7002/from2,均可成功獲取到屬性值,如下:

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot數(shù)據(jù)庫(kù)鏈接池配置方法
這篇文章主要介紹了Spring Boot數(shù)據(jù)庫(kù)鏈接池配置方法,需要的朋友可以參考下2017-04-04
Spring-boot oauth2使用RestTemplate進(jìn)行后臺(tái)自動(dòng)登錄的實(shí)現(xiàn)
這篇文章主要介紹了Spring-boot oauth2使用RestTemplate進(jìn)行后臺(tái)自動(dòng)登錄的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
SpringBoot的java -jar命令啟動(dòng)原理解讀
這篇文章主要介紹了SpringBoot的java -jar命令啟動(dòng)原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
解決Error occurred during initialization o
這篇文章主要介紹了解決Error occurred during initialization of VM Java虛擬機(jī)初始化失敗問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
關(guān)于SpringBoot Actuator漏洞補(bǔ)救方案
SpringBoot Actuator模塊提供了健康檢查,審計(jì),指標(biāo)收集,HTTP 跟蹤等,是幫助我們監(jiān)控和管理SpringBoot應(yīng)用的模塊,本文將主要介紹SpringBoot Actuator漏洞的補(bǔ)救方案,需要的朋友可以參考下2023-06-06
Java Robot應(yīng)用示例之機(jī)器人功能
這篇文章主要為大家詳細(xì)介紹了Java Robot應(yīng)用示例之機(jī)器人功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Jmeter結(jié)構(gòu)體系及運(yùn)行原理順序解析
這篇文章主要介紹了Jmeter結(jié)構(gòu)體系及運(yùn)行原理順序解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
Java?Web項(xiàng)目中如何添加Tomcat的Servlet-api.jar包(基于IDEA)
servlet-api.jar是在編寫(xiě)servlet必須用到的jar包下面這篇文章主要給大家介紹了基于IDEAJava?Web項(xiàng)目中如何添加Tomcat的Servlet-api.jar包的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04

