Skip to content

Commit

Permalink
App Config Sample with Entra ID (#753)
Browse files Browse the repository at this point in the history
* sameple with entra id

* updating main readme and pom

* Update pom.xml

---------

Co-authored-by: Muyao Feng <92105726+Netyyyy@users.noreply.github.com>
  • Loading branch information
mrm9084 and Netyyyy authored Aug 21, 2024
1 parent a04bfed commit a56e964
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
| [spring-cloud-azure-feature-management-sample](appconfiguration/spring-cloud-azure-feature-management/spring-cloud-azure-feature-management-sample) |||
| [spring-cloud-azure-feature-management-web-sample](appconfiguration/spring-cloud-azure-feature-management-web) |||
| [spring-cloud-azure-starter-appconfiguration-config-sample](appconfiguration/spring-cloud-azure-starter-appconfiguration-config/spring-cloud-azure-starter-appconfiguration-config-sample) |||
| [spring-cloud-azure-starter-appconfiguration-config-entraid-sample](appconfiguration/spring-cloud-azure-starter-appconfiguration-config/spring-cloud-azure-starter-appconfiguration-config-entraid-sample) |||
| [appconfiguration-client](appconfiguration/spring-cloud-azure-starter-appconfiguration/appconfiguration-client) |||

### Azure Cache
Expand Down
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).
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>
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());
}

}
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();
}

}
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);
}
}
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();
}

}
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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
example.AppConfiguration

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
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<module>appconfiguration/spring-cloud-azure-feature-management-web/spring2samples/spring-cloud-azure-targeting-filter-web-sample</module>
<module>appconfiguration/spring-cloud-azure-feature-management/spring-cloud-azure-feature-management-sample</module>
<module>appconfiguration/spring-cloud-azure-starter-appconfiguration-config/spring-cloud-azure-starter-appconfiguration-config-sample</module>
<module>appconfiguration/spring-cloud-azure-starter-appconfiguration-config/spring-cloud-azure-starter-appconfiguration-config-entraid-sample</module>
<module>appconfiguration/spring-cloud-azure-starter-appconfiguration/appconfiguration-client</module>
<module>cache/spring2-sample/spring-cloud-azure-starter/spring-cloud-azure-sample-cache</module>
<module>cache/spring2-sample/spring-cloud-azure-starter/spring-cloud-azure-sample-cache-passwordless</module>
Expand Down Expand Up @@ -160,6 +161,7 @@
<module>appconfiguration/spring-cloud-azure-feature-management-web/spring3samples/spring-cloud-azure-targeting-filter-web-sample</module>
<module>appconfiguration/spring-cloud-azure-feature-management/spring-cloud-azure-feature-management-sample</module>
<module>appconfiguration/spring-cloud-azure-starter-appconfiguration-config/spring-cloud-azure-starter-appconfiguration-config-sample</module>
<module>appconfiguration/spring-cloud-azure-starter-appconfiguration-config/spring-cloud-azure-starter-appconfiguration-config-entraid-sample</module>
<module>appconfiguration/spring-cloud-azure-starter-appconfiguration/appconfiguration-client</module>
<module>cache/spring3-sample/spring-cloud-azure-redis-sample</module>
<module>cache/spring3-sample/spring-cloud-azure-redis-sample-passwordless</module>
Expand Down

0 comments on commit a56e964

Please sign in to comment.