Connect Azure Cache for Redis to your application in Azure Spring Apps

Note

Azure Spring Apps is the new name for the Azure Spring Cloud service. Although the service has a new name, you'll see the old name in some places for a while as we work to update assets such as screenshots, videos, and diagrams.

This article applies to: ✔️ Java ✔️ C#

This article applies to: ✔️ Basic/Standard ✔️ Enterprise

Instead of manually configuring your Spring Boot applications, you can automatically connect selected Azure services to your applications by using Azure Spring Apps. This article shows how to connect your application to Azure Cache for Redis.

Prerequisites

  • A deployed Azure Spring Apps instance
  • An Azure Cache for Redis service instance
  • The Azure Spring Apps extension for the Azure CLI

If you don't have a deployed Azure Spring Apps instance, follow the steps in the Quickstart: Deploy your first application to Azure Spring Apps.

Prepare your project

  1. Add the following dependency to your project's pom.xml file:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
    </dependency>
    
  2. Remove any spring.redis.* properties from the application.properties file

  3. Update the current deployment using az spring app update or create a new deployment using az spring app deployment create.

Connect your app to the Azure Cache for Redis

Note

By default, Service Connectors are created at the application level. To override the connections, you can create other connections again in the deployments.

  1. Use the Azure CLI to configure your Spring app to connect to a Redis database with an access key using the az spring connection create command, as shown in the following example.

    az spring connection create redis \
        --resource-group $AZURE_SPRING_APPS_RESOURCE_GROUP \
        --service $AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME \
        --app $APP_NAME \
        --deployment $DEPLOYMENT_NAME \
        --target-resource-group $REDIS_RESOURCE_GROUP \
        --server $REDIS_SERVER_NAME\
        --database $REDIS_DATABASE_NAME \
        --secret
    

    Note

    If you're using Service Connector for the first time, start by running the command az provider register --namespace Microsoft.ServiceLinker to register the Service Connector resource provider.

    If you're using Redis Enterprise, use the az spring connection create redis-enterprise command instead.

    Tip

    Run the command az spring connection list-support-types --output table to get a list of supported target services and authentication methods for Azure Spring Apps. If the az spring command isn't recognized by the system, check that you have installed the required extension by running az extension add --name spring.

  2. Alternatively, you can use the Azure portal to configure this connection by completing the following steps. The Azure portal provides the same capabilities as the Azure CLI and provides an interactive experience.

    1. Select your Azure Spring Apps instance in the Azure portal and then select Apps from the navigation menu. Choose the app you want to connect and then select Service Connector on the navigation menu.

    2. Select Create.

    3. On the Basics tab, for service type, select Cache for Redis. Choose a subscription and a Redis cache server. Fill in the Redis database name ("0" in this example) and under client type, select Java. Select Next: Authentication.

    4. On the Authentication tab, choose Connection string. Service Connector automatically retrieves the access key from your Redis database account. Select Next: Networking.

    5. On the Networking tab, select Configure firewall rules to enable access to target service, then select Review + Create.

    6. On the Review + Create tab, wait for the validation to pass and then select Create. The creation can take a few minutes to complete.

    7. Once the connection between your Spring app your Redis database has been generated, you can see it in the Service Connector page and select the unfold button to view the configured connection variables.

Next steps

In this article, you learned how to connect your application in Azure Spring Apps to Azure Cache for Redis. To learn more about connecting services to your application, see Connect to an Azure Database for MySQL instance.