Deploy A Spring App to Azure Cloud
Prerequisite
- maven cli is installed
- Java 8 is installed
- Azure CLI is installed
- An Azure subscription (free account available).
Install maven cli if not installed
$ mvn -v
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /opt/homebrew/Cellar/maven/3.8.6/libexec
Java version: 1.8.0_292, vendor: AdoptOpenJDK, runtime: /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"
$ brew list maven
$ brew install maven # if not installed
Install Azure CLI if not installed
$ brew update
$ brew install azure-cli
$ az version
{
"azure-cli": "2.40.0",
"azure-cli-core": "2.40.0",
"azure-cli-telemetry": "1.0.8",
"extensions": {}
}
$ az extension add --name spring
If you don’t have a subscription, create a free Azure account before you begin. Then, login from a terminal
$ az login
A web browser has been opened at https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.
Provision an instance of Azure Spring Apps
You will provision like the following.
To know more about management group, subscription, resource group, please read this page.
a management group — yourself
a subscription (Pay as you go)
a resource group(my-resource-group)
a resource(my-spring-service)
an app (hellospring)
Find your subscription id
$ az account show
{
"environmentName": "AzureCloud",
"homeTenantId": "0dfcdbed-e135-4384-8271-d8c723fc97e0",
"id": "18b9a1a3-0cc0-4164-a2d4-c5ac8cb778d0",
"isDefault": true,
"managedByTenants": [],
"name": "Azure subscription 1",
"state": "Enabled",
"tenantId": "0dfcdbed-e135-4384-8271-d8c723fc97e0",
"user": {
"name": "allenhwkim@gmail.com",
"type": "user"
}
}$ az account list --output table
Name CloudName SubscriptionId ...
-------------------- ----------- --------------- ----------------
Azure subscription 1 AzureCloud xxxxxxxxxxxxxxx ...
Create a resource group, my-resource-group
, with a subscription
Copy the subscription id from above, xxxxxxxxxxxxxxx
, and use it for the following.
$ az account set --subscription xxxxxxxxxxxxxxx$ az group create \
--resource-group my-resource-group \
--location eastus
{
"id": "/subscriptions/xxxxxxxxxxxxxxx/resourceGroups/my-resource-group",
"location": "eastus",
"managedBy": null,
"name": "my-resource-group",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null,
"type": "Microsoft.Resources/resourceGroups"
}
Create a resource, a service called my-service
, within a resource group, my-resource-group
$ az spring create \
--resource-group my-resource-group \
--name my-spring-service
- Creating Service ..
Start configure Application Insights
Application Insights "my-spring-service" was created for this Azure Spring Apps. You can visit https://portal.azure.com/#resource/subscriptions/xxxxxxxxxxxxxxxx/resourceGroups/my-resource-group/providers/microsoft.insights/components/my-spring-service/overview to view your Application Insights component
{
"id": "/subscriptions/xxxxxxxxxxxxxxxx/resourceGroups/my-resource-group/providers/Microsoft.AppPlatform/Spring/my-spring-service",
"location": "eastus",
"name": "my-spring-service",
"properties": { ... },
"resourceGroup": "my-resource-group",
"sku": {
"capacity": null,
"name": "S0",
"tier": "Standard"
},
"systemData": { ... },
"tags": null,
"type": "Microsoft.AppPlatform/Spring"
}
Create an Azure app, hellospring
, within a service, my-spring-service
$ az spring app create \
--resource-group my-resource-group \
--service my-spring-service\
--name hellospring \
--assign-endpoint true
This command usually takes minutes to run. Add '--verbose' parameter if needed.
[1/3] Creating app hellospring
[2/3] Creating default deployment with name "default"
[3/3] Updating app "hellospring" (this operation can take a while to complete)
App create succeeded
{
"id": "/subscriptions/xxxxxxxxxxxxxxxx/resourceGroups/my-resource-group/providers/Microsoft.AppPlatform/Spring/my-spring-service/apps/hellospring",
"identity": null,
"location": "eastus",
"name": "hellospring",
"properties": {...},
"resourceGroup": "my-resource-group",
"systemData": {...},
"type": "Microsoft.AppPlatform/Spring/apps"
}
Deploy your code to Azure Cloud
Now, it’s time to deploy your .jar
(a Spring Boot build output) to Azure cloud
First, you need to have Java .jar
build output from your Java project. Thus, imply clone a gs-spring-boot
project from Git then run the build, mvn clean package
$ git clone https://github.com/spring-guides/gs-spring-boot.git
Cloning into 'gs-spring-boot'...
remote: Enumerating objects: 1522, done.
remote: Total 1522 (delta 0), reused 0 (delta 0), pack-reused 1522
Receiving objects: 100% (1522/1522), 932.56 KiB | 4.38 MiB/s, done.
Resolving deltas: 100% (989/989), done.
$ cd gs-spring-boot/
allens-MacBook-Air:gs-spring-boot allenkim$ ls
CONTRIBUTING.adoc LICENSE.txt README.adoc initial
Jenkinsfile LICENSE.writing.txt complete test$ cd complete/
$ ls
build.gradle gradlew mvnw pom.xml src
gradle gradlew.bat mvnw.cmd settings.gradle$ mvn clean package -DskipTests
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< com.example:spring-boot-complete >-------
[INFO] Building spring-boot-complete 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]----------------------
...$ ls target
classes maven-status
generated-sources spring-boot-complete-0.0.1-SNAPSHOT.jar
generated-test-sources maven-archiver test-classesYour application is now deployed to a Azure cloud. Visit https://my-spring-service-hellospring.azuremicroservices.io
Finally, it’s time to deploy .jar
file to Azure cloud using az spring app deploy
command.
$ az spring app deploy \
--resource-group my-resource-group \
--service my-spring-service \
--name hellospring \
--artifact-path target/spring-boot-complete-0.0.1-SNAPSHOT.jar
This command usually takes minutes to run. Add '--verbose' parameter if needed.
[1/3] Requesting for upload URL.
[2/3] Uploading package to blob.
[3/3] Updating deployment in app "hellospring" (this operation can take a while to complete)
{
"id": "/subscriptions/xxxxxxxxxxxxxxxx/resourceGroups/my-resource-group/providers/Microsoft.AppPlatform/Spring/my-spring-service/apps/hellospring/deployments/default",
"name": "default",
"properties": {...},
"resourceGroup": "my-resource-group",
"sku": {
"capacity": 1,
"name": "S0",
"tier": "Standard"
},
"systemData": {...},
"type": "Microsoft.AppPlatform/Spring/apps/deployments"
}
All done. Visit your page which is found at Azure home — my-springp-service
— Apps -hellospring
.