|
| 1 | +--- |
| 2 | +author: KarlErickson |
| 3 | +ms.author: xiada |
| 4 | +ms.service: spring-apps |
| 5 | +ms.topic: include |
| 6 | +ms.date: 07/11/2023 |
| 7 | +--- |
| 8 | + |
| 9 | +<!-- |
| 10 | +For clarity of structure, a separate markdown file is used to describe how to deploy to Azure Spring Apps consumption plan. |
| 11 | +
|
| 12 | +[!INCLUDE [deploy-to-azure-spring-apps-consumption-plan](includes/quickstart-deploy-web-app/deploy-consumption-plan.md)] |
| 13 | +
|
| 14 | +--> |
| 15 | + |
| 16 | +## 2. Prepare the Spring project |
| 17 | + |
| 18 | +Use the following steps to clone and run the app locally. |
| 19 | + |
| 20 | +1. Use the following command to clone the sample project from GitHub: |
| 21 | + |
| 22 | + ```bash |
| 23 | + git clone https://github.com/Azure-Samples/ASA-Samples-Web-Application.git |
| 24 | + ``` |
| 25 | + |
| 26 | +2. Use the following command to build the sample project: |
| 27 | + |
| 28 | + ```bash |
| 29 | + cd ASA-Samples-Web-Application |
| 30 | + ./mvnw clean package |
| 31 | + ``` |
| 32 | + |
| 33 | +3. Use the following command to run the sample application by using Maven: |
| 34 | + |
| 35 | + ```bash |
| 36 | + java -jar web/target/simple-todo-web-0.0.1-SNAPSHOT.jar |
| 37 | + ``` |
| 38 | + |
| 39 | +4. Go to `http://localhost:8080` in your browser to access the application. |
| 40 | + |
| 41 | +## 3. Prepare the cloud environment |
| 42 | + |
| 43 | +The main resources required to run this sample are an Azure Spring Apps instance and an Azure Database for PostgreSQL instance. This section provides the steps to create these resources. |
| 44 | + |
| 45 | +### 3.1. Provide names for each resource |
| 46 | + |
| 47 | +Create variables to hold the resource names by using the following commands. Be sure to replace the placeholders with your own values. |
| 48 | + |
| 49 | +```azurecli |
| 50 | +export RESOURCE_GROUP=<resource-group-name> |
| 51 | +export LOCATION=<location> |
| 52 | +export POSTGRESQL_SERVER=<server-name> |
| 53 | +export POSTGRESQL_DB=<database-name> |
| 54 | +export POSTGRESQL_ADMIN_USERNAME=<admin-username> |
| 55 | +export POSTGRESQL_ADMIN_PASSWORD=<admin-password> |
| 56 | +export AZURE_SPRING_APPS_NAME=<Azure-Spring-Apps-service-instance-name> |
| 57 | +export APP_NAME=<web-app-name> |
| 58 | +export MANAGED_ENVIRONMENT="<Azure-Container-Apps-environment-name>" |
| 59 | +export CONNECTION=<connection-name> |
| 60 | +``` |
| 61 | + |
| 62 | +### 3.2. Create a new resource group |
| 63 | + |
| 64 | +Use the following steps to create a new resource group: |
| 65 | + |
| 66 | +1. Use the following command to sign in to the Azure CLI: |
| 67 | + |
| 68 | + ```azurecli |
| 69 | + az login |
| 70 | + ``` |
| 71 | + |
| 72 | +1. Use the following command to set the default location: |
| 73 | + |
| 74 | + ```azurecli |
| 75 | + az configure --defaults location=${LOCATION} |
| 76 | + ``` |
| 77 | + |
| 78 | +1. Use the following command to list all available subscriptions to determine the subscription ID to use: |
| 79 | + |
| 80 | + ```azurecli |
| 81 | + az account list --output table |
| 82 | + ``` |
| 83 | + |
| 84 | +1. Use the following command to set the default subscription: |
| 85 | + |
| 86 | + ```azurecli |
| 87 | + az account set --subscription <subscription-ID> |
| 88 | + ``` |
| 89 | + |
| 90 | +1. Use the following command to create a resource group: |
| 91 | + |
| 92 | + ```azurecli |
| 93 | + az group create --resource-group ${RESOURCE_GROUP} |
| 94 | + ``` |
| 95 | + |
| 96 | +1. Use the following command to set the newly created resource group as the default resource group: |
| 97 | + |
| 98 | + ```azurecli |
| 99 | + az configure --defaults group=${RESOURCE_GROUP} |
| 100 | + ``` |
| 101 | + |
| 102 | +### 3.3. Create an Azure Spring Apps instance |
| 103 | + |
| 104 | +Azure Spring Apps is used to host the Spring web app. Create an Azure Spring Apps instance and an application inside it. |
| 105 | + |
| 106 | +An 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. For more information, see [Log Analytics workspace overview](../../../azure-monitor/logs/log-analytics-workspace-overview.md). |
| 107 | + |
| 108 | +1. Use the following command to create the environment: |
| 109 | + |
| 110 | + ```azurecli |
| 111 | + az containerapp env create \ |
| 112 | + --name ${MANAGED_ENVIRONMENT} |
| 113 | + ``` |
| 114 | + |
| 115 | +1. Use the following command to create a variable to store the environment resource ID: |
| 116 | + |
| 117 | + ```azurecli |
| 118 | + export MANAGED_ENV_RESOURCE_ID=$(az containerapp env show \ |
| 119 | + --name ${MANAGED_ENVIRONMENT} \ |
| 120 | + --query id \ |
| 121 | + --output tsv) |
| 122 | + ``` |
| 123 | + |
| 124 | +1. The Azure Spring Apps Standard consumption and dedicated plan instance is built on top of the Azure Container Apps environment. Create your Azure Spring Apps instance by specifying the resource ID of the environment you created. Use the following command to create an Azure Spring Apps service instance with the resource ID: |
| 125 | + |
| 126 | + ```azurecli |
| 127 | + az spring create \ |
| 128 | + --name ${AZURE_SPRING_APPS_NAME} \ |
| 129 | + --managed-environment ${MANAGED_ENV_RESOURCE_ID} \ |
| 130 | + --sku standardGen2 |
| 131 | + ``` |
| 132 | + |
| 133 | +1. Use the following command to specify the app name on Azure Spring Apps and to allocate required resources: |
| 134 | + |
| 135 | + ```azurecli |
| 136 | + az spring app create \ |
| 137 | + --service ${AZURE_SPRING_APPS_NAME} \ |
| 138 | + --name ${APP_NAME} \ |
| 139 | + --runtime-version Java_17 \ |
| 140 | + --assign-endpoint true |
| 141 | + ``` |
| 142 | + |
| 143 | +### 3.4. Prepare the PostgreSQL instance |
| 144 | + |
| 145 | +The Spring web app uses H2 for the database in localhost, and Azure Database for PostgreSQL for the database in Azure. |
| 146 | + |
| 147 | +Use the following command to create a PostgreSQL instance: |
| 148 | + |
| 149 | +```azurecli |
| 150 | +az postgres flexible-server create \ |
| 151 | + --name ${POSTGRESQL_SERVER} \ |
| 152 | + --database-name ${POSTGRESQL_DB} \ |
| 153 | + --admin-user ${POSTGRESQL_ADMIN_USERNAME} \ |
| 154 | + --admin-password ${POSTGRESQL_ADMIN_PASSWORD} \ |
| 155 | + --public-access 0.0.0.0 |
| 156 | +``` |
| 157 | + |
| 158 | +Specifying `0.0.0.0` enables public access from any resources deployed within Azure to access your server. |
| 159 | + |
| 160 | +### 3.5. Connect app instance to PostgreSQL instance |
| 161 | + |
| 162 | +After the application instance and the PostgreSQL instance are created, the application instance can't access the PostgreSQL instance directly. Use the following steps to enable the app to connect to the PostgreSQL instance: |
| 163 | + |
| 164 | +1. Use the following command to get the PostgreSQL instance's fully qualified domain name: |
| 165 | + |
| 166 | + ```azurecli |
| 167 | + export PSQL_FQDN=$(az postgres flexible-server show \ |
| 168 | + --name ${POSTGRESQL_SERVER} \ |
| 169 | + --query fullyQualifiedDomainName \ |
| 170 | + --output tsv) |
| 171 | + ``` |
| 172 | + |
| 173 | +1. Use the following command to provide the `spring.datasource.` properties to the app through environment variables: |
| 174 | + |
| 175 | + ```azurecli |
| 176 | + az spring app update \ |
| 177 | + --service ${AZURE_SPRING_APPS_NAME} \ |
| 178 | + --name ${APP_NAME} \ |
| 179 | + --env SPRING_DATASOURCE_URL="jdbc:postgresql://${PSQL_FQDN}:5432/${POSTGRESQL_DB}?sslmode=require" \ |
| 180 | + SPRING_DATASOURCE_USERNAME="${POSTGRESQL_ADMIN_USERNAME}" \ |
| 181 | + SPRING_DATASOURCE_PASSWORD="${POSTGRESQL_ADMIN_PASSWORD}" |
| 182 | + ``` |
| 183 | + |
| 184 | +## 4. Deploy the app to Azure Spring Apps |
| 185 | + |
| 186 | +Now that the cloud environment is prepared, the application is ready to deploy. Use the following command to deploy the app: |
| 187 | + |
| 188 | +```azurecli |
| 189 | +az spring app deploy \ |
| 190 | + --service ${AZURE_SPRING_APPS_NAME} \ |
| 191 | + --name ${APP_NAME} \ |
| 192 | + --artifact-path web/target/simple-todo-web-0.0.1-SNAPSHOT.jar |
| 193 | +``` |
0 commit comments