IllegalArgumentException: Could not resolve placeholder ‘from‘ in value “${from}“



项目场景:

学习Spring Cloud Config的时候出现的问题, 因此记录下问题希望帮助到有缘人




问题描述:

启动config-server 配置中心后成功, 启动调用的微服务 config-client的时候提示

Could not resolve placeholder 'from' in value "${from}"

//异常信息如下
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.configController': 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.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]




原因分析:

 第一个可能原因:

如果config-client客户端的启动日志中uri地址是http://localhost:8888, 那么问题可能是bootstrap.properties中的配置的config.uri配置不正确 引起

如下图:启动日志中打印的信息表示config server的地址信息

         可以查看下ConfigServicePropertySourceLocator 对象处理,locate方法中从通过ConfigClientProperties对象获取配置中心的config server 地址,然后通过日志打印,查看ConfigClientProperties对象会发现, 默认uri地址是"http://localhost:8888", 参考源码如下

public class ConfigServicePropertySourceLocator implements PropertySourceLocator{

@Override
	@Retryable(interceptor = "configServerRetryInterceptor")
	public org.springframework.core.env.PropertySource<?> locate(
			org.springframework.core.env.Environment environment) {

        //从ConfigClientProperties对象中获取配置信息 properties
		ConfigClientProperties properties = this.defaultProperties.override(environment);
		CompositePropertySource composite = new CompositePropertySource("configService");
		RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(properties)
				: this.restTemplate;
		Exception error = null;
		String errorBody = null;

        //这里打印了配置中的 Uri 
        //查看ConfigClientProperties对象会发现uri的默认值为"http://localhost:8888"
		logger.info("Fetching config from server at: " + properties.getRawUri());

        // 省略后面的代码
    }
}

        所以, 如果启动日志打印地址为 http://localhost:8888 且你的配置中心地址不是它,那么表示读取bootstrap.properties中的配置的spring.cloud.config.uri出现了问题,需要检查是否漏配置或者KEY配置字符错误,我第一次配置的时候spring 错误写成了spiring ,不仔细看还真不容易发现。 正确配置spring.cloud.config.uri 后, 启动config-client 的日志中的信息参考下图

 第二个可能原因:

 config-server中配置的Git地址错误或者Git用户密码缺失或者不正确,可以通过CURL工具验证请求 config-server是否成功, 如果不成功需要检查git配置,最常见的是GIT仓库用户名密码没有写对

#GIT仓库用户名
spring.cloud.config.server.git.username=
#GIT仓库密码
spring.cloud.config.server.git.password=

 第三个可能原因:

config-client端启动日志获取到对的server地址, 但是第二行爆出401错误

INFO 7496 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://127.0.0.1:8006/
WARN 7496 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: 401 null
INFO 7496 --- [           main] c.configclient.ConfigClientApplication   : No active profile set, falling back to default profiles: default
INFO 7496 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded

检查config-server是不是添加了security安全验证,如果添加了验证, 那么客户端config-client需要在配置文件中bootstrap.properties 增加验证的用户名密码,如果用户名或者密码错误,或者用户名密码配置在application.xml文件中的时候会出现401错误

spring.cloud.config.username=配置服务器上security配置的用户名
spring.cloud.config.password=配置服务器上security配置的用户密码

  上一篇:Spring Cloud Config快速入门

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

=PNZ=BeijingL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值