Setting Up a Centralized Configuration Server on Azure Spring Cloud

In my previous blog post, we introduced ourselves to Microsoft Azure deployment. If you've not read that yet then its the right time to check that before going ahead.

https://karthikeyanrm.hashnode.dev/deploy-a-spring-boot-application-to-microsoft-azure-using-github-actions

Centralized configuration management is crucial when scaling microservices. In this post, we’ll set up Azure Spring Cloud Config Server to manage Spring Boot configurations using a Git repository for easy updates and consistency across services.

Prerequisites

Before we start, make sure to:

  1. Create an Azure Resource Group

  2. Create an Azure Spring Apps environment

1. Create a Configuration Git Repository

Create a Git repository to store your configuration files. Add:

  • application.yml for shared configs.

  • application-dev.yml for dev-specific configurations.

2. Spring Boot Application Setup

Setup the application with Spring Initializr

You can generate a Spring Boot project at start.spring.io with the required dependencies for Spring Web, Spring Data JPA, and Spring Cloud Config.

Define Configuration Files

In the Git repository:

  • application.yml

  •     spring: application: name: customer-service datasource: url: jdbc:mysql://localhost:3306/customerdb username: ${DB_USER} password: ${DB_PASS}
    
  • application-dev.yml file

  •     spring: datasource: url: jdbc:mysql://dev-db-server:3306/customerdb username: ${DB_USER_DEV} password: ${DB_PASS_DEV} cloud: config: uri: https://<YOUR_AZURE_CONFIG_SERVER_URL> label: main enabled: true
    
  • Note: Secure sensitive information in Azure Key Vault.

Application Properties

In application.properties:

spring.application.name=customer-service 
spring.profiles.active=dev

Add depedencies in the pom file

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-spring-cloud-dependencies</artifactId>
    <version>2.1.1</version>
</dependency>

3. Build the Spring Boot Service

  1. Entity Class - Customer.java

     @Entity
     public class Customer {
         @Id
         @GeneratedValue
         private Long id;
         private String name;
         private String email;
     }
    
  2. Repository Interface - CustomerRepository.java

     public interface CustomerRepository extends JpaRepository<Customer, Long> { }
    
  3. Service Layer - CustomerService.java

     javaCopy code@Service
     public class CustomerService {
         @Autowired
         private CustomerRepository repository;
    
         public List<Customer> getAllCustomers() {
             return repository.findAll();
         }
     }
    
  4. Controller - CustomerController.java

     @RestController
     @RequestMapping("/api/customers")
     public class CustomerController {
         @Autowired
         private CustomerService service;
    
         @GetMapping
         public List<Customer> getCustomers() {
             return service.getAllCustomers();
         }
     }
    

4. Configure Azure Spring Cloud Config Server

Configure Azure Spring Cloud to point to your Git repository:

az spring-cloud config-server set \
    --name <SPRING_CLOUD_APP_NAME> \
    --config-file application.yml \
    --resource-group <RESOURCE_GROUP_NAME>

5. Connecting Spring Boot to the Config Server

The Spring Boot application will automatically retrieve configurations from Azure Spring Cloud.


6. Testing and Deployment

After deploying your application, test it using cURL, Postman, or a browser.

  1. Test with cURL

     -curl -X GET http://<APP_URL>/api/customers
    
  2. Postman/Insomnia

    • Create a GET request to http://<APP_URL>/api/customers.
  3. Browser

    • Navigate to http://<APP_URL>/api/customers.

Refreshing Configuration

To apply changes from the Git repository without restarting, enable the Actuator’s refresh endpoint in application.properties:

management.endpoints.web.exposure.include=refresh

Then refresh:

curl -X POST http://<APP_URL>/actuator/refresh

Cleanup

To avoid charges, delete your resource group:

az group delete --name <RESOURCE_GROUP_NAME>

This setup provides a flexible way to centralise the required configurations across multiple services using the Azure Spring Cloud Config Server. The above mentioned steps enable you to manage properties across environments, scale securely, and ensure all your microservices stay in sync.