【问题标题】:Deploy Spring Boot jar on Azure App Service在 Azure 应用服务上部署 Spring Boot jar
【发布时间】:2018-05-21 21:11:30
【问题描述】:

我无法让 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。

我尝试在有和没有应用程序服务器(Tomcat 和 Jetty,因为服务器嵌入在应用程序中,所以不需要)的情况下运行应用服务,但两种方法都失败了。

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

有什么建议吗?

谢谢,

伯特

【问题讨论】:

  • 现在有更新吗?

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


【解决方案1】:

为了让 Springboot 应用程序运行,您需要上传 JAR 文件并添加 web.config 文件。

要与服务通信您尝试运行的内容,您需要将 web.config 文件添加到应用服务的 site\wwwroot 文件夹。由于您已经创建了 web.config 文件,请使用 Maven 添加以下内容并获得一个项目/发布动态包含在包中。

<build>
    <resources>
        <resource>
            <directory>${project.basedir}/wwwroot</directory>
            <filtering>true</filtering>
            <targetPath>${basedir}/target</targetPath>
        </resource>
    </resources>
</build> 

现在将 jar 文件和 web.config 放在 Azure 应用服务中。

您只需检查一次是否已创建如下 web.config 文件,

<?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\@project.artifactId@-@project.version@.jar&quot;">
        </httpPlatform>
    </system.webServer>
</configuration>

【讨论】:

  • 嗨,这正是我配置所有内容并且应用程序运行的方式,但我无法访问 i。所以我猜http请求永远不会到达嵌入式tomcat服务器?或者这可能与我使用的计划类型有关?
【解决方案2】:

结合official tutorials中的步骤和你的实际情况,我提供以下检查点:

第1点:请使用mvn packagepom.xml文件所在目录下构建JAR包。

]

第二点:请确保web.config中配置的jar包名与上传的jar包名一致。

web.config

<?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\<your project name>&quot;">
    </httpPlatform>
  </system.webServer>
</configuration>

第3点:请使用FTP将jar filesweb.config发布到KUDU上的D:\home\site\wwwroot\目录。

第 4 点:请确保ApplicationSettings 与您的项目相匹配,例如jdk version,tomcat version

如果要部署war文件,需要在Azure门户配置应用服务的ApplicationSettings,然后将war文件上传到D:\home\site\wwwroot\webapps路径。

此外,您可以查看 KUDU 上的日志文件:https://&lt;your project name&gt;.scm.azurewebsites.net/DebugConsole.

作为参考,请参阅下面的文档和线程。

1.Configure web apps in Azure App Service

2.Create a Java web app in Azure App Service

3.Deploying Springboot to Azure App Service.

希望对你有所帮助。

【讨论】:

    【解决方案3】:

    事实证明,我认为这是 azure 资源问题的预感是正确的。升级资源内存和 CPU 解决了这个问题。

    【讨论】:

      【解决方案4】:

      请使用 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>
      

      注意:确保您已在 azure 上创建了同名的网络应用程序
      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>
      

      现在打开 azure CLI 并运行以下命令

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

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

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

      • az 登录

      • az 帐号列表

      • az 账户集--订阅 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:来宾用户 不允许执行此操作。”

      如果成功

      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:deploy
      • 部署 Web 应用后,访问 Azure 门户进行管理 它。它将列在应用服务中。

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

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

      Azure maven 插件文档

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

      【讨论】:

      • 它是否也负责数据库创建部分?
      • 您可以在应用程序中定义的数据库连接。属性文件,您可以将 db 用作服务或在 vm,docker 上使用。
      • --password "password" 现在已被 microsoft 删除,因此它不再适用于以下 -> az ad sp create-for-rbac --name "app-name" --password "密码”
      猜你喜欢
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 2018-06-10
      • 2021-05-18
      • 2016-12-13
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多