为什么在 Azure Web 应用部署后,Spring Boot 应用程序无法正常工作。

huangapple go评论79阅读模式
英文:

Why Spring Boot Application is not working after Deployment on Azure Web app

问题

以下是翻译好的部分:

Step 1:
创建了一个返回字符串 "Hello" 的 Spring Boot 应用程序,并在本地测试了该代码,成功运行。

Step 2:
在 Azure 门户上创建了一个 Web 应用,并配置了本地 Git 存储库。

Step 3:
在本地复制了 Git URL,并进行了 Git 克隆。

Step 4:
在 Webapps 文件夹内,我将 WAR 文件放置在其中,并将其重命名为 ROOT.war。

Step 4:
执行了以下命令:

git status

git add .

git commit -m "Test"

git push origin master

Step 4:
转到 Azure 门户,检查了 ROOT 文件夹和 WAR 文件,部署成功。
[点击这里查看截屏][1]

Step 6:
当我访问 URL 时,出现了 404 错误:
[点击这里查看错误图片][2]

控制器的代码片段:

package io.javabrains.springbootstarter;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello World RESTful with Spring Boot";
    }  
}

Spring Boot 启动器的代码片段:

package io.javabrains.springbootstarter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CourseApiApp {

    public static void main(String[] args) {
        SpringApplication.run(CourseApiApp.class, args);
    }

}

Pom.xml:

在 Pom.xml 中,通过在 Spring Boot 中运行 Maven 插件(目标:mvn-webapp:config),在最后创建了配置,然后在配置标签中映射了我在 Azure 门户中创建的相同值。目标是检查创建了哪些配置属性。

<?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>io.javabrains.springbootquickstart</groupId>
    <artifactId>course-api-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Java Brains Course Api</name>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <!-- WEB -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
        </dependency> -->
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-webapp-maven-plugin</artifactId>
                <version>1.7.0</version>
                <configuration>
                    <schemaVersion>V2</schemaVersion>
                    <resourceGroup>first-azure-application-28minutes</resourceGroup>
                    <appName>CourseapiTest</appName>
                    <pricingTier>F1</pricingTier>
                    <region>South Central US</region>
                    <appSettings>
                        <property>
                            <name>JAVA_OPTS</name>
                            <value>-Dserver.port=80</value>
                        </property>
                    </appSettings>
                    <runtime>
                        <os>windows</os>
                        <javaVersion>jre8</javaVersion>
                        <webContainer>TOMCAT 9</webContainer>
                    </runtime>
                    <deployment>
                        <resources>
                            <resource>
                                <directory>${project.basedir}/target</directory>
                                <includes>
                                    <include>*.war</include>
                                </includes>
                            </resource>
                        </resources>
                    </deployment>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.properties:

server.forward-headers-strategy=FRAMEWORK

错误信息:在 Azure 的 "Diagnose" 和 "Errors" 部分中:

[点击这里查看错误图片][3]

[点击这里查看错误图片][4]

  [1]: https://i.stack.imgur.com/0kjUh.png
  [2]: https://i.stack.imgur.com/9Gf1m.png
  [3]: https://i.stack.imgur.com/56Bro.png
  [4]: https://i.stack.imgur.com/f690e.png
英文:

Could please anyone help me with 404 Error when I try to access the api Url deployed on azure.Due to admin rights , not able to install azure CLI as if now.So I have followed below steps.

Step 1:
Created Spring boot application that is returning string "Hello" from HelloController and tested the code on local and it was up and running.

Step 2:
Create web app on azure portal and configured local git repository.

Step 3:
On my local,I copied the local git url and made git cloning.

Step 4:
Inside Webapps folder created ,I have placed the war file by renaming it to the ROOT.war

Step 4:
Ran the below command:

git status

git add .

git commit -m"Test"

git push origin master

Step 4:
Went on the azure portal and checked the ROOT folder and War File,it was succesfully deployed.
Click here For the Screenshot

Step 6:
When I hit on the url,I am getting 404 error:
Click here for Error Image

Code Snippet For Controller:

 package io.javabrains.springbootstarter;


 import org.springframework.web.bind.annotation.RequestMapping;

 import org.springframework.web.bind.annotation.RestController;

 @RestController
 public class HelloController {
      @RequestMapping(&quot;/hello&quot;)
    public String hello() {
        return &quot;Hello World RESTful with Spring Boot&quot;;
     }  
   }

Code Snippet For Spring Boot Starter:

package io.javabrains.springbootstarter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CourseApiApp {

public static void main(String[] args) {
	SpringApplication.run(CourseApiApp.class,args);

   }

 }

Pom.xml:

In Pom.xml, Configuration was created in the very last by running maven plugin(goal:mvn-webapp:config) in spring boot that created the default configuration.I then mapped the same values in configuration tag that I have created in my azure portal.Goal was to just check what configutation properties are created.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema- 
 instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven- 
 4.0.0.xsd&quot;&gt;  
 &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;  
 &lt;groupId&gt;io.javabrains.springbootquickstart&lt;/groupId&gt;  
 &lt;artifactId&gt;course-api-test&lt;/artifactId&gt;  
 &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;  
 &lt;packaging&gt;war&lt;/packaging&gt;  
 &lt;name&gt;Java Brains Course Api&lt;/name&gt;  
 &lt;parent&gt; 
 &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;  
 &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;  
 &lt;version&gt;2.1.3.RELEASE&lt;/version&gt;  
 &lt;relativePath/&gt;  
 &lt;!-- lookup parent from repository --&gt; 
 &lt;/parent&gt;  
 &lt;dependencies&gt; 
 &lt;!-- WEB --&gt;  
 &lt;dependency&gt; 
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;  
  &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt; 
 &lt;/dependency&gt;  
 &lt;!-- &lt;dependency&gt;
 &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
 &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
 &lt;scope&gt;provided&lt;/scope&gt;
 &lt;/dependency&gt;--&gt; 
 &lt;/dependencies&gt;  
 &lt;properties&gt; 
 &lt;java.version&gt;1.8&lt;/java.version&gt; 
 &lt;/properties&gt;  
 &lt;build&gt; 
   &lt;plugins&gt; 
    &lt;plugin&gt; 
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;  
    &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt; 
  &lt;/plugin&gt;  
  &lt;plugin&gt; 
    &lt;groupId&gt;com.microsoft.azure&lt;/groupId&gt;  
    &lt;artifactId&gt;azure-webapp-maven-plugin&lt;/artifactId&gt;  
    &lt;version&gt;1.7.0&lt;/version&gt;  
    &lt;configuration&gt;
      &lt;schemaVersion&gt;V2&lt;/schemaVersion&gt;
      &lt;resourceGroup&gt;first-azure-application-28minutes&lt;/resourceGroup&gt;
      &lt;appName&gt;CourseapiTest&lt;/appName&gt;
      &lt;pricingTier&gt;F1&lt;/pricingTier&gt;
      &lt;region&gt;South Central US&lt;/region&gt;
      &lt;appSettings&gt;
      &lt;property&gt;
      &lt;name&gt;JAVA_OPTS&lt;/name&gt;
      &lt;value&gt;-Dserver.port=80&lt;/value&gt;
      &lt;/property&gt;
      &lt;/appSettings&gt;
      &lt;runtime&gt;
        &lt;os&gt;windows&lt;/os&gt;
        &lt;javaVersion&gt;jre8&lt;/javaVersion&gt;
        &lt;webContainer&gt;TOMCAT 9&lt;/webContainer&gt;
      &lt;/runtime&gt;
      &lt;deployment&gt;
              &lt;resources&gt;
                    &lt;resource&gt;
                        &lt;directory&gt;${project.basedir}/target&lt;/directory&gt;
                           &lt;includes&gt;
                              &lt;include&gt;*.war&lt;/include&gt;
                           &lt;/includes&gt;
                    &lt;/resource&gt;
              &lt;/resources&gt;
      &lt;/deployment&gt;
    &lt;/configuration&gt;
  &lt;/plugin&gt; 
    &lt;/plugins&gt; 
    &lt;/build&gt; 
   &lt;/project&gt;

application.properties:

server.forward-headers-strategy=FRAMEWORK

Errors: Under Diagonose and Section In Azure:

Click here for Error Image

Click here for Error Image

答案1

得分: 1

部署 Spring Boot 应用到 Azure App Service 时,建议部署 JAR 文件。

我按照 这个教程 进行操作,一切正常。如果你总是看到默认页面,你需要添加一个 web.config 文件。

wwwroot 目录下的文件。

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\app.jar&quot;">
    </httpPlatform>
  </system.webServer>
</configuration>
英文:

To deploy a spring boot app to Azure App Service, it is recommended to deploy jar file.

I followed this tutorials and it works fine. If you always see the default page, you need to add a web.config file.

为什么在 Azure Web 应用部署后,Spring Boot 应用程序无法正常工作。

The files under wwwroot directory.

为什么在 Azure Web 应用部署后,Spring Boot 应用程序无法正常工作。

web.config

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

huangapple
  • 本文由 发表于 2020年5月3日 23:54:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61577418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定