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
猜你喜欢
- 需求: 给定一个URL地址, 例如: http://www.cncounter.com/tools/shorturl.php, 解析对应的I
- 一、Kotlin 调用 Java1. kotlin 关键字转义java 中的方法或变量 是 kotlin 的关键字时,使用反引号 `` 对关
- hashCode()和equals()方法可以说是Java完全面向对象的一大特色.它为我们的编程提供便利的同时也带来了很多危险.这篇文章我们
- 本文实例讲述了Android编程实现WebView添加进度条的方法。分享给大家供大家参考,具体如下:标准的XML界面<?xml ver
- Java获取环境变量Java 获取环境变量的方式很简单: System.getEnv() 得到所有的环境变量Syste
- FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化
- Step1: 安装JDK并配置环境变量;Step2: 安装Gradle进入点击打开链接官网首页点击install gra
- 在类中自定义的“函数”称为“方法”,由于C#是完全面向对象的
- ActiveMQ是Apache的一个开源项目,它是一个功能强劲的开源消息总线,也是一个中间件产品,它是JMS的一个实现。在介绍ActiveM
- LocalDateTime 是 Java 8 中日期时间 API 提供的一个类,在日期和时间的表示上提供了更加丰富和灵活的支持。LocalD
- 文章来源:csdn 作者:chensheng913对于Java语言,最体贴的一项设计就是它并没有打算让人们为了写程序而写程序——人们也需要考
- 所谓回调,就是客户程序C调用服务程序S中的某个方法A,然后S又在某个时候反过来调用C中的某个方法B,对于C来说,这个B便叫做回调方法。下面看
- 前文本章是关于Java流程控制语句的最全汇总,本篇为汇总中篇。流程是人们生活中不可或缺的一部分,它表示人们每天都在按照一定的流程做事。比如出
- 一.OO(面向对象)的设计基础面向对象(OO):就是基于对象概念,以对象为中心,以类和继承为构造机制,充分利用接口和多态提供灵活性,来认识、
- 一、饿汉式单例类public class Singleton { privat
- sms4j 2.0 全新来袭即sms-aggregation成功加入dromara之后,很多人向我们反应了项目名称太长不好记,也太绕口, 在
- 容器适配器我们可以看出,栈中没有空间配置器(内存池),而是适配器适配器是一种设计模式(设计模式是一套被反复使用的、多数人知晓的、经过分类编目
- 1.需求背景需要实现一个动态加载但不显示出来的视图,且该视图上有个动态生成的二维码,最后用其去生成一张快照(也就是图片)。(常见这种情况是来
- 对接前端后效果展示如图:1、CPU相关信息实体类/** * CPU相关信息 * * @author csp */public class
- 1、前言最近在用Kotlin+Spring Boot写一个后端项目,实体类习惯性地用了Kotlin中的data class,但是Spring