java - 微服务不从 spring-cloud-config-server 微服务中获取属性

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

我想用 spring 云配置服务器构建一个简单的微服务应用程序。我正在使用 Github 存储库来存储我的配置文件。目前,我有两个非常简单的微服务。其中之一是 cloud-config-server,它从 Github 上的远程仓库检索属性。在这个微服务的主类中,我添加了 @EnableConfigServer 注释,并在 application.properties 文件中提供了几个属性:

spring.application.name=cloud-config-server
server.port=8888

spring.cloud.config.server.git.uri=*Remote repo URI*
spring.cloud.config.server.git.username=*Username*
spring.cloud.config.server.git.password=*Password*
spring.cloud.config.server.git.clone-on-start=true
如果我去 http://localhost:8888/cloud-config-server/default 一切正常,我的 spring 云配置服务器会成功检索属性。
在我的第二个微服务中,我添加了带有 2 个属性的 bootstrap.properties 以连接到 cloud-config-server:
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.name=cloud-config-server
而且我还添加了这个依赖
       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
       </dependency>
不幸的是,我的微服务没有从 cloud-config-server 检索属性。当我尝试通过 @Value 注释使用属性时,出现错误:
注入(inject) Autowiring 的依赖项失败;嵌套异常是 java.lang.IllegalArgumentException:无法解析值“${test.text}”中的占位符“test.text”
我敢肯定,我没有在我的属性名称“test.text”中打错字。

最佳答案

我已经解决了我的问题。默认情况下不再启用 Bootstrap.properties 文件。您可以通过添加新的依赖项来启用它:

       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
或者,您可以删除 bootstrap.properties 文件并在 application.properties 文件中提供这些属性
spring.config.import=configserver:*URI to your cloud config server*
spring.cloud.config.name=*Your cloud config server name*

关于java - 微服务不从 spring-cloud-config-server 微服务中获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64880940/

相关文章:

java - Android - 跨 4 个 ImageView 设置背景

java - 将两个不同的数组与不同的数据组合起来

java - Spring 值 : incorrect shows fields

spring-boot - 未找到依赖项 : Spring Boot 的合格 bean 类型

java - 关于在同一端口上运行多个应用程序

java - 计算平方根的二进制搜索 (Java)

java - Java同时访问同一个对象的不同成员

Spring MVC 和 @Validate : Perform validate only on specific condition or if user changes the property

spring - Grails 3.0.9 : Spock integration tests' @Rollback annotation is not working

java - Spring 启动 : Initializing EmbeddedServletContainerFactory based on some flag