SpringCloudConfig之client端報錯Could?not?resolve?placeholder問題
一、前言
環(huán)境:jdk 1.8,SpringCloud Greenwich.SR2。
如題,springcloud config-client啟動報錯: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}"
..
二、排查
很顯然,無法成功啟動SpringCloudConfig客戶端的原因是不能從配置文件中解析并成功注入屬性值${from}。
回頭檢查SpringCloudConfig服務端的配置和運行狀況均正常無誤,再查看上面異常之前的啟動日志,也顯示了可以成功連接到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自身的問題,它的bootstrap.properties配置如下:

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

查看http請求參數(shù)args的值,期待參數(shù)/{name}/{profile}/{label}中的name=testcloud,而非spring.cloud.config.name的值spring-cloud-config-client,因此要么刪掉屬性spring.cloud.config.name,使第一個參數(shù)為spring.application.name=testcloud;要么將其直接設置為testcloud即可。
下面列出正確的配置:
#對應config-server中配置文件的{application}部分
spring.application.name=testcloud
server.port=7002
#注意此屬性將覆蓋spring.application.name,若不一致則會導致不能從config-server讀取PropertySources
spring.cloud.config.name=testcloud
#對應config-server中配置文件的{profile}部分
spring.cloud.config.profile=dev
#對應config-server中配置文件的{label}部分
spring.cloud.config.label=master
#配置中心config-server的地址
spring.cloud.config.uri=http://localhost:7001/四、測試
測試Controller類如下:
@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訪問http://localhost:7002/from和http://localhost:7002/from2,均可成功獲取到屬性值,如下:

總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring Boot數(shù)據(jù)庫鏈接池配置方法
這篇文章主要介紹了Spring Boot數(shù)據(jù)庫鏈接池配置方法,需要的朋友可以參考下2017-04-04
Spring-boot oauth2使用RestTemplate進行后臺自動登錄的實現(xiàn)
這篇文章主要介紹了Spring-boot oauth2使用RestTemplate進行后臺自動登錄的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
解決Error occurred during initialization o
這篇文章主要介紹了解決Error occurred during initialization of VM Java虛擬機初始化失敗問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
Java?Web項目中如何添加Tomcat的Servlet-api.jar包(基于IDEA)
servlet-api.jar是在編寫servlet必須用到的jar包下面這篇文章主要給大家介紹了基于IDEAJava?Web項目中如何添加Tomcat的Servlet-api.jar包的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2024-04-04

