/gs-spring-boot-for-azure

Deploying a Spring Boot app to Azure :: Learn how to deploy a Spring Boot app to Azure.

Primary LanguageShellApache License 2.0Apache-2.0

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

deploytoazurebutton

Basic/Standard plan

deploytoazurebutton

Enterprise plan

deploytoazurebutton

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.

What you’ll build

You’ll clone a sample Spring Boot application from GitHub and then use Maven to deploy it to Azure Spring Apps.

What you’ll need

The following prerequisites are required in order to follow the steps in this article:

Build and run the web app locally

In this section, you will clone a Spring Boot application and test it locally.

  1. Open a terminal window.

  2. Create a local directory to hold your Spring Boot application by typing mkdir SpringBoot

  3. Change to that directory by typing cd SpringBoot.

  4. 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

  5. Change to the directory of the completed project by typing cd gs-spring-boot-for-azure/complete

  6. Build the JAR file using Maven by typing ./mvnw clean package

  7. When the web app has been created, start it by typing ./mvnw spring-boot:run

  8. Test it locally by either visiting http://localhost:8080 or typing curl http://localhost:8080 from another terminal window.

  9. You should see the following message displayed: Hello World.

Config and deploy the app to Azure Spring Apps

Provision an Azure Spring Apps instance

  1. In a web browser, open the Azure portal, and sign in to your account.

  2. Search for Azure Spring Apps and then select Azure Spring Apps.

  3. On the overview page, select Create, and then do the following:

    1. 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.

    2. In the Subscription drop-down list, select the subscription you want to be billed for this resource.

    3. In the Resource group box, create a new resource group. Creating resource groups for new resources is a best practice.

    4. In the Location drop-down list, select the location for your service instance.

  4. It takes about 5 minutes for the service to be deployed.

Add configuration for deployment

  1. 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
  2. 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>

Deploy the app

  1. Run this command to deploy the app:

    ./mvnw azure-spring-apps:deploy
  2. It might take a few minutes to deploy. After deploy finished, there will be a URL shown in the output. Navigate to the URL in a web browser. You should see the message displayed: Hello World.

Summary

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.

Run with AZD

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

See also

Additional information about using Spring with Azure is available here: