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
- An Azure account with an active subscription. Create an account for free.
- Understand and fulfill the Requirements section of Enterprise plan in Azure Marketplace.
- The Azure CLI version 2.45.0 or higher.
- Git.
- jq
-
The Azure Spring Apps Enterprise plan extension. Use the following command to remove previous versions and install the latest Enterprise plan extension. If you previously installed the
spring-cloud
extension, uninstall it to avoid configuration and version mismatches.az extension add --upgrade --name spring az extension remove --name spring-cloud
- Complete the steps in Build and deploy apps to Azure Spring Apps using the Enterprise plan.
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.
Note
Microsoft recommends using the most secure authentication flow available. The authentication flow described in this procedure, such as for databases, caches, messaging, or AI services, requires a very high degree of trust in the application and carries risks not present in other flows. Use this flow only when more secure options, like managed identities for passwordless or keyless connections, are not viable. For local machine operations, prefer user identities for passwordless or keyless connections.
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>
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.
Use the following command to create an Azure Database for PostgreSQL Flexible Server instance:
Note
Microsoft recommends using the most secure authentication flow available. The authentication flow described in this procedure, such as for databases, caches, messaging, or AI services, requires a very high degree of trust in the application and carries risks not present in other flows. Use this flow only when more secure options, like managed identities for passwordless or keyless connections, are not viable. For local machine operations, prefer user identities for passwordless or keyless connections.
az postgres flexible-server create \ --resource-group ${RESOURCE_GROUP} \ --name ${POSTGRES_SERVER_NAME} \ --location ${REGION} \ --admin-user ${POSTGRES_USERNAME} \ --admin-password ${POSTGRES_PASSWORD} \ --yes
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
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}
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
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.
Use the following command to create a service connector to Azure Database for PostgreSQL for the Order Service application:
Note
Microsoft recommends using the most secure authentication flow available. The authentication flow described in this procedure, such as for databases, caches, messaging, or AI services, requires a very high degree of trust in the application and carries risks not present in other flows. Use this flow only when more secure options, like managed identities for passwordless or keyless connections, are not viable. For local machine operations, prefer user identities for passwordless or keyless connections.
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
Use the following command to create a service connector to Azure Database for PostgreSQL for the Catalog Service application:
Note
Microsoft recommends using the most secure authentication flow available. The authentication flow described in this procedure, such as for databases, caches, messaging, or AI services, requires a very high degree of trust in the application and carries risks not present in other flows. Use this flow only when more secure options, like managed identities for passwordless or keyless connections, are not viable. For local machine operations, prefer user identities for passwordless or keyless connections.
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
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
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}
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
toVerifyFull
. For more information, see the Npgsql 6.0 Release Notes.Use the following command to update the Order Service application:
Note
Microsoft recommends using the most secure authentication flow available. The authentication flow described in this procedure, such as for databases, caches, messaging, or AI services, requires a very high degree of trust in the application and carries risks not present in other flows. Use this flow only when more secure options, like managed identities for passwordless or keyless connections, are not viable. For local machine operations, prefer user identities for passwordless or keyless connections.
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}"
Use the following commands to retrieve Redis connection information and update the Cart Service application:
Note
Microsoft recommends using the most secure authentication flow available. The authentication flow described in this procedure, such as for databases, caches, messaging, or AI services, requires a very high degree of trust in the application and carries risks not present in other flows. Use this flow only when more secure options, like managed identities for passwordless or keyless connections, are not viable. For local machine operations, prefer user identities for passwordless or keyless connections.
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: