【问题标题】:Spring Cloud Config Service-Client not workingSpring Cloud Config Service-Client 不起作用
【发布时间】:2018-01-27 12:19:56
【问题描述】:

在运行 config-client 时出现错误。直接来自 spring 指南的简单事情不起作用。

2017-08-18 13:44:31.194 ERROR 12548 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.restController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'message' in value "${message}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.sprin

RestController

@RefreshScope
@org.springframework.web.bind.annotation.RestController
public class RestController {
    @Value("${message}")
    private String message;

    @RequestMapping("/message")
    String getMessage() {
        return this.message;
    }
}

bootstrap.yml of config-client 
spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8888
management:
  security:
    enabled: false


application.yml of config-server
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/config-properties
server:
  port: 8888

来自配置客户端服务的日志

启动 ApplicationContext 时出错。要显示自动配置报告,请在启用“调试”的情况下重新运行您的应用程序。 2017-08-18 23:13:45.080 错误 13812 --- [main] os.boot.SpringApplication:应用程序启动失败

org.springframework.beans.factory.BeanCreationException:创建名为“scopedTarget.restController”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 java.lang.IllegalArgumentException:无法解析值“${message}”中的占位符“消息” 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:345) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.cloud.context.scope.GenericScope$BeanLifecycleWrapper.getBean(GenericScope.java:359) ~[spring-cloud-context-1.2.3.RELEASE.jar:1.2.3.RELEASE] 在 org.springframework.cloud.context.scope.GenericScope.get(GenericScope.java:176) ~[spring-cloud-context-1.2.3.RELEASE.jar:1.2.3.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.cloud.context.scope.refresh.RefreshScope.start(RefreshScope.java:121) ~[spring-cloud-context-1.2.3.RELEASE.jar:1.2.3.RELEASE] 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144] 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_144] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_144] 在 java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_144] 在 org.springframework.context.event.ApplicationListenerMethodAdapter.doInvoke(ApplicationListenerMethodAdapter.java:256) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.context.event.ApplicationListenerMethodAdapter.processEvent(ApplicationListenerMethodAdapter.java:177) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.context.event.ApplicationListenerMethodAdapter.onApplicationEvent(ApplicationListenerMethodAdapter.java:140) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE] 在 com.demo.ConfigClientApplication.main(ConfigClientApplication.java:9) [classes/:na] 引起:java.lang.IllegalArgumentException:无法解析值“$ {message}”中的占位符“消息” 在 org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:831) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1086) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] ...省略了31个常用框架

与目标VM断开连接,地址:'127.0.0.1:59908',传输:'socket'

【问题讨论】:

  • github.com/config-properties 看起来不像一个有效的存储库。
  • 抱歉忘了提,它有效但已删除完整路径。文件内容与spring guides相同。
  • 日志中的任何内容?
  • 是的,已添加。配置客户端服务甚至没有出现。
  • 我没有足够的信息。也许你可以分享一个重现问题的项目?

标签: spring-cloud spring-cloud-config


【解决方案1】:

确保 GIT URI 是有效的,它应该是 https://github.com/userName/repositoryName 然后将您的配置文件夹推送到 repositoryName。

示例:https://github.com/PraveenKumarMekala/Microservices-With-Spring-Example

这里我有一个名为 ConfigData 的文件夹,我在其中放置了所有 yml 文件。同样,您也应该有一个包含 config-client.yml 文件的文件夹(yml 文件名应该与您的应用程序名称相同,因为您没有提到任何配置文件在您的客户端 yml 文件中,以便使用您的应用程序名称回退到您的默认配置文件)

您的 yml 文件应如下所示

config-client.yml --- message : Hello World!

config-client.yml 文件应该放在你的 git repo 中。

配置客户端的bootstrap.yml

--- spring: profiles: active: profileName application: name: config-client cloud: config: uri: http://localhost:8887 server: port: 8888

注意:如果你提到profileName那么yml文件名应该是config-client-profileName.yml

配置服务器的application.yml

--- spring: cloud: config: server: git: uri:https://github.com/PraveenKumarMekala/Microservices-With-Spring-Example searchPaths: ConfigData # "native" is used when the native profile is active, for local tests with a classpath repo: native: searchLocations: classpath:offline-repository/ server: port: 8888

做这个改变并尝试,希望它有用。

PS:如果你没有git账号,创建一个账号然后repository

https://help.github.com/articles/create-a-repo/

【讨论】:

    【解决方案2】:

    可能会改变

    配置客户端的bootstrap.yml


    spring:
      profiles:
        active: profileName
      application:
        name: config-client
      cloud:
        config:
          uri: http://localhost:8888
    server:
      port: 8887
    

    【讨论】:

      猜你喜欢
      • 2019-03-13
      • 2021-05-04
      • 1970-01-01
      • 2016-05-12
      • 2022-07-14
      • 2017-10-12
      • 2020-10-05
      • 2017-08-17
      • 2016-02-18
      相关资源
      最近更新 更多