Skip to content

Commit e87e261

Browse files
committed
sameple with entra id
1 parent 37cd4b0 commit e87e261

File tree

9 files changed

+244
-0
lines changed

9 files changed

+244
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- java
5+
products:
6+
- azure-app-configuration
7+
name: Refreshing Configuration Properties From App Configuration in Spring Boot Application using Entra ID
8+
description: This sample demonstrates how to refresh configuration properties from App Configuration in Spring Boot application using Entra ID.
9+
---
10+
11+
# Refreshing Configuration Properties From App Configuration in Spring Boot Application
12+
13+
## Prerequisite
14+
15+
* A [Java Development Kit (JDK)](https://docs.microsoft.com/java/azure/jdk/?view=azure-java-stable), version 8.
16+
* [Apache Maven](http://maven.apache.org/), version 3.0 or later.
17+
18+
## How to run
19+
20+
### Setup your App Configuration Store
21+
22+
1. To create your Azure App Configuration store, you can use:
23+
24+
```azurecli
25+
az appconfig create --resource-group <your-resource-group> --name <name-of-your-new-store> --sku Standard
26+
```
27+
28+
1. Create the test key in your new store:
29+
30+
```azurecli
31+
az appconfig kv set --key /application/config.message --value testKey --name <name-of-your-new-store> --yes
32+
```
33+
34+
1. Create monitor trigger.
35+
36+
```azurecli
37+
az appconfig kv set --key sentinel --value 1 --name <name-of-your-new-store> --yes
38+
```
39+
40+
This value should match the `spring.cloud.azure.appconfiguration.stores[0].monitoring.triggers[0].key` value in `bootstrap.properties`.
41+
42+
### Setup your environment
43+
44+
1. Set an environment variable named **APP_CONFIGURATION_ENDPOINT**, and set it to the endpoint to your App Configuration store. At the command line, run the following command and restart the command prompt to allow the change to take effect:
45+
46+
```cmd
47+
setx APP_CONFIGURATION_ENDPOINT "endpoint-of-your-app-configuration-store"
48+
```
49+
50+
If you use Windows PowerShell, run the following command:
51+
52+
```azurepowershell
53+
$Env:APP_CONFIGURATION_ENDPOINT = "endpoint-of-your-app-configuration-store"
54+
```
55+
56+
If you use macOS or Linux, run the following command:
57+
58+
```cmd
59+
export APP_CONFIGURATION_ENDPOINT='endpoint-of-your-app-configuration-store'
60+
```
61+
62+
### Run the application
63+
64+
1. Build the application
65+
66+
```console
67+
mvn clean package
68+
```
69+
70+
1. Run the application
71+
72+
```console
73+
mvn spring-boot:run
74+
```
75+
76+
1. Go to `localhost:8080` which will display the value `testKey`.
77+
78+
1. Update key to new value.
79+
80+
```azurecli
81+
az appconfig kv set --key /application/config.message --value updatedTestKey --name <name-of-your-new-store> --yes
82+
```
83+
84+
1. Update monitor trigger, to trigger refresh.
85+
86+
```azurecli
87+
az appconfig kv set --key sentinel --value 2 --name <name-of-your-new-store> --yes
88+
```
89+
90+
1. Refresh page, this will trigger the refresh update.
91+
92+
1. After a couple seconds refresh again, this time the new value `updatedTestKey` will show.
93+
94+
## Deploy to Azure Spring Apps
95+
96+
Now that you have the Spring Boot application running locally, it's time to move it to production. [Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/overview) makes it easy to deploy Spring Boot applications to Azure without any code changes. The service manages the infrastructure of Spring applications so developers can focus on their code. Azure Spring Apps provides lifecycle management using comprehensive monitoring and diagnostics, configuration management, service discovery, CI/CD integration, blue-green deployments, and more. To deploy your application to Azure Spring Apps, see [Deploy your first application to Azure Spring Apps](https://learn.microsoft.com/azure/spring-apps/quickstart?tabs=Azure-CLI).
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>com.azure.spring</groupId>
7+
<artifactId>azure-spring-boot-samples</artifactId>
8+
<version>1.0.0</version>
9+
<relativePath>../../../pom.xml</relativePath>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
14+
<artifactId>spring-cloud-azure-starter-appconfiguration-config-entraid-sample</artifactId>
15+
<version>1.0.0</version>
16+
<packaging>jar</packaging>
17+
<name>Azure App Configuration Refresh Sample with Entra ID</name>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.azure.spring</groupId>
30+
<artifactId>spring-cloud-azure-starter-appconfiguration-config</artifactId>
31+
</dependency>
32+
33+
<!-- Adds the Ability to Push Refresh -->
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-actuator</artifactId>
37+
</dependency>
38+
</dependencies>
39+
40+
41+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package example;
2+
3+
import com.azure.data.appconfiguration.ConfigurationClientBuilder;
4+
import com.azure.identity.DefaultAzureCredentialBuilder;
5+
import com.azure.security.keyvault.secrets.SecretClientBuilder;
6+
import com.azure.spring.cloud.appconfiguration.config.ConfigurationClientCustomizer;
7+
import com.azure.spring.cloud.appconfiguration.config.SecretClientCustomizer;
8+
9+
public class AppConfigClientCustomizer implements ConfigurationClientCustomizer, SecretClientCustomizer {
10+
11+
@Override
12+
public void customize(ConfigurationClientBuilder builder, String endpoint) {
13+
builder.credential(new DefaultAzureCredentialBuilder().build());
14+
}
15+
16+
@Override
17+
public void customize(SecretClientBuilder builder, String endpoint) {
18+
builder.credential(new DefaultAzureCredentialBuilder().build());
19+
}
20+
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package example;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
public class AppConfiguration {
8+
9+
@Bean
10+
public AppConfigClientCustomizer clientCustomizers() {
11+
return new AppConfigClientCustomizer();
12+
}
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package example;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
6+
7+
@SpringBootApplication
8+
@EnableConfigurationProperties
9+
public class Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Application.class, args);
13+
}
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE in the project root for
4+
* license information.
5+
*/
6+
package example;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import com.fasterxml.jackson.core.JsonProcessingException;
13+
14+
@RestController
15+
public class HelloController {
16+
17+
@Autowired
18+
private MessageProperties properties;
19+
20+
@GetMapping("")
21+
public String getMessage() throws JsonProcessingException {
22+
return properties.getMessage();
23+
}
24+
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE in the project root for
4+
* license information.
5+
*/
6+
package example;
7+
8+
import org.springframework.boot.context.properties.ConfigurationProperties;
9+
import org.springframework.context.annotation.Configuration;
10+
11+
@Configuration
12+
@ConfigurationProperties(prefix = "config")
13+
public class MessageProperties {
14+
15+
private String message;
16+
17+
public String getMessage() {
18+
return message;
19+
}
20+
21+
public void setMessage(String message) {
22+
this.message = message;
23+
}
24+
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
2+
example.AppConfiguration
3+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
spring.cloud.azure.appconfiguration.stores[0].endpoint=${CONFIG_STORE_ENDPOINT}
2+
3+
spring.cloud.azure.appconfiguration.stores[0].monitoring.enabled=true
4+
spring.cloud.azure.appconfiguration.stores[0].monitoring.refresh-interval=5s
5+
spring.cloud.azure.appconfiguration.stores[0].monitoring.triggers[0].key=sentinel

0 commit comments

Comments
 (0)