Introduction

When working with HashiCorp Consul in a Spring Boot application, you might want to share configuration properties efficiently. While the spring-cloud-consul-config dependency facilitates this, loading multiple property files from a directory is not immediately clear.

In contrast, Spring Cloud Config Server allows for easy configuration management using a native file system approach. For example, you can specify a directory for property files as follows:

spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          searchLocations: file:C:/springbootapp/properties/

Loading Properties with Spring Cloud Consul

To achieve similar functionality with Spring Cloud Config using Consul, you need to ensure that your application is set up to read from Consul's key-value store. Here’s how you can configure your Spring Boot application:

  1. Add Dependency: Make sure you have the spring-cloud-starter-consul-config dependency in your pom.xml or build.gradle file.

  2. Configure Application Properties: In your application.yml or application.properties, specify the Consul configuration settings. Here’s an example configuration:

spring:
  cloud:
    consul:
      config:
        enabled: true
        default-context: application
        profile-separator: '-'
        fail-fast: true
  1. Store Properties in Consul: Upload your property files to the Consul key-value store. You can do this using the Consul CLI or API. For instance, if you have a property file named application-dev.yml, you would store it in Consul under the key config/application-dev.

  2. Accessing Properties: Once your properties are stored in Consul, your Spring Boot application will automatically fetch them based on the active profile you set. Ensure that the profile matches the key used in Consul.

Conclusion

By following these steps, you can effectively manage your Spring Boot application's configuration properties using HashiCorp Consul. This approach not only centralizes your configuration management but also enhances the flexibility of your application deployment.