Skip to content

chenrujun/gs-spring-boot-for-azure

 
 

Repository files navigation

This article walks you through deploying an application to 2 Azure PaaS platforms: Azure Spring Cloud and Azure App Service.

Important
You are recommended to check out official Azure docs for Azure App Service and Azure Spring Cloud 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.

What you’ll need

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

Build and run a sample Spring Boot web app locally

In this section, you will clone an already written 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 Spring Boot Getting Started sample project into the directory you created by typing git clone https://github.com/spring-guides/gs-spring-boot

  5. Change to the directory of the completed project by typing cd gs-spring-boot/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: Greetings from Spring Boot!

(Option 1) Config and deploy the app to Azure Spring Cloud

  1. Before started, you will need to provision an Azure Spring Cloud cluster for instance using Azure Portal.

  2. From the terminal window, config your web app with Maven Plugin for Azure Spring Cloud by typing ./mvnw com.microsoft.azure:azure-spring-cloud-maven-plugin:1.3.0:config. 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.

  3. Then you can configure the deployment, run the maven command in the Command Prompt and select the Azure Spring Cloud cluster you just created, accept default for app name, then press 'y' to expose public access for this app. When you get the Confirm (Y/N) prompt, press 'y' and the configuration is done.

    ~@Azure:~/gs-spring-boot/complete$ mvn com.microsoft.azure:azure-spring-cloud-maven-plugin:1.3.0:config
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ----------------------< com.example:spring-boot >-----------------------
    [INFO] Building spring-boot 0.0.1-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- azure-spring-cloud-maven-plugin:1.3.0:config (default-cli) @ spring-boot ---
    [INFO] [Correlation ID: xxx] Instance discovery was successful
    Available Azure Spring Cloud Services:
     1. xxx*
    Select Azure Spring Cloud for deployment: [1-28] (1): 1
    [INFO] Using service: xxx
    Input the app name (spring-boot):
    Expose public access for this app spring-boot? (y/N):y
    Summary of properties:
    Subscription id   : xxx
    Service name      : xxx
    App name          : spring-boot
    Public access     : true
    Instance count    : 1
    CPU count         : 1
    Memory size(GB)   : 1
    Runtime Java version : Java 8
    Confirm to save all the above configurations (Y/n):
  4. Optionally, open your pom.xml to see all the configuration written.

    <plugin>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-spring-cloud-maven-plugin</artifactId>
        <version>1.3.0</version>
        <configuration>
            <subscriptionId>xxx</subscriptionId>
            <clusterName>xxx</clusterName>
            <appName>spring-boot</appName>
            <isPublic>true</isPublic>
            <deployment>
                <cpu>1</cpu>
                <memoryInGB>1</memoryInGB>
                <instanceCount>1</instanceCount>
                <runtimeVersion>Java 8</runtimeVersion>
                <resources>
                    <resource>
                        <filtering/>
                        <mergeId/>
                        <targetPath/>
                        <directory>${project.basedir}/target</directory>
                        <includes>
                            <include>*.jar</include>
                        </includes>
                    </resource>
                </resources>
            </deployment>
        </configuration>
    </plugin>
  5. Once you have configured all of the settings in the preceding sections, you are ready to deploy your web app to Azure Spring Cloud with mvn azure-spring-cloud:deploy. Maven will deploy your web app to Azure. It might take a few minutes before the web app is accessible at the URL shown in the output. Navigate to the URL in a Web browser. You should see the message displayed: Greetings from Spring Boot! Note that this simple Spring Boot sample app is not using any Spring Cloud components like Discovery Client, thus registration status will be down while the app is up and running normally.

(Option 2) Config and deploy the app to Azure App Service

  1. From the terminal window, config your web app with Maven Plugin for Azure Web App by typing ./mvnw com.microsoft.azure:azure-webapp-maven-plugin:1.14.0:config. 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.

  2. Then you can configure the deployment, run the maven command in the Command Prompt and use the default configurations by pressing ENTER until you get the Confirm (Y/N) prompt, press 'y' and the configuration is done.

    ~@Azure:~/gs-spring-boot/complete$ mvn com.microsoft.azure:azure-webapp-maven-plugin:1.14.0:config
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ----------------------< com.example:spring-boot >-----------------------
    [INFO] Building spring-boot 0.0.1-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- azure-webapp-maven-plugin:1.14.0:config (default-cli) @ spring-boot ---
    [INFO] Auth type: OAUTH2
    Username: xxx
    Available subscriptions:
    *  1: xxx (xxx)
    Please choose a subscription [xxx]: 1
    [INFO] It may take a few minutes to load all Java Web Apps, please be patient.
    Java SE Web Apps in subscription xxx:
    * 1: <create>
      2: xxx (linux, java 8-jre8)
    Please choose a Java SE Web App [<create>]:
    Define value for OS [Linux]:
    * 1: Linux
      2: Windows
      3: Docker
    Enter your choice:
    Define value for pricingTier [P1v2]:
       1: B1
       2: B2
       3: B3
       4: D1
       5: F1
    *  6: P1v2
       7: P2v2
       8: P3v2
       9: S1
      10: S2
      11: S3
    Enter your choice:
    Define value for javaVersion [Java 8]:
    * 1: Java 8
      2: Java 11
    Enter your choice:
    Please confirm webapp properties
    Subscription Id : xxx
    AppName : spring-boot-1621580171863
    ResourceGroup : spring-boot-1621580171863-rg
    Region : westeurope
    PricingTier : PremiumV2_P1v2
    OS : Linux
    Java : Java 8
    Web server stack: Java SE
    Deploy to slot : false
    Confirm (Y/N) [Y]:
  3. Optionally, open your pom.xml to see all the configuration written.

    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    <plugin>
      <groupId>com.microsoft.azure</groupId>
      <artifactId>azure-webapp-maven-plugin</artifactId>
      <version>1.14.0</version>
      <configuration>
        <schemaVersion>v2</schemaVersion>
        <subscriptionId>xxxxxxx</subscriptionId>
        <resourceGroup>spring-boot-1621580171863-rg</resourceGroup>
        <appName>spring-boot-1621580171863</appName>
        <pricingTier>P1v2</pricingTier>
        <region>westeurope</region>
        <runtime>
          <os>Linux</os>
          <javaVersion>Java 8</javaVersion>
          <webContainer>Java SE</webContainer>
        </runtime>
        <deployment>
          <resources>
            <resource>
              <directory>${project.basedir}/target</directory>
              <includes>
                <include>*.jar</include>
              </includes>
            </resource>
          </resources>
        </deployment>
      </configuration>
    </plugin>
  4. Once you have configured all of the settings in the preceding sections, you are ready to deploy your web app to Azure with mvn azure-webapp:deploy. Maven will deploy your web app to Azure; if the web app or web app plan does not already exist, it will be created for you. It might take a few minutes before the web app is accessible at the URL shown in the output. Navigate to the URL in a Web browser. You should see the message displayed: Greetings from Spring Boot!

Summary

Congratulations! You built and deployed a Spring Boot app to Azure. You can visit the Azure portal to manage it.

Important
Don’t forget to delete the Azure resources created if no longer needed.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 86.2%
  • Shell 13.8%