在Azure App Service上部署Spring Boot jar

Ber*_*mme 7 java spring azure spring-boot azure-web-app-service

我在使用Spring Boot API处理Azure应用服务时遇到问题.我在https://docs.microsoft.com/en-us/java/azure/spring-framework/deploy-spring-boot-java-web-app-on-azure上关注了Microsoft指南,但没有运气远.

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

我已经读过Azure应用服务只能选择在端口80或8080上运行的嵌入式tomcat服务器,但也没有运气.

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

我尝试使用和不使用应用程序服务器运行App Service(Tomcat和Jetty,因为服务器嵌入在应用程序中而不需要),但两种方法都失败了.

我错过了其他一些配置部分吗?或者这可能与我在天蓝色上使用的计划类型有关?也许资源有些问题?

有什么指针吗?

谢谢,

伯特

vaq*_*han 5

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

1)进入你有pom文件的app文件夹并运行

确保以下插件应该在 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>
Run Code Online (Sandbox Code Playgroud)

注意:确保您已经在 azure 上创建了与
maven-web-app-${maven.build.timestamp}同名的 web 应用程序

现在在 root 上创建名为“web.config”的文件,并在 web.comfig 中添加你的 jar

<?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>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

现在打开 azure CLI 并运行以下命令 在此处输入图片说明

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

确保应用程序在本地运行良好。

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

  • 登录

  • 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"

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

“azure.graphrbac.models.graph_error.GraphErrorException:不允许来宾用户执行此操作。”

如果成功

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

{
   "appId": "XXX-XXXX-XXX-XXX-XXXX",
   "displayName": "vaquarkhan",
   "name": "http://vaquarkhan",
   "password": "yourpassword",
   "tenant": "YYY-YYYY-YYY-YYY-YYYY"
}
Run Code Online (Sandbox Code Playgroud)

配置 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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

构建应用并将其部署到 Azure

1)运行以下命令

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

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

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

在此处输入图片说明 在此处输入图片说明

Azure Maven 插件文档

注意:网站名称必须是全球唯一的,并且使用 app name 生成,确保名称应该是唯一的。


Ber*_*mme 1

事实证明,我对天蓝色资源问题的预感是正确的。升级资源内存和 CPU 解决了该问题。