forked from Azure-Samples/azure-spring-boot-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
App Config Sample with Entra ID (Azure-Samples#753)
* sameple with entra id * updating main readme and pom * Update pom.xml --------- Co-authored-by: Muyao Feng <[email protected]>
- Loading branch information
Showing
11 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...fig/spring-cloud-azure-starter-appconfiguration-config-entraid-sample/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
--- | ||
page_type: sample | ||
languages: | ||
- java | ||
products: | ||
- azure-app-configuration | ||
name: Refreshing Configuration Properties From App Configuration in Spring Boot Application using Entra ID | ||
description: This sample demonstrates how to refresh configuration properties from App Configuration in Spring Boot application using Entra ID. | ||
--- | ||
|
||
# Refreshing Configuration Properties From App Configuration in Spring Boot Application | ||
|
||
## Prerequisite | ||
|
||
* A [Java Development Kit (JDK)](https://docs.microsoft.com/java/azure/jdk/?view=azure-java-stable), version 8. | ||
* [Apache Maven](http://maven.apache.org/), version 3.0 or later. | ||
|
||
## How to run | ||
|
||
### Setup your App Configuration Store | ||
|
||
1. To create your Azure App Configuration store, you can use: | ||
|
||
```azurecli | ||
az appconfig create --resource-group <your-resource-group> --name <name-of-your-new-store> --sku Standard | ||
``` | ||
1. Create the test key in your new store: | ||
```azurecli | ||
az appconfig kv set --key /application/config.message --value testKey --name <name-of-your-new-store> --yes | ||
``` | ||
1. Create monitor trigger. | ||
```azurecli | ||
az appconfig kv set --key sentinel --value 1 --name <name-of-your-new-store> --yes | ||
``` | ||
This value should match the `spring.cloud.azure.appconfiguration.stores[0].monitoring.triggers[0].key` value in `bootstrap.properties`. | ||
### Setup your environment | ||
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: | ||
```cmd | ||
setx APP_CONFIGURATION_ENDPOINT "endpoint-of-your-app-configuration-store" | ||
``` | ||
|
||
If you use Windows PowerShell, run the following command: | ||
|
||
```azurepowershell | ||
$Env:APP_CONFIGURATION_ENDPOINT = "endpoint-of-your-app-configuration-store" | ||
``` | ||
|
||
If you use macOS or Linux, run the following command: | ||
|
||
```cmd | ||
export APP_CONFIGURATION_ENDPOINT='endpoint-of-your-app-configuration-store' | ||
``` | ||
|
||
### Run the application | ||
|
||
1. Build the application | ||
|
||
```console | ||
mvn clean package | ||
``` | ||
|
||
1. Run the application | ||
|
||
```console | ||
mvn spring-boot:run | ||
``` | ||
|
||
1. Go to `localhost:8080` which will display the value `testKey`. | ||
|
||
1. Update key to new value. | ||
|
||
```azurecli | ||
az appconfig kv set --key /application/config.message --value updatedTestKey --name <name-of-your-new-store> --yes | ||
``` | ||
|
||
1. Update monitor trigger, to trigger refresh. | ||
|
||
```azurecli | ||
az appconfig kv set --key sentinel --value 2 --name <name-of-your-new-store> --yes | ||
``` | ||
|
||
1. Refresh page, this will trigger the refresh update. | ||
|
||
1. After a couple seconds refresh again, this time the new value `updatedTestKey` will show. | ||
|
||
## Deploy to Azure Spring Apps | ||
|
||
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). |
41 changes: 41 additions & 0 deletions
41
...guration-config/spring-cloud-azure-starter-appconfiguration-config-entraid-sample/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?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"> | ||
<parent> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>azure-spring-boot-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>spring-cloud-azure-starter-appconfiguration-config-entraid-sample</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
<name>Azure App Configuration Refresh Sample with Entra ID</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.azure.spring</groupId> | ||
<artifactId>spring-cloud-azure-starter-appconfiguration-config</artifactId> | ||
</dependency> | ||
|
||
<!-- Adds the Ability to Push Refresh --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
21 changes: 21 additions & 0 deletions
21
...pconfiguration-config-entraid-sample/src/main/java/example/AppConfigClientCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package example; | ||
|
||
import com.azure.data.appconfiguration.ConfigurationClientBuilder; | ||
import com.azure.identity.DefaultAzureCredentialBuilder; | ||
import com.azure.security.keyvault.secrets.SecretClientBuilder; | ||
import com.azure.spring.cloud.appconfiguration.config.ConfigurationClientCustomizer; | ||
import com.azure.spring.cloud.appconfiguration.config.SecretClientCustomizer; | ||
|
||
public class AppConfigClientCustomizer implements ConfigurationClientCustomizer, SecretClientCustomizer { | ||
|
||
@Override | ||
public void customize(ConfigurationClientBuilder builder, String endpoint) { | ||
builder.credential(new DefaultAzureCredentialBuilder().build()); | ||
} | ||
|
||
@Override | ||
public void customize(SecretClientBuilder builder, String endpoint) { | ||
builder.credential(new DefaultAzureCredentialBuilder().build()); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...tarter-appconfiguration-config-entraid-sample/src/main/java/example/AppConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package example; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class AppConfiguration { | ||
|
||
@Bean | ||
public AppConfigClientCustomizer clientCustomizers() { | ||
return new AppConfigClientCustomizer(); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
...ure-starter-appconfiguration-config-entraid-sample/src/main/java/example/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package example; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
|
||
@SpringBootApplication | ||
@EnableConfigurationProperties | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...starter-appconfiguration-config-entraid-sample/src/main/java/example/HelloController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE in the project root for | ||
* license information. | ||
*/ | ||
package example; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
|
||
@RestController | ||
public class HelloController { | ||
|
||
@Autowired | ||
private MessageProperties properties; | ||
|
||
@GetMapping("") | ||
public String getMessage() throws JsonProcessingException { | ||
return properties.getMessage(); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...arter-appconfiguration-config-entraid-sample/src/main/java/example/MessageProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE in the project root for | ||
* license information. | ||
*/ | ||
package example; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@ConfigurationProperties(prefix = "config") | ||
public class MessageProperties { | ||
|
||
private String message; | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...arter-appconfiguration-config-entraid-sample/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\ | ||
example.AppConfiguration | ||
|
5 changes: 5 additions & 0 deletions
5
...re-starter-appconfiguration-config-entraid-sample/src/main/resources/bootstrap.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
spring.cloud.azure.appconfiguration.stores[0].endpoint=${CONFIG_STORE_ENDPOINT} | ||
|
||
spring.cloud.azure.appconfiguration.stores[0].monitoring.enabled=true | ||
spring.cloud.azure.appconfiguration.stores[0].monitoring.refresh-interval=5s | ||
spring.cloud.azure.appconfiguration.stores[0].monitoring.triggers[0].key=sentinel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters