Spring Cloud Config Client - 无法解析占位符

时间:2017-03-24 13:20:23

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

我收到以下错误

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]

使用的春季启动版本是

<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:
  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魔法,为什么我必须坚持下去?是这个启动版本问题还是我的错误?

4 个答案:

答案 0 :(得分:3)

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

bootstrap.yml在application.yml

之前加载

答案 1 :(得分:2)

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

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