Spring 云配置客户端 - 无法解析占位符



我收到以下错误

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

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

bootstrap.yml 在 application.yml 之前加载

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

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

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

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

使用 git

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

最新更新