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配置如下:

SpringCloudConfig之client端报错Could not resolve placeholder问题

其中,testcloud为config-server中配置的Git仓库中存储的配置文件名中的${application}部分。

三、调试

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

SpringCloudConfig之client端报错Could not resolve placeholder问题

查看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,均可成功获取到属性值,如下:

SpringCloudConfig之client端报错Could not resolve placeholder问题

来源:https://blog.csdn.net/songzehao/article/details/101943569

标签:SpringCloudConfig,client,Could,resolve,placeholder
0
投稿

猜你喜欢

  • 亲自教你实现栈及C#中Stack源码分析

    2021-05-23 19:39:10
  • Java中使用websocket实现在线聊天功能

    2023-01-03 22:07:20
  • 详解 Java Maximum redirects (100) exceeded

    2023-08-05 21:03:47
  • Android开发之自动朗读TTS用法分析

    2021-08-21 22:31:00
  • 解决Swagger2返回map复杂结构不能解析的问题

    2022-07-15 09:17:04
  • android实现双日期选择控件(可隐藏日,只显示年月)

    2023-05-09 12:59:10
  • JavaScript嵌入百度地图API的最详细方法

    2023-04-12 14:33:03
  • C#实现一阶卡尔曼滤波算法的示例代码

    2022-12-23 05:28:45
  • SpringMVC源码解读之 HandlerMapping - AbstractDetectingUrlHandlerMapping系列初始化

    2023-02-12 16:14:21
  • Java环境配置图文教程(推荐)

    2023-09-17 11:27:42
  • c语言重要的字符串与内存函数

    2023-04-28 00:35:42
  • 基于C#的音乐播放器主Form实现代码

    2022-07-13 01:21:36
  • IDEA版最新MyBatis程序配置教程详解

    2022-08-25 03:27:26
  • Java同步函数代码详解

    2022-10-13 23:22:03
  • Android的UI调优教程

    2021-12-16 01:46:43
  • java实现文件拷贝的七种方式

    2023-07-20 19:01:41
  • C#编程简单实现生成PDF文档的方法示例

    2023-09-26 05:36:32
  • java模拟hibernate一级缓存示例分享

    2023-06-18 08:43:55
  • springBoot集成Elasticsearch 报错 Health check failed的解决

    2022-12-07 05:18:16
  • C#通过第三方组件生成二维码(QR Code)和条形码(Bar Code)

    2023-02-14 03:53:36
  • asp之家 软件编程 m.aspxhome.com