Search code examples
javaspring-cloud

Spring cloud client configuration for multiple module project


Though I have successfully setup spring cloud config client/server with GIT as backend properties repo. However I have a basic question for following module structure (maven) for one of my client code:

Client 1:

  common (maven module)
  app (maven module)
  web (Contains the spring boot application, bootstrap.properties, application.properties)

For the above structure, I am able to read/refresh properties from spring cloud config server for the "web" module (since that is the one where my SpringBootApplication is), however not able to understand that how to inject configure/inject properties in other modules as well, like there could be properties for common module or app module.

I tried adding bootstrap.properties in other modules with them pointing to same spring cloud config server. But that did not work out.

Spring cloud config server application.properties:

server.port=8888

spring.cloud.config.server.git.uri=

management.security.enabled=false

web module's bootstrap.properties

spring.application.name=test

spring.cloud.config.uri=http://localhost:8888

management.security.enabled=false

spring.profiles.active=default,prod

Maven dependencies (cloud config client):

    <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>

Please help me to understand, as to how I can read/refresh the properties across multiple modules like common, app or web which would be deployed on single instance/client.


Solution

  • Since your common and app modules aren't applications, they won't start and thus won't read your bootstrap.properties files.

    Take a look at type-safe configuration properties and third party configuration.

    Your common and app modules can provide multiple classes with their properties. In the Java configuration of your web module you can set up the binding between the values in your bootstrap.properties and/or application.properties files, and the configuration classes. Because of the @ConfigurationProperties annotation, Spring Boot will create regular beans of these configuration classes. That way you can inject your configuration just like any other bean.

    Hope that helps! :)