首页 > 解决方案 > Spring Cloud Config Client 应用程序不使用 bootstrap.properties

问题描述

我正在阅读一些关于 Spring Cloud 的教程,但我的配置客户端应用程序遇到了问题。我有一个在 Linux 机器上运行的 Eureka 服务器,在我的 Mac 上我有一个配置服务器,它正在与 Eureka 服务器正确注册。我使用 spring 初始化器生成了一个配置客户端应用程序。我拥有的依赖项是:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</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-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

我有一个这样的 bootstrap.properties 文件:

spring.application.name=config-client-app
spring.cloud.config.discovery.enabled=true
eureka.client.serviceUrl.defaultZone=http://192.168.1.125:8761/eureka/

当配置客户端应用程序启动时,它无法向 Eureka 服务器注册,因为它正在寻找在本地机器上运行的实例。如果我将它放在 application.properties 中,那么它会在 eureka 注册,但名称为 UNKNOWN。

eureka.client.serviceUrl.defaultZone=http://192.168.1.125:8761/eureka/

由此看来,配置客户端应用程序没有使用 bootstrap.properties 文件。根据我在类路径上使用 spring-cloud-starter-config 所读到的内容,它应该在启动时查找 bootstrap.properties 文件。

主应用程序类具有以下注释。

@SpringBootApplication
@EnableDiscoveryClient
@RestController

我在这里想要实现的是让客户端向 eureka 询问配置服务器的位置,然后从那里获取它的配置。

谢谢安德鲁

标签: javaspringspring-boot

解决方案


根据这里的这篇文章,我认为我已经解决了这个问题。Spring Cloud Config:客户端不尝试连接到配置服务器

我添加了

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

到我的 pom.xml ,现在它似乎按预期工作。


推荐阅读