【问题标题】:spring cloud config client not loading configuration from config serverspring cloud config客户端未从配置服务器加载配置
【发布时间】:2015-07-13 01:20:35
【问题描述】:

我正在关注此链接: http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_client_side_usage

我反复测试,没有看到Spring云客户端正在从云服务器加载配置,请帮忙看看错误在哪里:

POM:

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>
</dependencies>

应用:

@Configuration
@EnableAutoConfiguration
@RestController
public class ConfigclientApplication {
    
    @Value("${spring.cloud.config.uri}")
    String url;
    
    @Value("${production.host}")
    String host;
    
    
    @RequestMapping("/")
    public String home() {
        return "Host is  => " + this.host ;
    }
    
    public static void main(String[] args) {
        SpringApplication.run(ConfigclientApplication.class, args);
    }
}

bootstrap.properties: spring.cloud.config.uri=http://localhost:8888

  1. 配置服务器是好的: http://localhost:8888/spirent/default
{
  "name": "spirent",
  "profiles": [
    "default"
  ],
  "label": "master",
  "propertySources": [
    {
      "name": "classpath:/spirent.yml",
      "source": {
        "production.host": "server1",
        "production.port": 9999,
        "production.value1": 12345,
        "test.host": "server2.com",
        "test.port": 4444,
        "test.value": "hello123"
      }
    }
  ]
}
  1. 现在 http://localhost:8080/ 根本无法启动。

    创建名为“configclientApplication”的 bean 时出错 @Value 的自动注入似乎找不到 production.host 环境值。

从配置服务器加载后如何读取客户端中的配置?

感谢您的帮助。

【问题讨论】:

  • 如果您正在学习教程,请遵循教程。本教程提到你应该使用spring-cloud-starter 依赖而不是spring-cloud-config-client。对于应用程序而言,配置来自哪里并不重要,它们只是属性,您可以像任何其他属性一样通过Environment@Value 访问它们。
  • 我改成client或者starter,但是在配置服务器中找不到配置值,有什么办法吗?
  • 你能解决这个问题吗,我面临的问题是配置服务器的属性是从方法中开始读取的,如果它用 @RequestMapping 注释而不是其他方法。不知道在这种情况下可以做什么。

标签: spring cloud config


【解决方案1】:

正如 Deinum 所暗示的,我会确保您将客户端配置为 parent 作为 spring-cloud-starter-parent 并为其提供一个版本。当您包含在依赖项中并记住 cloud 与 boot 是不同的项目时,spring cloud 提供的 Maven 插件将不起作用。将其更改为:

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

其次,作为一门新学科(可能不是您的问题),我会在您的应用程序上使用新注释而不是 @Configuration 和 @EnableAutoConfiguration

@SpringBootApplication

第三,仔细检查你的配置服务器上有@EnableConfigServer

第四,确保您的客户端上的 bootstrap.properties 指定了您的 spring 应用程序名称:

spring.application.name=spirent

最后,如果您使用了 spring-cloud-config 示例项目,您必须在 URI 中设置默认用户和安全密码:

http://user:ddf4757e-0077-42e4-b2ad-2ae04340b08c@localhost:8888

否则,请尝试从此处的 spring-cloud-config 项目开始,以确保您的配置服务器设置正确:

https://github.com/spring-cloud/spring-cloud-config

【讨论】:

  • 谢谢@RubesMN,关键的修复是将:spring.application.name=myapp 添加到 bootstrap.properties,然后我可以看到 - {"profiles":[],"configService:classpath: /myapp.yml":{"my.otherprop":"myotherval"}," 来自 /env。现在,你能回答另一个问题,我如何访问配置客户端中的配置值?我试过 @Value(${profiles }) 并且代码无法启动,抱怨注入错误。谢谢
  • 您可以使用带有@Value("${my.otherprop}") 注释的变量声明或通过带有@Component @ConfigurationProperties(prefix="my") 注释的包含名为“otherprop”的变量的类来访问。请务必将答案标记为正确,以供其他人查看。
  • 我还注意到 @Value 注释变量不会被刷新,除非它们包含的 spring 组件具有 @RefreshScope 并且调用客户端的 /refresh 端点。当按照文档中的说明调用 /env 时,@Value@ConfigurationProperties 配置值都不会从配置服务器刷新。
  • 我想我在这里遗漏了一些东西。我在此处关注此代码 (github.com/spring-cloud-samples/svn-config-server) 并且配置服务器在另一台主机上运行,​​因此我更改了 bootstrap.yml 并将 URI 指向新主机。我的@Value 不起作用,客户端应用程序无法启动,“无法解析占位符”。
  • 似乎在客户端中您确实配置了spring.cloud.config.uri,所以您在那里很好。确保您也有spring.cloud.config.enabled: true。虽然这不会阻止它启动,但请检查您的实际属性是否为 SVN 正确设置。最后,在客户端,确保您的顶级类(通常称为应用程序)具有正确的注释。 @SpringBootApplication @RefreshScope。希望这会有所帮助
【解决方案2】:

对于那些在使用与 Spring Boot 相关的默认 Maven Spring Cloud 依赖版本到 2.4.0 或 2.4.1(例如 Spring Boot 2.4.0 的 2020.0.0-M5)之后到达这里的人,除了遵循 RubesMN 的好建议,请注意,在这种 Spring Cloud 依赖项中,默认情况下不启用引导。根据Spring Cloud 2020.0 Release Notes

由 spring-cloud-commons 提供的 Bootstrap 默认不再启用。如果您的项目需要它,可以通过属性或新的启动器重新启用它。

  • 通过属性集 spring.cloud.bootstrap.enabled=true 或 spring.config.use-legacy-processing=true 重新启用。这些需要设置为环境变量、java 系统属性或命令行参数。
  • 另一个选项是包含新的 spring-cloud-starter-bootstrap(在您的 POM 文件中)。

我使用了第二个选项,效果很好。

【讨论】:

    【解决方案3】:

    如果您使用的是 2020.0.0 版本的 spring cloud,则需要在 maven 依赖项中启用此依赖项以启用引导程序,该依赖项在 2020.0.0 中默认禁用。

    Breaking changes in 2020.0.0

    它对我有用。

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

    【讨论】:

    • 谢谢。也为我工作
    • 谢谢,这对我有帮助!
    • 感谢它的帮助,
    • 这正是我遇到的问题的解决方案。非常感谢!
    • 耶稣在天堂里有一个特别的地方给你。或湿婆神。
    【解决方案4】:

    如果你使用的是2020.0.0版本的spring cloud那么你需要在你的maven依赖中添加这个依赖来启用bootstrap,默认是禁用的:

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

    【讨论】:

    • Machan,谢谢 ban man dawas ganak kattak kawa,我依赖 eka nattam 配置服务器 ekata connect wenne naha。谢谢禁令。
    【解决方案5】:

    我正在添加我的发现。对我来说,在健康度过 2 小时后,我发现,

    • 配置服务器应该有:[spring-cloud-config-server]

      <parent>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>2.5.3</version>
           <relativePath/>
      </parent>
      <properties>
           <java.version>1.8</java.version>
           <spring-cloud.version>2020.0.3</spring-cloud.version>
      </properties>
      <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-config-server</artifactId>
           </dependency>
      </dependencies>
      
    • 客户服务应该有:[spring-cloud-starter-config]

      <parent>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-parent</artifactId>
           <version>2.5.3</version>
           <relativePath/>
      </parent>
      <properties>
           <java.version>1.8</java.version>
           <spring-cloud.version>2020.0.3</spring-cloud.version>
      </properties>
      <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-starter-config</artifactId>
           </dependency>
      </dependencies>
      
    • 客户服务应声明:application.propertiesapplication.yml

      #This does not work
      #spring.cloud.config.uri=http://localhost:8880/config-server
      spring.application.name=name-by-which-properties/yml-file-is-created
      spring.config.import=optional:configserver:http://host:port/application-context
      management.endpoints.web.exposure.include=*
      

    什么对我不起作用:

    • 添加spring-cloud-starter-bootstrap
    • 添加spring-cloud-starter-parent

    希望这将通过Spring Boot (2.5.3)Spring Cloud 2020.0.3 帮助像我这样的其他人

    【讨论】:

      【解决方案6】:

      这是spring-boot 版本的问题。 Spring-cloud-starter-config 依赖项不适用于较旧的 spring-boot 版本。尝试将您的spring-boot 版本更改为可能高于2.4.0 的最新版本。

      以下gradle 实现对我有用:

      id 'org.springframework.boot' version '2.4.5'
      id 'io.spring.dependency-management' version '1.0.11.RELEASE'
      id 'java'
      

      【讨论】:

        【解决方案7】:

        如果有人在 PCF(使用市场配置服务器)上遇到此问题,即使在为新的 Spring Boot 版本添加 spring-cloud-starter-bootstrap 之后,请确保也添加了以下依赖项 -

        <dependency>
          <groupId>io.pivotal.spring.cloud</groupId>
          <artifactId>spring-cloud-services-starter-config-client</artifactId>
        </dependency>
        

        【讨论】:

          猜你喜欢
          • 2016-11-26
          • 2021-12-10
          • 2020-07-23
          • 2020-07-10
          • 2021-02-26
          • 1970-01-01
          • 1970-01-01
          • 2016-06-26
          • 2021-03-17
          相关资源
          最近更新 更多