SpringCloudConfig之client端報(bào)錯(cuò)Could?not?resolve?placeholder問題
一、前言
環(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}"
..
二、排查
很顯然,無法成功啟動(dòng)SpringCloudConfig客戶端的原因是不能從配置文件中解析并成功注入屬性值${from}。
回頭檢查SpringCloudConfig服務(wù)端的配置和運(yùn)行狀況均正常無誤,再查看上面異常之前的啟動(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自身的問題,它的bootstrap.properties配置如下:
其中,testcloud為config-server中配置的Git倉庫中存儲(chǔ)的配置文件名中的${application}部分。
三、調(diào)試
config-client要從config-server通過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類如下:
@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,均可成功獲取到屬性值,如下:
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Java鎖性能提高(鎖升級(jí))機(jī)制的總結(jié)
這篇文章主要介紹了關(guān)于Java鎖性能提高(鎖升級(jí))機(jī)制的總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05使用Java實(shí)現(xiàn)對(duì)兩個(gè)秒級(jí)時(shí)間戳相加
在現(xiàn)代應(yīng)用程序開發(fā)中,時(shí)間戳的處理是一個(gè)常見需求,特別是當(dāng)我們需要對(duì)時(shí)間戳進(jìn)行運(yùn)算時(shí),比如時(shí)間戳的相加操作,本文我們將探討如何使用Java對(duì)兩個(gè)秒級(jí)時(shí)間戳進(jìn)行相加,并展示詳細(xì)的代碼示例和運(yùn)行結(jié)果,需要的朋友可以參考下2024-08-08詳解Guava Cache本地緩存在Spring Boot應(yīng)用中的實(shí)踐
Guava Cache是一個(gè)全內(nèi)存的本地緩存實(shí)現(xiàn),本文將講述如何將 Guava Cache緩存應(yīng)用到 Spring Boot應(yīng)用中。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01java實(shí)現(xiàn)創(chuàng)建臨時(shí)文件然后在程序退出時(shí)自動(dòng)刪除文件
這篇文章主要介紹了java實(shí)現(xiàn)創(chuàng)建臨時(shí)文件然后在程序退出時(shí)自動(dòng)刪除文件,從個(gè)人項(xiàng)目中提取出來的,小伙伴們可以直接拿走使用。2015-02-02