java - 在 Azure 应用服务上部署 Spring Boot jar

标签 java spring azure spring-boot azure-web-app-service

我在让 Spring Boot API 在 Azure 应用服务上运行时遇到问题。我已按照 Microsoft 指南 https://learn.microsoft.com/en-us/java/azure/spring-framework/deploy-spring-boot-java-web-app-on-azure 进行操作但到目前为止还没有运气。

应用程序确实启动(我可以在日志文件中看到应用程序启动),但对应用程序服务 URL 的 http 请求始终以超时结束。

我了解到 Azure 应用服务仅选取在端口 80 或 8080 上运行的嵌入式 tomcat 服务器,但也没有成功。

该应用程序部署在 www 根目录中,并且还部署了适当的 web.config。

我尝试过使用或不使用应用程序服务器(Tomcat 和 Jetty,这不是必需的,因为服务器嵌入在应用程序中)来运行应用服务,但这两种方法都失败了。

我是否缺少其他配置部分?或者这可能与我在 azure 上使用的计划类型有关吗?也许资源有问题?

有什么指点吗?

谢谢,

伯特

最佳答案

请使用 spring 和 azure 社区提供的以下步骤在 azure 上部署 spring boot 应用程序:

1) 进入包含 pom 文件的应用程序文件夹并运行

确保以下插件应位于 pom 文件中

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- tag::actuator[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- end::actuator[] -->
        <!-- tag::tests[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- end::tests[] -->
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
        <maven.build.timestamp.format>yyyyMMddHHmmssSSS</maven.build.timestamp.format>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-webapp-maven-plugin</artifactId>
                <version>0.1.5</version>
                <configuration>
                    <authentication>
                        <serverId>azure-auth</serverId>
                    </authentication>
                    <resourceGroup>maven-plugin</resourceGroup>
                    <appName>maven-web-app-${maven.build.timestamp}</appName>
                    <region>westus</region>
                    <javaVersion>1.8</javaVersion>
                    <deploymentType>ftp</deploymentType>
                    <stopAppDuringDeployment>true</stopAppDuringDeployment>
                    <resources>
                        <resource>
                            <directory>${project.basedir}/target</directory>
                            <targetPath>/</targetPath>
                            <includes>
                                <include>*.jar</include>
                            </includes>
                        </resource>
                        <resource>
                            <directory>${project.basedir}</directory>
                            <targetPath>/</targetPath>
                            <includes>
                                <include>web.config</include>
                            </includes>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Note : Make sure you have created web app on azure with same name as
maven-web-app-${maven.build.timestamp}

现在在根目录上创建名为“web.config”的文件,并将您的 jar 添加到 web.comfig

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
        </handlers>
        <httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
                      arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%\site\wwwroot\azure-rest-example-app-0.1.0.jar&quot;">
        </httpPlatform>
    </system.webServer>
</configuration>

enter image description here

现在打开 azure CLI 并运行以下命令 enter image description here

  • mvn 清理包
  • mvn spring-boot:run

确保应用程序在本地正常运行。

如果您有多个帐户与您的 ID 关联,请使用以下命令

  • az 登录

  • az 帐户列表

  • az 帐户设置 --subscription XXX-XXX-XXX-XXXXXXXXXXXX

现在您需要创建“Microsoft Azure 中的服务主体”

1) 打开终端窗口。

2) 通过键入 az login 使用 Azure CLI 登录到您的 Azure 帐户

3) 通过键入 az ad sp create-for-rbac --name "vaquarkhan"--password "yourpassword"创建 Azure 服务主体(vaquarkhan 是服务主体的用户名,yourpassword 是服务主体的密码)。

az ad sp create-for-rbac --name "app-name" --password "password"

注意:如果出现错误,需要更改设置---> here

"azure.graphrbac.models.graph_error.GraphErrorException: Guest users are not allowed to perform this action."

如果成功

Azure 应打印出类似于以下内容的 JSON 响应:

{
   "appId": "XXX-XXXX-XXX-XXX-XXXX",
   "displayName": "vaquarkhan",
   "name": "http://vaquarkhan",
   "password": "yourpassword",
   "tenant": "YYY-YYYY-YYY-YYY-YYYY"
}

配置 Maven 以使用您的 Azure 服务主体

1) 在文本编辑器中打开 Maven settings.xml 文件(通常位于/etc/maven/settings.xml 或 $HOME/.m2/settings.xml)。

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                          http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository/>
  <interactiveMode/>
  <usePluginRegistry/>
  <offline/>
  <pluginGroups/>

  <servers>
   <server>
     <id>azure-auth</id>
      <configuration>
         <client>ur key</client>
         <tenant>ur tenant</tenant>
         <key>YOUR PASSWORD</key>
         <environment>AZURE</environment>
      </configuration>
   </server>
</servers>


  <proxies/>

  <profiles>
    <profile>
      <id>hwx</id>
      <repositories>
        <repository>
          <id>hwx</id>
          <name>hwx</name>
          <url>http://nexus-private.hortonworks.com/nexus/content/groups/public/</url>
        </repository>
      </repositories>
    </profile>
  </profiles>


  <mirrors>
    <mirror>
      <id>public</id>
      <mirrorOf>*</mirrorOf>
      <url>http://nexus-private.hortonworks.com/nexus/content/groups/public/</url>
    </mirror>
  </mirrors>

  <activeProfiles/>
</settings>

2) 将本教程上一部分中的 Azure 服务主体设置添加到 settings.xml 文件中的集合,如下所示:

<servers>
   <server>
     <id>azure-auth</id>
      <configuration>
         <client>aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa</client>
         <tenant>tttttttt-tttt-tttt-tttt-tttttttttttt</tenant>
         <key>pppppppp</key>
         <environment>AZURE</environment>
      </configuration>
   </server>
</servers>

3) 保存并关闭settings.xml 文件。

构建应用并将其部署到 Azure

1) 运行以下命令

  • mvn azure-webapp:部署
  • 部署 Web 应用后,请访问 Azure 门户进行管理 它。它将在应用服务中列出。

  • 单击该应用程序。从那里,面向公众的 URL 您的网络应用程序将列在概述部分

  • 确定您的网络应用程序的 URL 您可以单击此链接 访问 Spring Boot 应用程序并与其交互。

enter image description here enter image description here

Azure maven 插件文档

Note : The website name has to be globally unique and its generated using app name , make sure name should be unique.

关于java - 在 Azure 应用服务上部署 Spring Boot jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47700526/

相关文章:

java - Junit 贯穿还是冗余测试? (基本的)

java - 非法状态异常 : Cannot convert value of type A to required type : no matching editors or conversion strategy found

java - Spring Evaluate Placeholder In Java,模拟@Value注解

azure - 我应该在生产环境中使用 Azure AD SAML 工具包吗?

python - 使用 Databricks 处理大量 JSON (~12TB)

java - 创建类型对象的通用列表并添加字符串

java - 考虑到内存限制,是否有另一种方法可以在 Java 中处理大型二维数组?

java - 如何向main添加命令参数? java

java - 缓存不适用于 map

powershell - Azure 自动化获取-AzureRmStorageAccountKey