SpringCloudConfig之client端报错Could not resolve placeholder 'from' in value "${from}"

一、前言

环境: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}部分。

三、调试

config-client要从config-server通过http的方式获取到对应分支和对应环境的testcloud配置属性,即http://localhost:7001/testcloud/dev/master/,因此在ConfigServicePropertySourceLocator中,打断点看看具体是怎么获取的。

查看http请求参数args的值,期待参数/{name}/{profile}/{label}中的name=testcloud,而非spring.cloud.config.name的值spring-cloud-config-client,因此要么删掉属性spring.cloud.config.name,使第一个参数为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/fromhttp://localhost:7002/from2,均可成功获取到属性值,如下:

发布了62 篇原创文章 · 获赞 22 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/songzehao/article/details/101943569