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

aurhwmvo  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(325)

我的目标是得到 world-serviceconfig-service .
体系结构: config-service 有依赖关系 spring-cloud-config-server
localhost:8888 world-service 依赖 spring-web 以及 spring-cloud-starter-config .
我所做的:
我已设置配置服务器并向发送get请求 http://localhost:8888/hello-service/master 配置服务器得到 hello-service.properties 从config repo存储库(如果你需要 config-service 的源代码,我将把它推送到这个存储库。)
我的预期结果是 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>
csbfibhn

csbfibhn1#

我花了一天时间,终于找到了解决办法。它可以帮助别人
您需要添加新的依赖项

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

据《Spring Cloud2020.0》
默认情况下,SpringCloudCommons提供的引导不再启用。如果项目需要它,可以通过属性或新的启动程序重新启用它。
要通过属性重新启用,请设置spring.cloud.bootstrap.enabled=true或spring.config.use legacy processing=true。这些需要设置为环境变量、java系统属性或命令行参数。另一个选项是包含新的springcloudstarter引导程序(在pom文件中)。
我用了第一种方法,这对我很有效。

q3qa4bjr

q3qa4bjr2#

SpringBoot2.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-数据导入

628mspwn

628mspwn3#

使用SpringCloud2020,他们改变了引导程序的工作方式,您必须包括一个新的启动程序: spring-cloud-starter-bootstrap .

相关问题