将Spring Boot应用程序部署到Azure

建议查看官方文档,了解最新说明。

从GitHub克隆一个Spring Boot应用,使用maven部署到Azure.

需要

必须具备以下先决条件:

  • Azure订阅。如果您还没有Azure订阅,则可以注册一个免费的Azure帐户或激活您的MSDN订阅者权益
  • 一个最新的 JDK 版本(JDK) ,版本1.8或更高。
  • 一个 Git 客户端。

在本地构建并运行一个示例 Spring Boot web 应用程序

在本节中,您将克隆一个已经编写的 Spring Boot 应用程序,并在本地进行测试:

  1. 打开一个终端窗口.
  2. 通过输入以下命令 mkdir SpringBoot创建一个本地目录来保存 Spring Boot 应用程序
  3. 通过键入cd SpringBoot切换目录.
  4. Spring Boot Getting Started 示例项目复制到您创建的目录中,输入 git clone https://github.com/spring-guides/gs-spring-boot
  5. 通过键入 cd gs-spring-boot/complete命令更改已完成项目的目录
  6. 使用 Maven 构建 JAR 文件,输入 ./mvnw clean package
  7. 当网络应用程序创建完成后,输入 ./mvnw spring-boot:run
  8. 通过访问 http://localhost:8080 或者从另一个终端窗口输入 curl http://localhost:8080 来进行本地测试。
  9. 您应该会看到以下显示的消息: Greetings from Spring Boot!

配置并将应用程序部署到 Azure

  1. 在终端窗口中,通过输入./mvnw com.microsoft.azure:azure-webapp-maven-plugin:1.8.0:config 来配置你的 Maven Plugin for Azure Web App 。这个 maven 目标将首先使用 Azure 验证,如果您已经使用Azure CLI 登录,它将使用现有的身份验证标记。

  2. 否则,它将使你自动登录到 azure-maven-plugin。然后,您可以配置部署,在命令提示符中运行 maven 命令,并通过按 ENTER 使用默认配置,直到您得到 Confirm (Y/N)提示,按“ y”并完成配置。

    ~@Azure:~/gs-spring-boot/complete$ mvn azure-webapp:config
    [INFO] Scanning for projects...
    [INFO]
    [INFO] -----------------< org.springframework:gs-spring-boot >-----------------
    [INFO] Building gs-spring-boot 0.1.0
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- azure-webapp-maven-plugin:1.6.0:config (default-cli) @ gs-spring-boot ---
    [WARNING] The plugin may not work if you change the os of an existing webapp.
    Define value for OS(Default: Linux):
    1. linux [*]
    2. windows
    3. docker
    Enter index to use:
    Define value for javaVersion(Default: Java 8):
    1. Java 11
    2. Java 8 [*]
    Enter index to use:
    Please confirm webapp properties
    AppName : gs-spring-boot-1559091271202
    ResourceGroup : gs-spring-boot-1559091271202-rg
    Region : westeurope
    PricingTier : Premium_P1V2
    OS : Linux
    RuntimeStack : JAVA 8-jre8
    Deploy to slot : false
    Confirm (Y/N)? : Y
    

    3.然后,打开pom.xml 查看所有编写的配置,并将****部分添加到 ****的 ****部分,以监听80端口.

    <plugin>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-webapp-maven-plugin</artifactId>
        <version>1.8.0</version>
        <configuration>
           <schemaVersion>V2</schemaVersion>
           <resourceGroup>gs-spring-boot-1559091271202-rg</resourceGroup>
           <appName>gs-spring-boot-1559091271202</appName>
           <region>westeurope</region>
           <pricingTier>P1V2</pricingTier>
           <runtime>
             <os>linux</os>
             <javaVersion>jre8</javaVersion>
             <webContainer>jre8</webContainer>
           </runtime>
           <!-- Begin of App Settings  -->
           <appSettings>
              <property>
                    <name>JAVA_OPTS</name>
                    <value>-Dserver.port=80</value>
              </property>
           </appSettings>
           <!-- End of App Settings  -->
           <deployment>
             <resources>
               <resource>
                 <directory>${project.basedir}/target</directory>
                 <includes>
                   <include>*.jar</include>
                 </includes>
               </resource>
             </resources>
           </deployment>
          </configuration>
    </plugin>
    

    4.一旦您在本文前面的章节中配置了所有的设置,使用mvn azure-webapp:deploy,就可以将 web 应用部署到 Azure 上。将把你的 web 应用部署到 Azure 上; 如果 web 应用或 web 应用计划还不存在,它将为你创建。可能需要几分钟才能通过输出中显示的 URL 访问 web 应用程序。在 Web 浏览器中导航到 URL。您应该会看到显示的消息: Greetings from Spring Boot!

总结

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值