【问题标题】:Spring Cloud Config Client - could not resolve placeholderSpring Cloud Config Client - 无法解析占位符
【发布时间】:2017-08-17 10:56:01
【问题描述】:

我收到以下错误

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'rate' in string value "${rate}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.3.5.RELEASE.jar:4.3.5.RELEASE]

使用的spring boot版本是

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

服务器的yml文件是

server:
  port: 9000
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/kswat/microservices
          search-paths:
            - 'station*'

服务器启动正常,在 9000 端口。

客户项目: 使用相同版本的 spring boot。

spring:
  application:
    name: s1rates
  profiles:
    active: default 
  cloud:
    config:
      uri: http://localhost:9000
      enabled: true

控制器代码:

@RestController
public class RateController {

    @Value("${rate}")
    String rate;

    @RequestMapping("/rate")
    public String getRate(){        
        return rate;        
    }

}

8888端口有限制吗? 为什么我的客户从寻找 8888 开始

 :: Spring Boot ::        (v1.4.3.RELEASE)

2017-03-24 13:04:51.348  INFO 1048 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8888
2017-03-24 13:04:52.479  WARN 1048 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/s1rates/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
2017-03-24 13:04:52.483  INFO 1048 --- [           main] c.b.samples.M2ConfigclientApplication    : The following profiles are active: default
2017-03-24 13:04:52.518  INFO 1048 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@e84a8e1: startup date [Fri Mar 24 13:04:52 GMT 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@70325e14
2017-03-24 13:04:53.492  WARN 1048 --- [           main] o.s.c.a.ConfigurationClassPostProcessor  : Cannot enhance @Configuration bean definition 'refreshScope' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
2017-03-24 13:04:53.657  INFO 1048 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=d17fb23f-878c-3e56-87f0-af48d4c36965
2017-03-24 13:04:53.743  INFO 1048 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$4e824d73] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-03-24 13:04:54.178  INFO 1048 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-03-24 13:04:54.194  INFO 1048 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat

如果我在配置服务器中使用 8888,那么客户端可以干净地工作,无一例外。 什么是 8888 魔法,为什么我必须坚持它?这是启动版本的问题还是我的错误?

【问题讨论】:

    标签: spring-boot cloud spring-cloud-config


    【解决方案1】:

    已解决 - 将云客户端项目 application.yml 文件名更改为 bootstrap.yml 服务器的9000端口工作

    bootstrap.yml 在 application.yml 之前加载

    【讨论】:

      【解决方案2】:

      非常重要,客户端应用程序名称需要与存储库中的属性名称相同。例如,您的配置客户端应用程序名称是 config-client,那么您的存储库中的属性文件应该是 config-client-dev .properties。否则您将收到无法解析占位符 ${xxx} 错误。

      【讨论】:

      • 没错。这是初学者经常容易错过的最重要的部分。
      • 您错过了个人资料。由于您将属性文件命名为 *-dev.properties。您必须定义“spring.profiles.active=dev”来获取开发属性文件。
      【解决方案3】:

      客户端应用程序名称需要与存储库中的属性名称相同。例如,您的配置客户端应用程序名称是config-client,那么您的存储库中的属性文件应该是config-client-dev.properties。否则您将收到“无法解析占位符 ${xxx}”错误。

      【讨论】:

        【解决方案4】:

        使用 git 作为属性源的 Spring 云服务器与 git 样式的存储库一起使用,因此它可以使用不同的分支,以及关于该问题的重要事项 - 必须提交更改才能可见强>。

        【讨论】:

          【解决方案5】:

          在您的 pom.xml 文件中添加以下依赖项

              <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-bootstrap</artifactId>
              </dependency>
          

          【讨论】:

          • 在最新版本 2020 + 中,您需要此依赖项才能使应用程序能够查找 bootstrap.yml/properties。但最初的问题是引导程序本身不存在..所以只能说与 OP 有一点关系
          猜你喜欢
          • 2020-10-05
          • 2017-05-01
          • 2019-01-12
          • 1970-01-01
          • 2016-10-22
          • 1970-01-01
          • 1970-01-01
          • 2018-08-14
          • 2016-07-24
          相关资源
          最近更新 更多