Skip to content

Latest commit

 

History

History
245 lines (199 loc) · 7.6 KB

File metadata and controls

245 lines (199 loc) · 7.6 KB

Lab 1: Centralized Configuration Management with Spring Cloud Config (Spring Boot 3.4.1)

Objective

Learn how to set up centralized configuration management for microservices using Spring Boot 3.4.1 and Spring Cloud Config. You will create:

  1. A Config Service that reads a local property initially.
  2. A Config Server that manages configurations from a Git repo.
  3. A Config Client that fetches and refreshes properties dynamically from the server.

Lab Steps

Part 1: Creating the Config Service (Client) as a Simple Microservice

  1. Generate a new Spring Boot project for ConfigService.

    • Visit https://start.spring.io/.
    • Configure the project:
      • Spring Boot Version: 3.4.1
      • Group Id: com.microservices
      • Artifact Id: config-service
      • Package Name: com.microservices.configservice
      • Dependencies:
        • Spring Web
    • Click Generate to download the project zip file.
    • Extract it into a folder named ConfigService.
  2. Import the project into your IDE.

    • Open your IDE and import the ConfigService project as a Maven project.
  3. Add a local property in application.properties.

    • In src/main/resources/application.properties, add:
      message=HelloWorld
  4. Create a controller to verify the property.

    • In src/main/java/com/microservices/configservice, create HelloController.java:
      package com.microservices.configservice;
      
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      @RestController
      public class HelloController {
      
          @Value("${message:Default message}")
          private String message;
      
          @GetMapping("/message")
          public String getMessage() {
              return message;
          }
      }
  5. Run and test the Config Service.

    • Use:
      ./mvnw spring-boot:run
    • Access http://localhost:8080/message. You should see HelloWorld.

Part 2: Creating a Git Repository for Centralized Config

  1. Create a Git repository.

    • Go to GitHub (or another Git provider).
    • Create a repo named config-repo (public or private).
  2. Clone the repository locally.

    • In a terminal:
      git clone https://github.com/<your-username>/config-repo.git
      cd config-repo
  3. Add configuration files to the repo.

    • Create two files for now:
      • application.properties:
        message=Hello from Config Server!
      • config-service.properties:
        message=Hello from Config Service!
    • Commit and push:
      git add .
      git commit -m "Initial config files"
      git push origin main

Part 3: Setting Up the Spring Cloud Config Server

  1. Generate a new Spring Boot project for ConfigServer.

    • Visit https://start.spring.io/.
    • Configure the project:
      • Spring Boot Version: 3.4.1
      • Group Id: com.microservices
      • Artifact Id: config-server
      • Name: ConfigServer
      • Package Name: com.microservices.configserver
      • Dependencies:
        • Spring Web
        • Spring Cloud Config Server
    • Click Generate and extract into ConfigServer.
  2. Import ConfigServer into your IDE.

  3. Enable the Config Server.

    • In ConfigServerApplication.java, add:
      package com.microservices.configserver;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.config.server.EnableConfigServer;
      
      @SpringBootApplication
      @EnableConfigServer
      public class ConfigServerApplication {
          public static void main(String[] args) {
              SpringApplication.run(ConfigServerApplication.class, args);
          }
      }
  4. Configure the Config Server to use your Git repo.

    • In src/main/resources/application.properties, add:
      server.port=8888
      spring.application.name=config-server
      
      spring.cloud.config.server.git.uri=https://github.com/<your-username>/config-repo
    • Replace <your-username> with your actual GitHub username.
  5. Run and test the Config Server.

    • Use:
      ./mvnw spring-boot:run
    • Check:
      • http://localhost:8888/application/default
      • http://localhost:8888/config-service/default
    • You should see your Git-based properties.

Part 4: Connecting ConfigService to the Config Server

  1. Add Spring Cloud Config dependency to ConfigService.

    • In ConfigService’s pom.xml:
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-config</artifactId>
      </dependency>
  2. Configure ConfigService to import configs from the server.

    • In application.properties of ConfigService, add:
      spring.application.name=config-service
      spring.cloud.import=configserver:http://localhost:8888
    • This is the updated approach for Spring Boot 3.4.x / Spring Cloud 2023.0+.
  3. Restart the ConfigService.

    • Again:
      ./mvnw spring-boot:run
    • Check logs to ensure it pulls from config-server.
  4. Test dynamic retrieval.

    • Access http://localhost:8080/message.
    • Now you should see Hello from Config Service! (based on config-service.properties in Git).

Part 5: Advanced Configuration Management

  1. Add Spring Actuator to enable refresh.

    • In ConfigService’s pom.xml:
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
  2. Expose the refresh endpoint.

    • In application.properties:
      management.endpoints.web.exposure.include=refresh
  3. Update a property in Git and refresh.

    • Modify config-service.properties in your config-repo:
      message=Hello from Updated Config Server!
    • Commit and push:
      git add .
      git commit -m "Update message property"
      git push origin main
  4. Trigger a refresh and verify.

    • Use:
      curl -X POST http://localhost:8080/actuator/refresh
    • Access http://localhost:8080/message and confirm the updated message is shown.

Optional Exercise

  1. Add environment-specific configuration.
    • In your Git repo, create application-dev.properties and application-prod.properties with different message values.
    • In ConfigService’s application.properties, set:
      spring.profiles.active=dev
    • Verify different message values for dev or prod by switching profiles.

Conclusion

You have successfully:

  • Created a microservice (ConfigService) that initially used a local property.
  • Set up a Git-based ConfigServer.
  • Pulled configurations dynamically using Spring Cloud Config with Spring Boot 3.4.1.
  • Optionally, refreshed configurations at runtime and handled environment-specific setups.

Enjoy centralized configuration management for your microservices!