为什么 bootstrap.properties 被 spring-cloud-starter-config 忽略?

Why bootstrap.properties is ignored by spring-cloud-starter-config?

我的目标是从 config-service.

获取 world-service 的配置

架构:

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

我做了什么:

  1. 我已设置配置服务器并向 http://localhost:8888/hello-service/master 发送 GET 请求,配置服务器从 the config-repo repository. (If you need the config-service's source code, I will push it to this repository 获取 hello-service.properties。)

我的预期结果: 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>

在 Spring Cloud 2020 中,他们改变了 bootstrap 的工作方式,您必须包括一个新的入门者:spring-cloud-starter-bootstrap.

折腾了一天,终于找到了解决办法。它可能会帮助其他人

您需要添加新的依赖项

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

根据 Spring Cloud 2020.0

Bootstrap, provided by spring-cloud-commons, is no longer enabled by default. If your project requires it, it can be re-enabled by properties or by a new starter.

To re-enable by properties set spring.cloud.bootstrap.enabled=true or spring.config.use-legacy-processing=true. These need to be set as an environment variable, java system property or a command line argument. The other option is to include the new spring-cloud-starter-bootstrap (in your POM file).

我使用了第一个选项并且对我有用。

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

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

spring:
  application:
    name: APPLICATION_NAME
  config:
    import: optional:configserver:http://USER:PASSWORD@MY_HOST:PORT/

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