spring cloud config客户端没有从配置服务器加载配置

use*_*967 9 cloud spring config

我关注此链接: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>
Run Code Online (Sandbox Code Playgroud)

应用:

    @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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  1. 配置服务器很好: http:// localhost:8888/spirent/default

    { "名称": "思博伦", "简档":[ "默认"], "标签": "主", "propertySources":[{ "名称": "类路径:/spirent.yml", "源": { "production.host": "服务器1", "production.port":9999, "production.value1":12345 "test.host": "server2.com", "test.port":4444,"测试.值 ":" hello123" }}]}

  2. 现在http:// localhost:8080 /根本无法启动.

    创建名为'configclientApplication'的bean时出错似乎自动注入@Value无法找到production.host环境值.

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

谢谢你的帮助.

小智 31

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

2020.0.0 中的重大变化

它对我有用。

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
Run Code Online (Sandbox Code Playgroud)


小智 24

对于那些在使用与 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 发行说明

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

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

我使用了第二个选项并且工作得很好。


小智 8

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

2020.0.0 中的重大变化

它对我有用。

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

是的,这是正确的,这对我有用......谢谢


Muh*_*der 7

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

  • 配置服务器应具有:[ 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>
    
    Run Code Online (Sandbox Code Playgroud)
  • 客户服务应具备:[ 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>
    
    Run Code Online (Sandbox Code Playgroud)
  • Client Services should declare: in application.properties or application.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=*
    
    Run Code Online (Sandbox Code Playgroud)

What did not work for me:

  • Adding spring-cloud-starter-bootstrap
  • Adding spring-cloud-starter-parent

Hope this will help others like me with Spring Boot (2.5.3) and Spring Cloud 2020.0.3


Rub*_*sMN 5

正如Deinum所暗示的那样,我确保您将客户端配置为父级作为spring-cloud-starter-parent并为其提供版本.当你在依赖项中包含并且记住云是一个与启动不同的项目时,Spring云提供的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>
Run Code Online (Sandbox Code Playgroud)

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

@SpringBootApplication
Run Code Online (Sandbox Code Playgroud)

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

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

spring.application.name=spirent
Run Code Online (Sandbox Code Playgroud)

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

http://user:ddf4757e-0077-42e4-b2ad-2ae04340b08c@localhost:8888
Run Code Online (Sandbox Code Playgroud)

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

https://github.com/spring-cloud/spring-cloud-config
Run Code Online (Sandbox Code Playgroud)