【问题标题】:Spring Cloud Config Client app not using bootstrap.propertiesSpring Cloud Config Client 应用程序不使用 bootstrap.properties
【发布时间】:2021-05-04 17:08:53
【问题描述】:

我正在学习一些 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 询问配置服务器的位置,然后从那里获取它的配置。

谢谢 安德鲁

【问题讨论】:

  • 我认为bootstrap.properties 只对服务器有效;您列出的属性应该放在普通客户的application.properties
  • 不确定是不是这样。查看此页面cloud.spring.io/spring-cloud-config/reference/html/… Discovery First Bootstrap 确实说客户端需要一个 bootstrap.properties 文件来定位发现服务器,然后它可以从中获取客户端应用程序的配置。就我而言,虽然它没有获取 bootstrap.properties 文件。

标签: java spring spring-boot


【解决方案1】:

根据这里的这篇文章,我认为我已经解决了这个问题。 Spring Cloud Config: client doesn't attempt to connect to the config server

我加了

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

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

【讨论】:

    猜你喜欢
    • 2018-01-27
    • 2021-03-11
    • 2019-12-04
    • 2022-07-14
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2021-07-08
    • 2019-03-13
    相关资源
    最近更新 更多