java - 多模块项目的 Spring Cloud 客户端配置

标签 java spring-cloud

虽然我已经成功设置了 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)读取/刷新将部署在单个实例/客户端上的属性。

最佳答案

由于您的 commonapp 模块不是应用程序,因此它们不会启动,因此不会读取您的 bootstrap.properties 文件。

看看type-safe configuration propertiesthird party configuration .

您的 commonapp 模块可以为多个类提供其属性。 在 web 模块的 Java 配置中,您可以设置 bootstrap.properties 和/或 application.properties 文件中的值之间的绑定(bind)和配置类。 由于@ConfigurationProperties注释,Spring Boot将创建这些配置类的常规bean。这样您就可以像任何其他 bean 一样注入(inject)您的配置。

希望有帮助! :)

关于java - 多模块项目的 Spring Cloud 客户端配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56957665/

相关文章:

java - Jersey 2.0 HashMap 与 Json 之间的转换

java - 使用 HTMLUnit 通过 Java 进行网页抓取

spring-boot - Spring Cloud Config 与 Github repo 使用凭据

spring - 如何使用 Spring Cloud Security 实现 OAuth2 "Token Exchange"

spring-mvc - 在 Zuul 代理后面时,Spring 重定向 url 问题

spring-cloud - 如何向 Spring Cloud Bootstrap 添加功能

docker - Eureka 客户端的 "prefer-ip-address"无法在 docker 容器中工作

java - 文本文件打印 Java Applet 在 Windows 操作系统中无法运行

java - 类似的命令适用于solaris sparc

java - 在所有 Activity 中显示恒定的 admob 广告横幅