Edit

Share via


Quickstart: Create a Java Quarkus app with Azure App Configuration

In this quickstart, you incorporate Azure App Configuration into a Java Quarkus app to centralize storage and management of application settings separate from your code.

Prerequisites

Add a key-value pair

Add the following key-value pair to the App Configuration store. For more information about how to add key-value pairs to a store using the Azure CLI, see Create a key-value.

Key Value
/application/config.message Hello

Create a Quarkus app

In this section, you create a Quarkus app with the Azure App Configuration extension and the REST extension. The REST extension is used to create a REST service that returns the configuration value from the App Configuration store. For more information, see Quarkus Azure App Configuration.

Create a Quarkus app by using the following command:

mvn io.quarkus.platform:quarkus-maven-plugin:create \
    -DprojectGroupId=com.example \
    -DprojectArtifactId=quarkus-app-configuration \
    -Dextensions='io.quarkiverse.azureservices:quarkus-azure-app-configuration,rest' \
    -DjavaVersion="17"

The app is created in a new directory named quarkus-app-configuration. Here's the structure of the app:

quarkus-app-configuration
├── src
│   ├── main/java/com/example
│   │   ├── GreetingResource.java
│   └── test/java/com/example
│       └── GreetingResourceTest.java
├── pom.xml

The pom.xml file contains the dependencies for the app, including the Azure App Configuration extension and the REST extension. The following XML is a typical example:

<dependency>
    <groupId>io.quarkiverse.azureservices</groupId>
    <artifactId>quarkus-azure-app-configuration</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-rest</artifactId>
</dependency>

The GreetingResource.java file contains the REST service. You modify this file to return the configuration value from the App Configuration store later.

The GreetingResourceTest.java file contains the unit test for the REST service. You modify this file to assert that the value returned from the REST service equals the value in the App Configuration store later.

Connect to an App Configuration store

Now that you have an App Configuration store and a Quarkus app with the Azure App Configuration extension, you can connect the app to the store.

Code the application

Configure the application by using the following steps:

  1. Open the GreetingResource.java file in the src/main/java/com/example directory, and replace the contents with the following code:

    package com.example;
    
    import jakarta.ws.rs.GET;
    import jakarta.ws.rs.Path;
    import jakarta.ws.rs.Produces;
    import jakarta.ws.rs.core.MediaType;
    
    import org.eclipse.microprofile.config.inject.ConfigProperty;
    
    @Path("/hello")
    public class GreetingResource {
    
        @ConfigProperty(name = "/application/config.message")
        String value;
    
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String hello() {
            return value;
        }
    }
    

    The modified code uses the MicroProfile Config @ConfigProperty annotation to inject the value of the key /application/config.message from the App Configuration store into the value field, and returns the value in the REST service.

  2. Open the auto-generated unit test, which is in the GreetingResourceTest.java file in the src/test/java/com/example directory. Replace the contents of the file with the following code:

    package com.example;
    
    import io.quarkus.test.junit.QuarkusTest;
    import org.junit.jupiter.api.Test;
    
    import javax.inject.Inject;
    
    import static io.restassured.RestAssured.given;
    import static org.hamcrest.CoreMatchers.is;
    
    @QuarkusTest
    public class GreetingResourceTest {
        @Test
        public void testHelloEndpoint() {
            given()
                .when().get("/hello")
                .then()
                .statusCode(200)
                .body(is("Hello"));
        }
    }
    

    The modified code asserts that the value returned from the REST service equals the value in the App Configuration store. This value is Hello for the key /application/config.message that you added earlier.

These code changes to the application are all required. Before running the application, you need to configure the authentication and connection to the App Configuration store.

Configure authentication to the App Configuration store

Besides authentication with access keys, the Quarkus Azure App Configuration extension supports authentication with Microsoft Entra ID using DefaultAzureCredential from the Azure Identity client library.

You use Microsoft Entra ID for authentication in this quickstart. For more information, see DefaultAzureCredential in Azure Identity client library for Java.

Configure authentication to the App Configuration store by using the following steps:

  1. Set environment variables by using the following commands, replacing the <...> placeholders with the values you created previously:

    export RESOURCE_GROUP_NAME="<resource-group-name>"
    export APP_CONFIG_NAME="<app-configuration-store-name>"
    
  2. Use the following commands to assign the App Configuration Data Reader role to the signed-in user:

    # Retrieve the app configuration resource ID
    export APP_CONFIGURATION_RESOURCE_ID=$(az appconfig show \
        --resource-group $RESOURCE_GROUP_NAME \
        --name "${APP_CONFIG_NAME}" \
        --query 'id' \
        --output tsv)
    # Assign the "App Configuration Data Reader" role to the current signed-in identity
    az role assignment create \
        --assignee $(az ad signed-in-user show --query 'id' --output tsv) \
        --role "App Configuration Data Reader" \
        --scope $APP_CONFIGURATION_RESOURCE_ID
    
  3. Export the endpoint of the App Configuration store to an environment variable named QUARKUS_AZURE_APP_CONFIGURATION_ENDPOINT by using the following command:

    export QUARKUS_AZURE_APP_CONFIGURATION_ENDPOINT=$(az appconfig show \
        --resource-group "${RESOURCE_GROUP_NAME}" \
        --name "${APP_CONFIG_NAME}" \
        --query endpoint \
        --output tsv)
    

    Note

    The value of the environment variable QUARKUS_AZURE_APP_CONFIGURATION_ENDPOINT is used in the configuration property quarkus.azure.app.configuration.endpoint of the Quarkus Azure App Configuration extension in order to set up the connection to the Azure App Configuration store. Alternatively, you can configure this property in the application.properties file in the src/main/resources directory by setting it directly, as in the following example. Be sure to replace <your-app-configuration-store-endpoint> with your actual value.

    quarkus.azure.app.configuration.endpoint=<your-app-configuration-store-endpoint>
    

Build and run the app locally

Build and run the app locally by using the following steps:

  1. Use the following commands to build and run the app in JVM mode:

    # Build, test and package the app
    mvn clean package
    
    # Run the app
    java -jar ./target/quarkus-app/quarkus-run.jar
    

    Note

    As an alternative, you can run the sample in native mode. To use native mode, you need to have GraalVM installed, or use a builder image to build the native executable. For more information, see Building a Native Executable. This quickstart uses Docker as container runtime to build a Linux native executable. If you don't have Docker installed, you can download it from the Docker website.

    Use the following commands to build and execute the native executable in a Linux environment:

    mvn package -Dnative -Dquarkus.native.container-build
    ./target/storage-blob-1.0.0-SNAPSHOT-runner
    
  2. Test your application by using the following command:

    curl localhost:8080/hello
    

    You should see Hello in the output, because Hello is the value of the /application/config.message key you added to the App Configuration store.

  3. Press Control+C to stop the application.

Clean up resources

To avoid Azure charges, you should clean up unneeded resources. When you no longer need the App Configuration store, use the following command to remove the resource group and all related resources:

az group delete --name $RESOURCE_GROUP_NAME --yes --no-wait

See also