|
| 1 | +--- |
| 2 | +title: Quickstart - Deploy event-driven application to Azure Spring Apps with the Standard consumption plan |
| 3 | +description: Learn how to deploy an event-driven application to Azure Spring Apps with the Standard consumption plan. |
| 4 | +author: karlerickson |
| 5 | +ms.service: spring-apps |
| 6 | +ms.topic: quickstart |
| 7 | +ms.date: 03/17/2023 |
| 8 | +ms.author: rujche |
| 9 | +ms.custom: devx-track-java, devx-track-azurecli, mode-other, event-tier1-build-2022, engagement-fy23 |
| 10 | +--- |
| 11 | + |
| 12 | +# Quickstart: Deploy an event-driven application to Azure Spring Apps with the Standard consumption plan |
| 13 | + |
| 14 | +> [!NOTE] |
| 15 | +> The first 50 vCPU hours and 100 GB hours of memory are free each month. For more information, see [Price Reduction - Azure Spring Apps does more, costs less!](https://techcommunity.microsoft.com/t5/apps-on-azure-blog/price-reduction-azure-spring-apps-does-more-costs-less/ba-p/3614058) on the [Apps on Azure Blog](https://techcommunity.microsoft.com/t5/apps-on-azure-blog/bg-p/AppsonAzureBlog). |
| 16 | +
|
| 17 | +> [!NOTE] |
| 18 | +> 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. |
| 19 | +
|
| 20 | +**This article applies to:** ✔️ Standard consumption (Preview) ❌ Basic/Standard ❌ Enterprise |
| 21 | + |
| 22 | +This article explains how to deploy a Spring Boot event-driven application to Azure Spring Apps with the Standard consumption plan. |
| 23 | + |
| 24 | +The sample project is an event-driven application that subscribes to a [Service Bus queue](/azure/service-bus-messaging/service-bus-queues-topics-subscriptions#queues) named `lower-case`, and then handles the message and sends another message to another queue named `upper-case`. To make the app simple, message processing just converts the message to uppercase. The following diagram depicts this process: |
| 25 | + |
| 26 | +:::image type="content" source="media/quickstart-deploy-event-driven-app-standard-consumption/diagram.png" alt-text="Diagram of Spring event-driven app architecture." lightbox="media/quickstart-deploy-event-driven-app-standard-consumption/diagram.png"::: |
| 27 | + |
| 28 | +## Prerequisites |
| 29 | + |
| 30 | +- An Azure subscription. If you don't have a subscription, create a [free account](https://azure.microsoft.com/free/) before you begin. |
| 31 | +- [Azure CLI](/cli/azure/install-azure-cli). Version 2.45.0 or greater. |
| 32 | +- [Git](https://git-scm.com/downloads). |
| 33 | +- [Java Development Kit (JDK)](/java/azure/jdk/), version 17. |
| 34 | + |
| 35 | +## Clone and build the sample project |
| 36 | + |
| 37 | +Use the following steps to prepare the sample locally. |
| 38 | + |
| 39 | +1. The sample project is ready on GitHub. Clone sample project by using the following command: |
| 40 | + |
| 41 | + ```shell |
| 42 | + git clone https://github.com/Azure-Samples/ASA-Samples-Event-Driven-Application.git |
| 43 | + ``` |
| 44 | + |
| 45 | +1. Build the sample project by using the following commands: |
| 46 | + |
| 47 | + ```shell |
| 48 | + cd ASA-Samples-Event-Driven-Application |
| 49 | + ./mvnw clean package -DskipTests |
| 50 | + ``` |
| 51 | + |
| 52 | +## Prepare the cloud environment |
| 53 | + |
| 54 | +The main resources you need to run this sample is an Azure Spring Apps instance and an Azure Service Bus instance. Use the following steps to create these resources. |
| 55 | + |
| 56 | +1. Use the following commands to create variables for the names of your resources and for other settings as needed. Resource names in Azure must be unique. |
| 57 | + |
| 58 | + ```azurecli |
| 59 | + RESOURCE_GROUP=<event-driven-app-resource-group-name> |
| 60 | + LOCATION=<desired-region> |
| 61 | + SERVICE_BUS_NAME_SPACE=<event-driven-app-service-bus-namespace> |
| 62 | + AZURE_CONTAINER_APPS_ENVIRONMENT=<Azure-Container-Apps-environment-name> |
| 63 | + AZURE_SPRING_APPS_INSTANCE=<Azure-Spring-Apps-instance-name> |
| 64 | + APP_NAME=<event-driven-app-name> |
| 65 | + ``` |
| 66 | + |
| 67 | +1. Sign in to Azure by using the following command: |
| 68 | + |
| 69 | + ```azurecli |
| 70 | + az login |
| 71 | + ``` |
| 72 | + |
| 73 | +1. Set the default location by using the following command: |
| 74 | + |
| 75 | + ```azurecli |
| 76 | + az configure --defaults location=${LOCATION} |
| 77 | + ``` |
| 78 | + |
| 79 | +1. Set your default subscription. First, list all available subscriptions: |
| 80 | + |
| 81 | + ```azurecli |
| 82 | + az account list --output table |
| 83 | + ``` |
| 84 | + |
| 85 | +1. Determine the ID of the subscription you want to set and use it with the following command to set your default subscription. |
| 86 | + |
| 87 | + ```azurecli |
| 88 | + az account set --subscription <subscription-ID> |
| 89 | + ``` |
| 90 | + |
| 91 | +1. Create a resource group by using the following command: |
| 92 | + |
| 93 | + ```azurecli |
| 94 | + az group create --resource-group ${RESOURCE_GROUP} |
| 95 | + ``` |
| 96 | + |
| 97 | +1. Use the following command to set the newly created resource group as the default resource group. |
| 98 | + |
| 99 | + ```azurecli |
| 100 | + az configure --defaults group=${RESOURCE_GROUP} |
| 101 | + ``` |
| 102 | + |
| 103 | +## Create a Service Bus instance |
| 104 | + |
| 105 | +Create a Service Bus instance by using the following steps. |
| 106 | + |
| 107 | +1. Use the following command to create a Service Bus namespace. |
| 108 | + |
| 109 | + ```azurecli |
| 110 | + az servicebus namespace create --name ${SERVICE_BUS_NAME_SPACE} |
| 111 | + ``` |
| 112 | + |
| 113 | +1. Use the following commands to create two queues named `lower-case` and `upper-case`. |
| 114 | + |
| 115 | + ```azurecli |
| 116 | + az servicebus queue create \ |
| 117 | + --namespace-name ${SERVICE_BUS_NAME_SPACE} \ |
| 118 | + --name lower-case |
| 119 | + az servicebus queue create \ |
| 120 | + --namespace-name ${SERVICE_BUS_NAME_SPACE} \ |
| 121 | + --name upper-case |
| 122 | + ``` |
| 123 | + |
| 124 | +## Create an Azure Container Apps environment |
| 125 | + |
| 126 | +The Azure Container Apps environment creates a secure boundary around a group of applications. Apps deployed to the same environment are deployed in the same virtual network and write logs to the same Log Analytics workspace. |
| 127 | + |
| 128 | +Use the following steps to create the environment: |
| 129 | + |
| 130 | +1. Install the Azure Container Apps extension for the CLI by using the following command: |
| 131 | + |
| 132 | + ```azurecli |
| 133 | + az extension add --name containerapp --upgrade |
| 134 | + ``` |
| 135 | + |
| 136 | +1. Register the `Microsoft.App` namespace by using the following command: |
| 137 | + |
| 138 | + ```azurecli |
| 139 | + az provider register --namespace Microsoft.App |
| 140 | + ``` |
| 141 | + |
| 142 | +1. If you haven't previously used the Azure Monitor Log Analytics workspace, register the `Microsoft.OperationalInsights` provider by using the following command: |
| 143 | + |
| 144 | + ```azurecli |
| 145 | + az provider register --namespace Microsoft.OperationalInsights |
| 146 | + ``` |
| 147 | + |
| 148 | +1. Create the environment by using the following command: |
| 149 | + |
| 150 | + ```azurecli |
| 151 | + az containerapp env create --name ${AZURE_CONTAINER_APPS_ENVIRONMENT} |
| 152 | + ``` |
| 153 | + |
| 154 | +## Create the Azure Spring Apps instance |
| 155 | + |
| 156 | +An Azure Spring Apps Consumption plan instance hosts the Spring event-driven app. Use the following steps to create the service instance and then create an app inside the instance. |
| 157 | + |
| 158 | +1. Install the Spring extension designed for `StandardGen2` Azure Spring Apps by using the following command: |
| 159 | + |
| 160 | + ```azurecli |
| 161 | + az extension remove --name spring && \ |
| 162 | + az extension add \ |
| 163 | + --source https://ascprivatecli.blob.core.windows.net/cli-extension/spring-1.8.0-py3-none-any.whl \ |
| 164 | + --yes |
| 165 | + ``` |
| 166 | + |
| 167 | +1. Register the `Microsoft.AppPlatform` provider for the Azure Spring Apps by using the following command: |
| 168 | + |
| 169 | + ```azurecli |
| 170 | + az provider register --namespace Microsoft.AppPlatform |
| 171 | + ``` |
| 172 | + |
| 173 | +1. Get the Azure Container Apps environment resource ID by using the following command: |
| 174 | + |
| 175 | + ```azurecli |
| 176 | + MANAGED_ENV_RESOURCE_ID=$(az containerapp env show \ |
| 177 | + --name ${AZURE_CONTAINER_APPS_ENVIRONMENT} \ |
| 178 | + --query id \ |
| 179 | + --output tsv) |
| 180 | + ``` |
| 181 | + |
| 182 | +1. Use the following command to create your Azure Spring Apps instance, specifying the resource ID of the Azure Container Apps environment you created. |
| 183 | + |
| 184 | + ```azurecli |
| 185 | + az spring create \ |
| 186 | + --name ${AZURE_SPRING_APPS_INSTANCE} \ |
| 187 | + --managed-environment ${MANAGED_ENV_RESOURCE_ID} \ |
| 188 | + --sku standardGen2 |
| 189 | + ``` |
| 190 | + |
| 191 | +1. Create an app in the Azure Spring Apps instance by using the following command: |
| 192 | + |
| 193 | + ```azurecli |
| 194 | + az spring app create \ |
| 195 | + --service ${AZURE_SPRING_APPS_INSTANCE} \ |
| 196 | + --name ${APP_NAME} \ |
| 197 | + --cpu 1 \ |
| 198 | + --memory 2 \ |
| 199 | + --instance-count 2 \ |
| 200 | + --runtime-version Java_17 \ |
| 201 | + --assign-endpoint true |
| 202 | + ``` |
| 203 | + |
| 204 | +## Bind the Service Bus to Azure Spring Apps and deploy the app |
| 205 | + |
| 206 | +Now both the Service Bus and the app in Azure Spring Apps have been created, but the app can't connect to the Service Bus. Use the following steps to enable the app to connect to the Service Bus, and then deploy the app. |
| 207 | + |
| 208 | +1. Get the Service Bus's connection string by using the following command: |
| 209 | + |
| 210 | + ```azurecli |
| 211 | + SERVICE_BUS_CONNECTION_STRING=$(az servicebus namespace authorization-rule keys list \ |
| 212 | + --namespace-name ${SERVICE_BUS_NAME_SPACE} \ |
| 213 | + --name RootManageSharedAccessKey \ |
| 214 | + --query primaryConnectionString \ |
| 215 | + --output tsv) |
| 216 | + ``` |
| 217 | + |
| 218 | +1. Use the following command to provide the connection string to the app through an environment variable. |
| 219 | + |
| 220 | + ```azurecli |
| 221 | + az spring app update \ |
| 222 | + --service ${AZURE_SPRING_APPS_INSTANCE} \ |
| 223 | + --name ${APP_NAME} \ |
| 224 | + --env SERVICE_BUS_CONNECTION_STRING=${SERVICE_BUS_CONNECTION_STRING} |
| 225 | + ``` |
| 226 | + |
| 227 | +1. Now the cloud environment is ready. Deploy the app by using the following command. |
| 228 | + |
| 229 | + ```azurecli |
| 230 | + az spring app deploy \ |
| 231 | + --service ${AZURE_SPRING_APPS_INSTANCE} \ |
| 232 | + --name ${APP_NAME} \ |
| 233 | + --artifact-path target/simple-event-driven-app-0.0.1-SNAPSHOT.jar |
| 234 | + ``` |
| 235 | + |
| 236 | +## Validate the event-driven app |
| 237 | + |
| 238 | +Use the following steps to confirm that the event-driven app works correctly. You can validate the app by sending a message to the `lower-case` queue, then confirming that there's a message in the `upper-case` queue. |
| 239 | + |
| 240 | +1. Send a message to `lower-case` queue with Service Bus Explorer. For more information, see the [Send a message to a queue or topic](../service-bus-messaging/explorer.md#send-a-message-to-a-queue-or-topic) section of [Use Service Bus Explorer to run data operations on Service Bus](../service-bus-messaging/explorer.md). |
| 241 | + |
| 242 | +1. Confirm that there's a new message sent to the `upper-case` queue. For more information, see the [Peek a message](../service-bus-messaging/explorer.md#peek-a-message) section of [Use Service Bus Explorer to run data operations on Service Bus](../service-bus-messaging/explorer.md). |
| 243 | + |
| 244 | +## Clean up resources |
| 245 | + |
| 246 | +Be sure to delete the resources you created in this article when you no longer need them. To delete the resources, just delete the resource group that contains them. You can delete the resource group using the Azure portal. Alternately, to delete the resource group by using Azure CLI, use the following commands: |
| 247 | + |
| 248 | +```azurecli |
| 249 | +echo "Enter the Resource Group name:" && |
| 250 | +read resourceGroupName && |
| 251 | +az group delete --name $resourceGroupName && |
| 252 | +echo "Press [ENTER] to continue ..." |
| 253 | +``` |
| 254 | + |
| 255 | +## Next steps |
| 256 | + |
| 257 | +- [Azure Spring Apps Samples](https://github.com/Azure-Samples/Azure-Spring-Cloud-Samples). |
| 258 | +- [Spring on Azure](/azure/developer/java/spring/) |
| 259 | +- [Spring Cloud Azure](/azure/developer/java/spring-framework/) |
0 commit comments