Search code examples
javaspringspring-bootspring-cloud-config

How to configure spring cloud config server to read properties from File System?


I'm learning how to centralize configurations using Spring Cloud Config Server File System Backend : docs.spring.io

I have a multi maven project

project:
├── pom.xml
│
├── project-config-provider
│   ├── pom.xml
│   ├── src
│   │   └── main
│   │       └── java
│   │           └── com
│   │               └── company
│   │                   └── project
│   │                       └── config_provider
│   │                           └── Application.java
│   ├── config
│   │   ├── application_config-provider--native.yaml
│   │   └── application_config-provider--aws.yaml
│   │
│   └── cloud_config
│       ├── application-config_main.yaml
│       ├── application-config_storage.yaml
│       ├── application-config_swagger.yaml
│       └── application-env_local.yaml
│
│
└── project-abc
    ├── pom.xml
    ├── src
    │   └── main
    │       └── java
    │           └── com
    │               └── company
    │                   └── project
    │                       └── abc
    │                           └── AbcApplication.java
    └── config
        └── application_abc.yaml

The module project-config-provider will have to be the spring cloud config server, and the module projcet-abc will use some of the configs from project-config-provider.

each module has a config folder:

  • the project-config-provider contains multiple yaml files, one for each environment (application_config-provider--native.yaml for local test, application_config-provider--aws.yaml for aws test with secretmanager an S3)

application_config-provider--native.yaml :

spring:
   application:
      name: config-provider
   profile:
      active: native
   cloud:
      config:
         server:
            native:
               search-location: cloud_config
            prefix: /config
            bootstrap: true

server.port: 8888
  • all the other modules has only one yaml file

application_abc.yaml :

spring:
   application:
      name: maps
      version: 1.0.0
   profiles:
      #inlcude configurations from cloud_config of config-provider 
      include: config_main, config_storage, config_swagger
   config:
      import: configserver:http://${CONFIG_PROVIDER_HOST:localhost}:${CONFIG_PROVIDER_PORT:8888}/config

project-abc application:

@SpringBootApplication
@ComponentScan({ "com.project*" })
@EntityScan("com.project*")
@EnableJpaRepositories("com.project*")
public class AbcApplication {

    public static void main(String[] args) {
        SpringApplication.run(AbcApplication.class, args);
    }

}

on project-abc pom.xml, I have added:

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

project-config-provider application:

@EnableConfigServer
@ComponentScan(basePackages = { "com.project" } )
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class Application {

    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Configuration
    public static class SecurityPermitAllConfig {
        
        @Bean
        public SecurityFilterChain filterChain(final HttpSecurity httpSecurity) throws Exception {
            httpSecurity.authorizeRequests().anyRequest().permitAll().and().csrf().disable();
            return httpSecurity.build();
        }
        
    }
    
}

project-config-provider pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  
  <parent>
    <groupId>com.project</groupId>
    <artifactId>project</artifactId>
    <version>1.0.0</version>
    <relativePath>../</relativePath>
  </parent>
  
  <artifactId>project-config-provider</artifactId>
  <name>Project Config Provider</name>
  
  <dependencies>
     
    <!-- cloud config server -->        
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    
    <!-- custom configuration in application-*.yaml -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

  </dependencies>   
    
</project>

the Spring Boot Application project-config-provider is runned using

--spring.config.import=config/application-config-provider--native.yaml

as program arguments, but as soon as it starts, it fails:

***************************
APPLICATION FAILED TO START
***************************

Description:

Invalid config server configuration.

Action:

If you are using the git profile, you need to set a Git URI in your configuration.  If you have set spring.cloud.config.server.bootstrap=true, you need to use a composite configuration.

I'm not using git.. but I have set bootstrap=true,
I have also tried using composite on application_config-provider--native.yaml :

spring:
   application:
      name: config-provider
   profile:
      active: composite
   cloud:
      config:
         server:
            composite:
             - type: native
               search-locations: cloud_config
            prefix: /config
            bootstrap: true

server.port: 8888

Solution

  • project-config-provider pom.xml :

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      
      <parent>
        <groupId>com.project</groupId>
        <artifactId>project</artifactId>
        <version>1.0.0</version>
        <relativePath>../</relativePath>
      </parent>
      
      <artifactId>project-config-provider</artifactId>
      <name>Project Config Provider</name>
      
      <dependencies>
         
        <!-- spring cloud config -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        
        <!-- spring security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    
      </dependencies>
      
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
        
    </project>
    

    application_config-provider--native.yaml :

    spring:
       application:
          name: config-provider
       profiles:
          active: native
       cloud:
          config:
             server:
                native:
                   search-locations: cloud_config
                prefix: /config
                bootstrap: true
    
    server.port: 8888
    

    Now I'm able to see the properties on:

    http://localhost:8888/config/application-config_storage/env_local