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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47700526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:45:51  来源:igfitidea点击:

Deploy Spring Boot jar on Azure App Service

javaspringazurespring-bootazure-web-app-service

提问by Bert Vandamme

I'm having trouble getting a Spring Boot API to work on an Azure app service. I've followed the Microsoft guide on https://docs.microsoft.com/en-us/java/azure/spring-framework/deploy-spring-boot-java-web-app-on-azurebut having no luck so far.

我无法让 Spring Boot API 在 Azure 应用服务上工作。我遵循了https://docs.microsoft.com/en-us/java/azure/spring-framework/deploy-spring-boot-java-web-app-on-azure上的 Microsoft 指南,但没有运气所以远的。

The application does start (I can see the app boot up in the log file) but http requests to the app service url always end in a timeout.

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

I've read that Azure app services only pick up embedded tomcat servers that run on port 80 or 8080, but had no luck with that as well.

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

The app is deployed in the www root and an appropriate web.config is deployed as well.

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

I've tried running the App Service with and without an application server (Tomcat and Jetty, that is not needed because the server is embedded in the application), but both approaches failed.

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

Am I missing some other configuration part? Or could this be related to the type of plan I'm using on azure? Maybe some issue with the resource?

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

Any pointers?

任何指针?

Thx,

谢谢,

Bert

伯特

采纳答案by Bert Vandamme

It turns out that my hunch about it being an issue with the azure resource was correct. Upscaling resource memory and CPU resolved the issue.

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

回答by vaquar khan

Please use following steps given by spring and azure community to deploy spring boot app on azure:

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

1) Go inside of your app folder where you have pom file and run

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

make sure following plugins should be in pom file

确保以下插件应该在 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}

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

Now create file on root with name "web.config" and add your jar in web.comfig

现在在 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>

enter image description here

在此处输入图片说明

Now open azure CLI and run following commands enter image description here

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

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

Make sure app is working fine on local.

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

Now use following commands if you have multiple account associated with your id

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

  • az login

  • az account list

  • az account set --subscription XXX-XXX-XXX-XXXXXXXXXXXX

  • 登录

  • az 帐户列表

  • az 帐户集 --subscription XXX-XXX-XXX-XXXXXXXXXXXX

Now you need to create "Service Principals in Microsoft Azure"

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

1) Open a terminal window.

1) 打开终端窗口。

2) Sign into your Azure account with the Azure CLI by typing az login

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

3) Create an Azure service principal by typing az ad sp create-for-rbac --name "vaquarkhan" --password "yourpassword" (vaquarkhan is the user name and yourpassword is the password for the service principal).

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"

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

NOTE :if you getting error need to change settings---> here

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

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

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

if success

如果成功

Azure should print out a JSON response resembling this:

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

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

Configure Maven to use your Azure service principal

配置 Maven 以使用 Azure 服务主体

1) Open your Maven settings.xml file in a text editor (usually found at either /etc/maven/settings.xml or $HOME/.m2/settings.xml).

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) Add your Azure service principal settings from the previous section of this tutorial to the collection in the settings.xml file as shown below:

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) Save and close the settings.xml file.

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

Build and deploy your app to Azure

构建应用并将其部署到 Azure

1) run following command

1)运行以下命令

  • mvn azure-webapp:deploy
  • When your web app has been deployed, visit the Azure portal to manage it. It will be listed in App Services.

  • Click on the application. From there, the publicly-facing URL for your web app will be listed in the Overview section

  • Determining the URL for your web app You can click on this link to visit the Spring Boot application and interact with it.

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

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

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

enter image description hereenter image description here

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

Azure maven plugin doc

Azure Maven 插件文档

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

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

回答by Jay Gong

Combining the steps in the official tutorialsand your actual situation, I provide the following check points:

结合官方教程中的步骤和你的实际情况,我提供以下检查点:

Point 1:Please use mvn packageto bulid the JAR package in the directory under which the pom.xmlfile is located.

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

enter image description here]

在此处输入图片说明]

Point 2:Please make sure that the jar package name configured in web.config is the same as the uploaded jar package name.

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

enter image description here

在此处输入图片说明

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>

Point 3:Please use FTP to publish jar filesand web.configto D:\home\site\wwwroot\directory on KUDU.

要点3:请使用FTP发布jar files,并web.configD:\home\site\wwwroot\目录中KUDU。

Point 4:Please make sure ApplicationSettingsmatches your project such as jdk version,tomcat version.

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

enter image description here

在此处输入图片说明

If you want to deploy a warfile, you need to configure the ApplicationSettings of your app service on Azure portal, then upload the war file into the path D:\home\site\wwwroot\webapps.

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

In addition, you could check the log files on KUDU : https://<your project name>.scm.azurewebsites.net/DebugConsole.

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

As references, please refer to the documents and threads below.

作为参考,请参阅下面的文档和主题。

1.Configure web apps in Azure App Service

1.在 Azure 应用服务中配置 Web 应用

2.Create a Java web app in Azure App Service

2.在 Azure 应用服务中创建 Java Web 应用

3.Deploying Springboot to Azure App Service.

3. 将Springboot 部署到 Azure 应用服务

Hope it helps you.

希望对你有帮助。

回答by Swikruti Bose

In order to get a Springboot application running you need to upload your JAR file and add the web.config file.

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

To communicate to the service what you are trying to run, you need to add a web.config file to the site\wwwroot folder of the app service. As you have already created web.config file, use Maven to add the following and get a project / release dynamically included on package.

要将您尝试运行的内容与服务进行通信,您需要将 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> 

Now place the jar file and the web.config within the Azure App Service.

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

You can just check once whether you have created the web.config file as below,

你可以检查一下你是否已经创建了 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@[email protected]@.jar&quot;">
        </httpPlatform>
    </system.webServer>
</configuration>