【问题标题】:Why bootstrap.properties is ignored by spring-cloud-starter-config?为什么 spring-cloud-starter-config 忽略 bootstrap.properties?
【发布时间】:2021-03-11 18:35:58
【问题描述】:

我的目标是从config-service 获取world-service 的配置。

架构:

  1. config-service 依赖于 spring-cloud-config-server localhost:8888
  2. world-service 依赖于 spring-webspring-cloud-starter-config

我做了什么:

  1. 我已经设置了配置服务器并向http://localhost:8888/hello-service/master发送了一个GET请求,配置服务器从the config-repo repository获取了hello-service.properties。 (如果需要config-service的源码,我会推送到this repository。)

我的预期结果: world-service 使用 8081 端口。

我的实际结果: world-service 使用 8080 端口。

bootstrap.properties

spring.application.name=world-service
spring.cloud.config.uri=http://localhost:8888

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>world-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>world-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>2020.0.0-M5</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

【问题讨论】:

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


    【解决方案1】:

    在 Spring Cloud 2020 中,他们改变了引导程序的工作方式,您必须包含一个新的启动器:spring-cloud-starter-bootstrap

    【讨论】:

    • 你的意思是添加`org.springframework.cloudspring-cloud-starter-bootstrap`到pom.xml吗?感谢您指出这一点,我试图用谷歌搜索但找不到任何解释。尝试将其添加到 pom.xml Cannot resolve org.springframework.cloud:spring-cloud-starter-bootstrap:3.0.0-M5 时我也遇到了错误
    • @kidfrom 看起来是正确的。这是我的 c&p:org.springframework.cloudspring-cloud-starter-bootstrap3.0.0-M5
    • @kidfrom 它在里程碑回购中:repo.spring.io/milestone/org/springframework/cloud/…
    • @kidfrom 在此处查看云团队关于该主题的信息:spring.io/blog/2020/10/07/…
    • 或者使用新的spring.config.import github.com/spring-cloud/spring-cloud-release/wiki/…
    【解决方案2】:

    我花了一天时间,终于找到了解决方案。它可能会帮助其他人

    你需要添加新的依赖

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

    根据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 文件中)。

    我使用了第一个选项,这对我有用。

    【讨论】:

      【解决方案3】:

      Spring Boot 2.4 引入了一种通过 spring.config.import 属性导入配置数据的新方法。这是现在绑定到配置服务器的默认方式。

      要连接到配置服务器,请在 application.yml 中设置以下内容:

      春天: 应用: 名称:APPLICATION_NAME 配置: 导入:可选:configserver:http://USER:PASSWORD@MY_HOST:PORT/

      您可以在https://docs.spring.io/spring-cloud-config/docs/3.0.0/reference/html/#config-data-import查看更多详细信息

      【讨论】:

      • 反对这个答案的人是错误的。自 Spring Boot 2.4 起,Bootstrap 已弃用,默认情况下不再启用。您可以根据接受的答案重新启用它,但将来它将被删除,并且此答案中描述的方式是新的默认值。
      • @Mzzl 您在哪里看到不推荐使用引导程序?我看到它不再是默认设置,但我没有看到任何提及弃用...?
      猜你喜欢
      • 2018-02-13
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-14
      • 2020-04-17
      • 2018-08-01
      • 2022-08-16
      相关资源
      最近更新 更多