Managing Java configuration with Azure App Configuration

  1. Polyglot language support — Java, .NET, .NET core, Python, Javascript
  2. Feature Management — Not only configuration but also feature flag
  3. Private Endpoint Support
  4. Encryption using CMK
  5. Managed Identity support

Create Azure App Configuration

I will create an instance with the name"appconf-java"using az CLI. Needless to say, you should choose a unique name. Creating an instance takes only a few minutes to complete.

Configuration explorer on Azure Portal
Test key/value pair

Create Sample Spring Boot Apps

Generate a project using Spring initilizr with a few simple dependencies, Web, Actuator, and Azure Support.

<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-cloud-appconfiguration-config</artifactId>
<version>2.8.0</version>
</dependency>
spring:
cloud:
azure:
appconfiguration:
enabled: true
stores[0]:
connection-string: ${APP_CONFIGURATION_CONNECTION_STRING}
management:
endpoints:
web:
exposure:
include: "*"
proertySources shows properties from App Configuration

spring.application.name and @Profile with Azure App Configuration

If you have sharp eyes, you must have noticed that the key of our message in App Configuration is "/application/message" but actual property presented is "message". This is the predefined behavior by Azure Spring Cloud AppConfiguration library. This library reads all the properties that start with "/application/" and present them to the client. This is a critical detail to remember for Spring developers as this is the way to separate the different applications where we used to use "spring.application.name" in Spring Cloud Config Server. Here is an example.

spring:
cloud:
azure:
appconfiguration:
enabled: true
stores[0]:
connection-string: ${APP_CONFIGURATION_CONNECTION_STRING}
selects[0]:
key-filter: /${spring.application.name}/

application:
name: appconfig-java

management:
endpoints:
web:
exposure:
include: "*"
key1 and key2 have been added with the prefix "/appconfig-java/"
Properties with key-filter "/appconfig-java/"
spring:
cloud:
azure:
appconfiguration:
enabled: true
stores[0]:
connection-string: ${APP_CONFIGURATION_CONNECTION_STRING}
selects[0]:
key-filter: /${spring.application.name}/
application:
name: appconfig-java
profiles:
active: dev

management:
endpoints:
web:
exposure:
include: "*"
Previous properties are missing.
New key-value pairs with the label 'dev'
key-value pairs with the label 'dev.'

Run sample app on Azure Container Apps with Managed Identity

As most of the basics are covered and the sample app is ready, let's deploy this to the platform. Then we will switch the access keys to managed identity as access keys wouldn't be a recommended approach for the production use case.

spring:
cloud:
azure:
appconfiguration:
stores[0]:
endpoint: https://appconf-java.azconfig.io
selects[0]:
key-filter: /${spring.application.name}/
credential:
managed-identity-enabled: true

enabled: true
application:
name: appconfig-java
config:
activate:
on-profile: containerapps

---
spring:
cloud:
azure:
appconfiguration:
enabled: true
stores[0]:
connection-string: ${APP_CONFIGURATION_CONNECTION_STRING}
selects[0]:
key-filter: /${spring.application.name}/
application:
name: appconfig-java
config:
activate:
on-profile: dev
management:
endpoints:
web:
exposure:
include: "*"
logging:
level:
com.azure: DEBUG
$ az containerapp create -n appconfig-java-system -g containerapps-rg \
--image eggboy/appconfig-java:0.0.1 --environment jay-containerapps-env \
--ingress external --target-port 8080 \
--cpu 1 --memory 2.0Gi \
--system-assigned \
--env-vars SPRING_PROFILES_ACTIVE=containerapps \
--query properties.configuration.ingress.fqdn
...
Container app created. Access your app at https://appconfig-java.victoriousocean-1bfe0788.westus.azurecontainerapps.io/
"appconfig-java.victoriousocean-1bfe0788.westus.azurecontainerapps.io"
List down shows all the available managed identities.
$ az containerapp revision list -n appconfig-java -g containerapps-rg -o table
Command group 'containerapp' is in preview and under development. Reference and support levels: https://aka.ms/CLI_refstatus
CreatedTime Active TrafficWeight Name
------------------------- -------- --------------- -----------------------
2022-07-21T03:31:01+00:00 True 100 appconfig-java--6f47omf
$ az containerapp revision restart --revision appconfig-java--6f47omf -n appconfig-java -g containerapps-rg
There are no key-value pairs yet starting with "/appconfig-java/" with label "containerapps"
New pair with label "containerapps"
key1 with label "containerapps"

Refresh Configuration at runtime

App Configuration provides two ways to refresh the configuration from apps, poll-based and push-based models. Poll-based model is literally polling changes by a predefined interval. In contrast, the push-based model uses Azure Event Grid to send the change event down to the application using a webhook. Just like Spring Cloud Config, App Configuration exposes endpoint in actuator like /actuator/appconfiguration-refresh, /actuator/appconfiguration-refresh-bus that can be used with webhook. I will explore the poll-based model in this article for a quick test.

<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-cloud-appconfiguration-config-web</artifactId>
<version>2.8.0</version>
</dependency>
spring:
cloud:
azure:
appconfiguration:
enabled: true
stores[0]:
monitoring:
enabled: true
refresh-interval: 5s
triggers[0]:
key: refresh-key
label: dev

connection-string: ${APP_CONFIGURATION_CONNECTION_STRING}
selects[0]:
key-filter: /${spring.application.name}/
application:
name: appconfig-java
config:
activate:
on-profile: dev
2022-07-21 15:13:18.698 DEBUG 90978 --- [         task-4] c.a.s.c.config.AppConfigurationRefresh   : Skipping configuration refresh check for https://appconf-java.azconfig.io

Wrapping Up

Azure App Configuration is a relatively unknown service among Azure developers, but hopefully, this article shows its potential to you developers and makes you keen to try it out. Its integration with Spring Boot and Cloud with Azure SDK ensures an easier transition from Spring Cloud Config to Azure App Configuration. I can't wait to see the full integration of App Configuration SDK into Spring Cloud Azure 4.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jay Lee

Jay Lee

Cloud Native Enthusiast. Java, Golang, Spring, Kubernetes.