You are currently viewing Spring Cloud Config Server with GitHub

Spring Cloud Config Server with GitHub

Configuring a Spring Cloud Config Server to read configuration from a GitHub repository, using @RefreshScope to refresh beans dynamically, and leveraging Spring Boot Actuator for monitoring and refreshing can be a powerful combination. Let’s go through a step-by-step tutorial.

Step 1: Setup GitHub Repository for Configuration

  1. Create a GitHub Repository:
  • Create a new GitHub repository to store your configuration files. Let’s call it config-repo.
  1. Add Configuration Files:
  • Add application configuration files (e.g., application.yml, application.properties) to the config-repo. These files will contain the properties for your services.

Step 2: Create Spring Cloud Config Server

  1. Create a new Spring Boot Project:
  • Use Spring Initializr to create a new Spring Boot project.
  • Add dependencies: Config Server, Actuator, and Web.
  1. Configure application.properties (or application.yml):
   spring.application.name=config-server
   server.port=8888
   spring.cloud.config.server.git.uri=https://github.com/your-username/config-repo.git
  1. Enable Config Server:
  • Annotate the main application class with @EnableConfigServer.
  1. Run the Config Server:
  • Start the Config Server. It will fetch configuration from the GitHub repository.

Step 3: Create a Sample Service

  1. Create a new Spring Boot Project:
  • Use Spring Initializr to create a new Spring Boot project.
  • Add dependencies: Web, Actuator, and Config Client.
  1. Configure bootstrap.properties (or bootstrap.yml):
   spring.application.name=sample-service
   server.port=8080
   spring.cloud.config.uri=http://localhost:8888
  1. Create a Sample Controller:
  • Create a simple REST controller with a property injected using @Value.

Step 4: Add @RefreshScope

  1. Update the Controller:
  • Add @RefreshScope annotation to the controller to enable dynamic refresh.

Step 5: Enable Actuator Endpoints

  1. Configure Actuator in application.properties (or application.yml):
   management.endpoints.web.exposure.include=*
  1. Refresh Endpoint:
  • The actuator/refresh endpoint can be used to trigger a refresh.

Step 6: Test the Application

  1. Run the Sample Service:
  • Start the Sample Service.
  1. Access the Sample Endpoint:
  • Visit http://localhost:8080/sample to see the initial configuration.
  1. Update Configuration in GitHub:
  • Make changes to the configuration files in the GitHub repository.
  1. Trigger Refresh:
  • Hit the actuator/refresh endpoint of the Sample Service: POST http://localhost:8080/actuator/refresh.
  1. Access the Sample Endpoint Again:
  • Visit http://localhost:8080/sample to see the updated configuration.

Congratulations! You’ve successfully set up a Spring Cloud Config Server reading configuration from a GitHub repository, used @RefreshScope to dynamically refresh beans, and enabled Actuator for monitoring and refreshing.

Leave a Reply