虽然我已经成功设置了 Spring Cloud 配置客户端/服务器,并使用 GIT 作为后端属性存储库。但是,我对我的客户端代码之一的以下模块结构(maven)有一个基本问题:
客户端1:
common (maven module)
app (maven module)
web (Contains the spring boot application, bootstrap.properties, application.properties)
对于上述结构,我能够从 Spring Cloud 配置服务器中读取/刷新“web”模块的属性(因为这是我的 SpringBootApplication 所在的模块),但是无法理解如何注入(inject)配置/也可以在其他模块中注入(inject)属性,例如公共(public)模块或应用程序模块可能有属性。
我尝试在其他模块中添加 bootstrap.properties,让它们指向同一个 Spring Cloud 配置服务器。但这并没有成功。
Spring云配置服务器application.properties:
服务器端口=8888
spring.cloud.config.server.git.uri=
management.security.enabled=false
网络模块的 bootstrap.properties
spring.application.name=测试
spring.cloud.config.uri= http://localhost:8888
management.security.enabled=false
spring.profiles.active=默认,产品
Maven 依赖项(云配置客户端):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
请帮助我理解如何跨多个模块(例如公共(public)、应用程序或 Web)读取/刷新将部署在单个实例/客户端上的属性。
最佳答案
由于您的 common
和 app
模块不是应用程序,因此它们不会启动,因此不会读取您的 bootstrap.properties 文件。
看看type-safe configuration properties和 third party configuration .
您的 common
和 app
模块可以为多个类提供其属性。
在 web
模块的 Java 配置中,您可以设置 bootstrap.properties
和/或 application.properties
文件中的值之间的绑定(bind)和配置类。
由于@ConfigurationProperties注释,Spring Boot将创建这些配置类的常规bean。这样您就可以像任何其他 bean 一样注入(inject)您的配置。
希望有帮助! :)
关于java - 多模块项目的 Spring Cloud 客户端配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56957665/