Before delving into the step-by-step execution of the application, you can simply click the Deploy to Azure button. This will instantly deploy the app to Azure Spring Apps.
Deploy to Azure Spring Apps |
|
Consumption plan |
|
Basic/Standard plan |
|
Enterprise plan |
This article walks you through deploying a Spring Boot application to Azure Spring Apps.
Important
|
You are recommended to check out Azure Spring Apps quick start doc for the latest instructions for the same task. |
You’ll clone a sample Spring Boot application from GitHub and then use Maven to deploy it to Azure Spring Apps.
The following prerequisites are required in order to follow the steps in this article:
-
An Azure subscription. If you don’t have an Azure subscription, you can sign up for a free Azure account or activate your MSDN subscriber benefits.
-
An Java Development Kit (JDK), version 17 or later.
-
A Git client.
In this section, you will clone a Spring Boot application and test it locally.
-
Open a terminal window.
-
Create a local directory to hold your Spring Boot application by typing
mkdir SpringBoot
-
Change to that directory by typing
cd SpringBoot
. -
Clone the Deploying a Spring Boot app to Azure sample project into the directory you created by typing
git clone https://github.com/spring-guides/gs-spring-boot-for-azure
-
Change to the directory of the completed project by typing
cd gs-spring-boot-for-azure/complete
-
Build the JAR file using Maven by typing
./mvnw clean package
-
When the web app has been created, start it by typing
./mvnw spring-boot:run
-
Test it locally by either visiting http://localhost:8080 or typing
curl http://localhost:8080
from another terminal window. -
You should see the following message displayed: Hello World.
-
In a web browser, open the Azure portal, and sign in to your account.
-
Search for
Azure Spring Apps
and then selectAzure Spring Apps
. -
On the overview page, select Create, and then do the following:
-
In the Service Name box, specify the name of your service instance. The name must be from 4 to 32 characters long and can contain only lowercase letters, numbers, and hyphens. The first character of the service name must be a letter, and the last character must be either a letter or a number.
-
In the Subscription drop-down list, select the subscription you want to be billed for this resource.
-
In the Resource group box, create a new resource group. Creating resource groups for new resources is a best practice.
-
In the Location drop-down list, select the location for your service instance.
-
-
It takes about 5 minutes for the service to be deployed.
-
Config the web app with Maven Plugin for Azure Spring Apps by this command:
export MAVEN_OPTS="--add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED" ./mvnw com.microsoft.azure:azure-spring-apps-maven-plugin:1.18.0:config -DadvancedOptions
-
Command explanation:
-
export MAVEN_OPTS="--add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED"
is used to avoid the problem tracked in azure-maven-plugins#2222. -
This maven goal will first authenticate with Azure, if you have logged in with Azure CLI, it will consume its existing authentication token. Otherwise, it will get you logged in with azure-maven-plugin automatically.
-
-
Input necessary information:
-
Select subscription: Select the subscription which host the Azure Spring Apps you just created.
-
Select Azure Spring Apps for deployment: Select the Azure Spring Apps you just created.
-
Input runtime Java version(Java 11): Java 17
-
Expose public access for this app boot-for-azure? (y/N): y
-
For other options, just select the default value.
-
-
Here is a sample terminal output:
~@Azure:~/gs-spring-boot/complete$ ./mvnw com.microsoft.azure:azure-spring-apps-maven-plugin:1.14.0:config -DadvancedOptions [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building boot-for-azure 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- azure-spring-apps-maven-plugin:1.14.0:config (default-cli) @ demo --- ... Available subscription: 1. xxx ... Select subscription [1-105] (20): ... Available Azure Spring Apps: 1. xxx ... Select Azure Spring Apps for deployment: [1-28] (1): 1 [INFO] Using Azure Spring Apps: xxx Input the app name (demo): Expose public access for this app boot-for-azure? (y/N):y Summary of properties: Subscription id : xxx Resource group name : xxx Azure Spring Apps name : xxx App name : demo Public access : yes Instance count : 1 CPU count : 1 Memory size(GB) : 1 Runtime Java version : Java 17 Confirm to save all the above configurations (Y/n):Y
-
-
Optionally, open the pom.xml to see the new added content.
<plugin> <groupId>com.microsoft.azure</groupId> <artifactId>azure-spring-apps-maven-plugin</artifactId> <version>1.14.0</version> <configuration> <subscriptionId>xxx</subscriptionId> <resourceGroup>xxx</resourceGroup> <clusterName>xxx</clusterName> <appName>demo</appName> <isPublic>true</isPublic> <deployment> <cpu>1</cpu> <memoryInGB>1</memoryInGB> <instanceCount>1</instanceCount> <runtimeVersion>Java 17</runtimeVersion> <resources> <resource> <filtering/> <mergeId/> <targetPath/> <directory>${project.basedir}/target</directory> <includes> <include>*.jar</include> </includes> </resource> </resources> </deployment> </configuration> </plugin>
Congratulations! You built and deployed a Spring Boot app to Azure Spring Apps. You can visit the Azure portal to manage it.
Important
|
Don’t forget to delete the Azure resources created if no longer needed. |
You can also use the Azure Developer CLI (azd) to quickly run this application on Azure Spring Apps. The Azure Developer CLI is an open-source tool that accelerates the time it takes for you to get your application from your local development environment to Azure.
For how to run it with azd, you can refer to Deploy your first application to Azure Spring Apps
Additional information about using Spring with Azure is available here: