SpringCloudConfig之client端报错Could not resolve placeholder问题
作者:DayDayUp丶 发布时间:2023-11-23 11:19:17
一、前言
环境: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/from和http://localhost:7002/from2,均可成功获取到属性值,如下:
来源:https://blog.csdn.net/songzehao/article/details/101943569


猜你喜欢
- 一.摘要emmmm..对springmvc不太熟练的情况下,如果不出意外的话,项目启动后出现404页面是很烦人。在这里,我记录一下可能会导致
- 简介机器学习在全球范围内越来越受欢迎和使用。 它已经彻底改变了某些应用程序的构建方式,并且可能会继续成为我们日常生活中一个巨大的(并且正在增
- Android 图形特效 &nbs
- 先介绍去掉标题栏的方法: 第一种:也一般入门的时候经常使用的一种方法 requestWindowFeature(Window.FEATURE
- RecyclerView 是 android-support-v7-21 版本中新增的一个 Widgets, 还有一个 CardView 会
- 新手学习记录。写在springboot test 示例 示例代码地址看结尾。后面有带页面的示例。SpringBoot Test无
- 本文实例为大家分享了winform实现五子棋游戏的具体代码,供大家参考,具体内容如下利用数组,根据新旧数组值的不同,获取那个点是什么棋子;说
- 前言多数据源的事务处理是个老生常谈的话题,跨两个数据源的事务管理也算是分布式事务的范畴,在同一个JVM里处理多数据源的事务,比较经典的处理方
- 前言博主上个礼拜,已经实现了quarkus的native image应用的上线,经过两天的监控下来,一切运行指标良好,就是内存升到了100M
- 微信对话列表滑动删除效果很不错的,借鉴了github上SwipeListView(项目地址:https://github.com/likeb
- c#调用surfer软件,添加应用的方法:1.在项目文件上右键->添加引用2.选择COM标签页3.找到Surfer 9 type li
- 本文实例为大家分享了DataGridView带图标的单元格实现具体代码,供大家参考,具体内容如下目的:扩展 C# WinForm 自带的表格
- pom.xml文件需要的内容<dependency> <groupId>re
- 一、场景笔者就Zuul网关下实现其负载均衡与熔断机制(雪崩)进行实践,前提是已经导入zuul相关依赖springboot版本:1.5.9.R
- 包的作用,1是为了防止类和方法的重名,2是为了管理众多的java类。步骤 1 工具包里面有很多个工具类之前讲了打印数据的方法:S
- 作为工厂方法模式的孪生兄弟,相信大家对工厂方法模式和抽象工厂模式傻傻分不清楚吧。那么,就让我来拯救大家吧!抽象工厂模式定义:所谓抽象工厂模式
- 1.使用java.util.Properties类的load()方法示例:Java代码InputStream in = lnew Buffe
- 具体代码如下所示:public class Parent { public static int a = parentStati
- 本文实例讲述了基于C#实现XML文件读取工具类。分享给大家供大家参考。具体如下:这是我去年写的一个XML文件读取工具类,现在做了一些调整 基
- 定义与结构 备忘录(Memento)模式又称标记(Token)模式。GOF给备忘录模式的定义为:在不破坏