Quickstart: Integrate with Azure Database for PostgreSQL and Azure Cache for Redis

Note

The Basic, Standard, and Enterprise plans will be deprecated starting from mid-March, 2025, with a 3 year retirement period. We recommend transitioning to Azure Container Apps. For more information, see the Azure Spring Apps retirement announcement.

The Standard consumption and dedicated plan will be deprecated starting September 30, 2024, with a complete shutdown after six months. We recommend transitioning to Azure Container Apps. For more information, see Migrate Azure Spring Apps Standard consumption and dedicated plan to Azure Container Apps.

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

This quickstart shows you how to provision and prepare an Azure Database for PostgreSQL and an Azure Cache for Redis to be used with apps running in the Azure Spring Apps Enterprise plan.

This article uses these services for demonstration purposes. You can connect your application to any backing service of your choice by using instructions similar to the ones in the Create Service Connectors section later in this article.

Prerequisites

Provision services

To add persistence to the application, create an Azure Cache for Redis and an Azure Database for PostgreSQL Flexible Server.

The following steps describe how to provision an Azure Cache for Redis instance and an Azure Database for PostgreSQL Flexible Server by using the Azure CLI.

  1. Create variables to hold the resource names by using the following commands. Be sure to replace the placeholders with your own values.

    export REGION=<region>
    export RESOURCE_GROUP=<resource-group-name>
    export REDIS_CACHE_NAME=<redis-cache-name>
    export POSTGRES_SERVER_NAME=<postgres-server-name>
    export POSTGRES_USERNAME=<postgres-username>
    export POSTGRES_PASSWORD=<postgres-password>
    export AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME=<Azure-Spring-Apps-service-instance-name>
    
  2. Use the following command to create an instance of Azure Cache for Redis:

    az redis create \
        --resource-group ${RESOURCE_GROUP} \
        --name ${REDIS_CACHE_NAME} \
        --location ${REGION} \
        --sku Basic \
        --vm-size c0
    

    Note

    Redis Cache creation takes approximately 20 minutes.

  3. Use the following command to create an Azure Database for PostgreSQL Flexible Server instance:

    az postgres flexible-server create \
        --resource-group ${RESOURCE_GROUP} \
        --name ${POSTGRES_SERVER_NAME} \
        --location ${REGION} \
        --admin-user ${POSTGRES_USERNAME} \
        --admin-password ${POSTGRES_PASSWORD} \
        --yes
    
  4. Use the following command to allow connections from other Azure Services to the newly created Flexible Server:

    az postgres flexible-server firewall-rule create \
        --rule-name allAzureIPs \
        --name ${POSTGRES_SERVER_NAME} \
        --resource-group ${RESOURCE_GROUP} \
        --start-ip-address 0.0.0.0 \
        --end-ip-address 0.0.0.0
    
  5. Use the following command to enable the uuid-ossp extension for the newly created Flexible Server:

    az postgres flexible-server parameter set \
        --resource-group ${RESOURCE_GROUP} \
        --name azure.extensions \
        --value uuid-ossp \
        --server-name ${POSTGRES_SERVER_NAME}
    
  6. Use the following command to create a database for the Order Service application:

    az postgres flexible-server db create \
        --resource-group ${RESOURCE_GROUP} \
        --server-name ${POSTGRES_SERVER_NAME} \
        --database-name acmefit_order
    
  7. Use the following command to create a database for the Catalog Service application:

    az postgres flexible-server db create \
        --resource-group ${RESOURCE_GROUP} \
        --server-name ${POSTGRES_SERVER_NAME} \
        --database-name acmefit_catalog
    

Create Service Connectors

The following steps show how to bind applications running in the Azure Spring Apps Enterprise plan to other Azure services by using Service Connectors.

  1. Use the following command to create a service connector to Azure Database for PostgreSQL for the Order Service application:

    az spring connection create postgres-flexible \
        --resource-group ${RESOURCE_GROUP} \
        --target-resource-group ${RESOURCE_GROUP} \
        --connection order_service_db \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --app order-service \
        --deployment default \
        --server ${POSTGRES_SERVER_NAME} \
        --database acmefit_order \
        --secret name=${POSTGRES_USERNAME} secret=${POSTGRES_PASSWORD} \
        --client-type dotnet
    
  2. Use the following command to create a service connector to Azure Database for PostgreSQL for the Catalog Service application:

    az spring connection create postgres-flexible \
        --resource-group ${RESOURCE_GROUP} \
        --target-resource-group ${RESOURCE_GROUP} \
        --connection catalog_service_db \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --app catalog-service \
        --deployment default \
        --server ${POSTGRES_SERVER_NAME} \
        --database acmefit_catalog \
        --secret name=${POSTGRES_USERNAME} secret=${POSTGRES_PASSWORD} \
        --client-type springboot
    
  3. Use the following command to create a service connector to Azure Cache for Redis for the Cart Service application:

    az spring connection create redis \
        --resource-group ${RESOURCE_GROUP} \
        --target-resource-group ${RESOURCE_GROUP} \
        --connection cart_service_cache \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --app cart-service \
        --deployment default \
        --server ${REDIS_CACHE_NAME} \
        --database 0 \
        --client-type java
    
  4. Use the following command to reload the Catalog Service application to load the new connection properties:

    az spring app restart \
        --resource-group ${RESOURCE_GROUP} \
        --name catalog-service \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME}
    
  5. Use the following command to retrieve the database connection information:

    export POSTGRES_CONNECTION_STR=$(az spring connection show \
        --resource-group ${RESOURCE_GROUP} \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --deployment default \
        --connection order_service_db \
        --app order-service \
        | jq '.configurations[0].value' -r)
    

    Note

    If you get an SSL verification exception with Nofsql 6.0, be sure to change the SSL mode from Require to VerifyFull. For more information, see the Npgsql 6.0 Release Notes.

  6. Use the following command to update the Order Service application:

    az spring app update \
        --resource-group ${RESOURCE_GROUP} \
        --name order-service \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --env "DatabaseProvider=Postgres" "ConnectionStrings__OrderContext=${POSTGRES_CONNECTION_STR}"
    
  7. Use the following commands to retrieve Redis connection information and update the Cart Service application:

    export REDIS_CONN_STR=$(az spring connection show \
        --resource-group ${RESOURCE_GROUP} \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --deployment default \
        --app cart-service \
        --connection cart_service_cache | jq -r '.configurations[0].value')
    
    export GATEWAY_URL=$(az spring gateway show \
        --resource-group ${RESOURCE_GROUP} \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} | jq -r '.properties.url')
    
    az spring app update \
        --resource-group ${RESOURCE_GROUP} \
        --name cart-service \
        --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} \
        --env "CART_PORT=8080" "REDIS_CONNECTIONSTRING=${REDIS_CONN_STR}" "AUTH_URL=https://${GATEWAY_URL}"
    

Access the application

Retrieve the URL for Spring Cloud Gateway and explore the updated application. You can use the output from the following command to explore the application:

export GATEWAY_URL=$(az spring gateway show \
    --resource-group ${RESOURCE_GROUP} \
    --service ${AZURE_SPRING_APPS_SERVICE_INSTANCE_NAME} | jq -r '.properties.url')

echo "https://${GATEWAY_URL}"

Clean up resources

If you plan to continue working with subsequent quickstarts and tutorials, you might want to leave these resources in place. When no longer needed, delete the resource group, which deletes the resources in the resource group. To delete the resource group by using Azure CLI, use the following commands:

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
az group delete --name $resourceGroupName &&
echo "Press [ENTER] to continue ..."

Next steps

Continue on to any of the following optional quickstarts: