SpringCloud-config 配置中心Client端注入报错异常 Could not resolve placeholder xxx

SpringCloud-config 配置中心Client端注入报错异常如下

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-06-26 09:27:01.685 ERROR 29560 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configClientController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'config.info' in value "${config.info}"
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$182/15605342.getObject(Unknown Source) ~[na:na]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]

排查思路:

  1. 检查yml文件格式内容是否有误(请注意:是远程yml配置文件)

在这里插入图片描述

  1. 确认服务端的路径正确(服务端3344)
server:
  port: 3344
spring:
  application:
    name: cloud-config-center # 服务名称
  cloud:
    config:
      server:
        git:
#          uri: [email protected]:NoMessages/sprincloud-config.git # 填写你自己的github路径
          uri: https://github.com/NoMessages/sprincloud-config.git # 填写你自己的github路径
         # 搜索目录
          search-paths:
            - springcloud-config
        # 阅读分支
      label: master
# 注册到eureka
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka


  1. 确保服务端的yml配置文件无误(bootstrap.yml)
server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master # master分支
      name: config  # 读取config配置文件 前缀
      profile: dev  # 后缀为dev
      uri: http://localhost:3344  # 相当于是从服务端获取数据而不是github
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka


  1. 检查配置是否对应远程yml配置文件内容(3355客户端)
@RestController
public class ConfigClientController {

    @Value("${info}")  <----- 这个对应的是远程仓库的info 请注意
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

最终访问效果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43409994/article/details/106966485