Spring Cloud Config simplifies configuration management for microservices, but its power truly shines when dealing with diverse environments. This post dives into how to leverage multiple profiles with Spring Cloud Config, giving you fine-grained control over your application’s behavior.

Why Use Multiple Profiles?

Imagine your application needs different settings for development, testing, and production. Hardcoding these variations is messy and error-prone. Profiles let you define distinct sets of properties, activating the appropriate ones based on the environment.

Setting Up Profiles with Spring Cloud Config

  1. Organize Your Configuration Files:

    Within your configuration repository (e.g., Git), structure your files to reflect your profiles. Here’s a common approach:

    ├── application.yml       # Default properties
    ├── application-dev.yml   # Development-specific properties
    ├── application-test.yml  # Test-specific properties
    └── application-prod.yml  # Production-specific properties
    
  2. Activate Profiles in Your Application:

    You have several ways to activate profiles:

    • spring.profiles.active Property: Set this property in your application.properties or application.yml (or as an environment variable).

      spring:
        profiles:
          active: dev
      
    • Command-Line Arguments: Pass profiles when starting your application:

      java -jar myapp.jar --spring.profiles.active=dev
      
  3. Profile-Specific Properties:

    In your profile-specific YAML files, define the properties that should apply to that environment. For example, in application-dev.yml:

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/dev_db
    

    And in application-prod.yml:

    spring:
      datasource:
        url: jdbc:mysql://production-db:3306/prod_db
    

Advanced Profile Handling

  • Profile Inheritance: Spring applies properties in a specific order. application.yml provides defaults, overridden by profile-specific files. This allows you to define common properties once and customize only what’s needed.

  • Multiple Profiles: Activate multiple profiles simultaneously (e.g., spring.profiles.active=dev,debug). Properties from both application-dev.yml and application-debug.yml will be applied.

  • Placeholders: Use placeholders (${...}) to keep your configuration flexible. You can define placeholders in application.yml and override them in profile-specific files.

Example Scenario

Let’s say you want to enable a “mock” data source for your tests.

  1. Create application-test.yml:

    spring:
      profiles:
        active: test 
      datasource:
        url: jdbc:h2:mem:testdb
        driver-class-name: org.h2.Driver
    
  2. Activate the “test” profile when running your tests.

Now, your tests will use the in-memory H2 database instead of your development or production database.

Key Takeaways

  • Profiles provide a structured way to manage environment-specific configurations.
  • Spring Cloud Config seamlessly integrates with Spring profiles, making it easy to externalize and manage these variations.
  • By combining Spring Cloud Config with proper profile usage, you gain flexibility, reduce errors, and streamline your development process.

This post provides a solid foundation for using multiple profiles with Spring Cloud Config. Experiment with different configurations and explore further options like profile-specific files within your application to tailor your setup to your needs.


Discover more from GhostProgrammer - Jeff Miller

Subscribe to get the latest posts sent to your email.

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.