How to externalize Spring Boot Properties to an Azure App Configuration Store
Hello everyone, Hope you are doing well. In this tutorial, you will learn how to centralize Spring Boot properties to an Azure App Configuration.
A little bit of Background
Azure App Configuration
Azure App Configuration provides a service to centrally manage application settings and feature flags. Modern programs, especially programs running in a cloud, generally have many components that are distributed in nature. Spreading configuration settings across these components can lead to hard-to-troubleshoot errors during application deployment. Use App Configuration to store all the settings for your application and secure their access in one place.
Spring Boot
Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can "just run".
More Info - https://spring.io/projects/spring-boot
Create an App Configuration store
Sign in to the Azure portal https://portal.azure.com/#home
and search for "App Configuration" like the below image,
Then click on the "Create app configuration" button.
You will be taken to a page like the below image,
Here we selected our Resource group as "Knowledgefactory" and entered the Resource name as "Knowledgefactory-poc". Then click on the "Review + create" button.
You will be taken to a page like the below image,
Then click on the "Create" button.
Now, You can see "Deployment is in progress" like the below image.
Once deployment is completed you can see the "Your deployment is complete" page like the below image,
Then go to to "Access Keys" page,
Select "Configuration explorer",
Creating a simple Spring Boot Application
Final Project Structure:
Pom.xml
Add the Spring Cloud Azure Config starter.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath />
</parent>
<groupId>com.knd.dev.demo</groupId>
<artifactId>spring-azure-app-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-azure-app-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>
azure-spring-cloud-appconfiguration-config
</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yaml
Configure your Spring Boot application to use your Azure App Configuration.
spring:
cloud:
azure:
appconfiguration:
stores[0]:
connection-string:<Connection string>
**Important note** Do not publish the fields directly for security. It would be a good choice to define these variables as an environment variable.
Add a Rest Controller to be able to test
package com.knd.dev.demo;
import java.util.HashMap;
import java.util.Map;
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("${config.greeting}")
private String greeting;
@GetMapping
public Map<String, String> getMessage() {
Map<String, String> message =
new HashMap<String, String>();
message.put("message", greeting);
return message;
}
}
Spring Boot Main Driver
package com.knd.dev.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Local Setup and Run the application
Step 1: Download or clone the source code from GitHub to a local machine - Click here
Step 2: mvn clean install
Step 3: Run the Spring Boot application -
mvn spring-boot:run