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

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

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

问题

以下是翻译好的部分:

  1. Step 1:
  2. 创建了一个返回字符串 "Hello" Spring Boot 应用程序,并在本地测试了该代码,成功运行。
  3. Step 2:
  4. Azure 门户上创建了一个 Web 应用,并配置了本地 Git 存储库。
  5. Step 3:
  6. 在本地复制了 Git URL,并进行了 Git 克隆。
  7. Step 4:
  8. Webapps 文件夹内,我将 WAR 文件放置在其中,并将其重命名为 ROOT.war
  9. Step 4:
  10. 执行了以下命令:
  11. git status
  12. git add .
  13. git commit -m "Test"
  14. git push origin master
  15. Step 4:
  16. 转到 Azure 门户,检查了 ROOT 文件夹和 WAR 文件,部署成功。
  17. [点击这里查看截屏][1]
  18. Step 6:
  19. 当我访问 URL 时,出现了 404 错误:
  20. [点击这里查看错误图片][2]
  21. 控制器的代码片段:
  22. package io.javabrains.springbootstarter;
  23. import org.springframework.web.bind.annotation.RequestMapping;
  24. import org.springframework.web.bind.annotation.RestController;
  25. @RestController
  26. public class HelloController {
  27. @RequestMapping("/hello")
  28. public String hello() {
  29. return "Hello World RESTful with Spring Boot";
  30. }
  31. }
  32. Spring Boot 启动器的代码片段:
  33. package io.javabrains.springbootstarter;
  34. import org.springframework.boot.SpringApplication;
  35. import org.springframework.boot.autoconfigure.SpringBootApplication;
  36. @SpringBootApplication
  37. public class CourseApiApp {
  38. public static void main(String[] args) {
  39. SpringApplication.run(CourseApiApp.class, args);
  40. }
  41. }
  42. Pom.xml:
  43. Pom.xml 中,通过在 Spring Boot 中运行 Maven 插件(目标:mvn-webapp:config),在最后创建了配置,然后在配置标签中映射了我在 Azure 门户中创建的相同值。目标是检查创建了哪些配置属性。
  44. <?xml version="1.0" encoding="UTF-8"?>
  45. <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">
  46. <modelVersion>4.0.0</modelVersion>
  47. <groupId>io.javabrains.springbootquickstart</groupId>
  48. <artifactId>course-api-test</artifactId>
  49. <version>0.0.1-SNAPSHOT</version>
  50. <packaging>war</packaging>
  51. <name>Java Brains Course Api</name>
  52. <parent>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-starter-parent</artifactId>
  55. <version>2.1.3.RELEASE</version>
  56. <relativePath/>
  57. </parent>
  58. <dependencies>
  59. <!-- WEB -->
  60. <dependency>
  61. <groupId>org.springframework.boot</groupId>
  62. <artifactId>spring-boot-starter-web</artifactId>
  63. </dependency>
  64. <!-- <dependency>
  65. <groupId>org.springframework.boot</groupId>
  66. <artifactId>spring-boot-starter-tomcat</artifactId>
  67. <scope>provided</scope>
  68. </dependency> -->
  69. </dependencies>
  70. <properties>
  71. <java.version>1.8</java.version>
  72. </properties>
  73. <build>
  74. <plugins>
  75. <plugin>
  76. <groupId>org.springframework.boot</groupId>
  77. <artifactId>spring-boot-maven-plugin</artifactId>
  78. </plugin>
  79. <plugin>
  80. <groupId>com.microsoft.azure</groupId>
  81. <artifactId>azure-webapp-maven-plugin</artifactId>
  82. <version>1.7.0</version>
  83. <configuration>
  84. <schemaVersion>V2</schemaVersion>
  85. <resourceGroup>first-azure-application-28minutes</resourceGroup>
  86. <appName>CourseapiTest</appName>
  87. <pricingTier>F1</pricingTier>
  88. <region>South Central US</region>
  89. <appSettings>
  90. <property>
  91. <name>JAVA_OPTS</name>
  92. <value>-Dserver.port=80</value>
  93. </property>
  94. </appSettings>
  95. <runtime>
  96. <os>windows</os>
  97. <javaVersion>jre8</javaVersion>
  98. <webContainer>TOMCAT 9</webContainer>
  99. </runtime>
  100. <deployment>
  101. <resources>
  102. <resource>
  103. <directory>${project.basedir}/target</directory>
  104. <includes>
  105. <include>*.war</include>
  106. </includes>
  107. </resource>
  108. </resources>
  109. </deployment>
  110. </configuration>
  111. </plugin>
  112. </plugins>
  113. </build>
  114. </project>
  115. application.properties:
  116. server.forward-headers-strategy=FRAMEWORK
  117. 错误信息:在 Azure "Diagnose" "Errors" 部分中:
  118. [点击这里查看错误图片][3]
  119. [点击这里查看错误图片][4]
  120. [1]: https://i.stack.imgur.com/0kjUh.png
  121. [2]: https://i.stack.imgur.com/9Gf1m.png
  122. [3]: https://i.stack.imgur.com/56Bro.png
  123. [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:

  1. package io.javabrains.springbootstarter;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class HelloController {
  6. @RequestMapping(&quot;/hello&quot;)
  7. public String hello() {
  8. return &quot;Hello World RESTful with Spring Boot&quot;;
  9. }
  10. }

Code Snippet For Spring Boot Starter:

  1. package io.javabrains.springbootstarter;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class CourseApiApp {
  6. public static void main(String[] args) {
  7. SpringApplication.run(CourseApiApp.class,args);
  8. }
  9. }

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.

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-
  3. instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-
  4. 4.0.0.xsd&quot;&gt;
  5. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  6. &lt;groupId&gt;io.javabrains.springbootquickstart&lt;/groupId&gt;
  7. &lt;artifactId&gt;course-api-test&lt;/artifactId&gt;
  8. &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  9. &lt;packaging&gt;war&lt;/packaging&gt;
  10. &lt;name&gt;Java Brains Course Api&lt;/name&gt;
  11. &lt;parent&gt;
  12. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  13. &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
  14. &lt;version&gt;2.1.3.RELEASE&lt;/version&gt;
  15. &lt;relativePath/&gt;
  16. &lt;!-- lookup parent from repository --&gt;
  17. &lt;/parent&gt;
  18. &lt;dependencies&gt;
  19. &lt;!-- WEB --&gt;
  20. &lt;dependency&gt;
  21. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  22. &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
  23. &lt;/dependency&gt;
  24. &lt;!-- &lt;dependency&gt;
  25. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  26. &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
  27. &lt;scope&gt;provided&lt;/scope&gt;
  28. &lt;/dependency&gt;--&gt;
  29. &lt;/dependencies&gt;
  30. &lt;properties&gt;
  31. &lt;java.version&gt;1.8&lt;/java.version&gt;
  32. &lt;/properties&gt;
  33. &lt;build&gt;
  34. &lt;plugins&gt;
  35. &lt;plugin&gt;
  36. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  37. &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
  38. &lt;/plugin&gt;
  39. &lt;plugin&gt;
  40. &lt;groupId&gt;com.microsoft.azure&lt;/groupId&gt;
  41. &lt;artifactId&gt;azure-webapp-maven-plugin&lt;/artifactId&gt;
  42. &lt;version&gt;1.7.0&lt;/version&gt;
  43. &lt;configuration&gt;
  44. &lt;schemaVersion&gt;V2&lt;/schemaVersion&gt;
  45. &lt;resourceGroup&gt;first-azure-application-28minutes&lt;/resourceGroup&gt;
  46. &lt;appName&gt;CourseapiTest&lt;/appName&gt;
  47. &lt;pricingTier&gt;F1&lt;/pricingTier&gt;
  48. &lt;region&gt;South Central US&lt;/region&gt;
  49. &lt;appSettings&gt;
  50. &lt;property&gt;
  51. &lt;name&gt;JAVA_OPTS&lt;/name&gt;
  52. &lt;value&gt;-Dserver.port=80&lt;/value&gt;
  53. &lt;/property&gt;
  54. &lt;/appSettings&gt;
  55. &lt;runtime&gt;
  56. &lt;os&gt;windows&lt;/os&gt;
  57. &lt;javaVersion&gt;jre8&lt;/javaVersion&gt;
  58. &lt;webContainer&gt;TOMCAT 9&lt;/webContainer&gt;
  59. &lt;/runtime&gt;
  60. &lt;deployment&gt;
  61. &lt;resources&gt;
  62. &lt;resource&gt;
  63. &lt;directory&gt;${project.basedir}/target&lt;/directory&gt;
  64. &lt;includes&gt;
  65. &lt;include&gt;*.war&lt;/include&gt;
  66. &lt;/includes&gt;
  67. &lt;/resource&gt;
  68. &lt;/resources&gt;
  69. &lt;/deployment&gt;
  70. &lt;/configuration&gt;
  71. &lt;/plugin&gt;
  72. &lt;/plugins&gt;
  73. &lt;/build&gt;
  74. &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 文件内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <system.webServer>
  4. <handlers>
  5. <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
  6. </handlers>
  7. <httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
  8. arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;%HOME%\site\wwwroot\app.jar&quot;">
  9. </httpPlatform>
  10. </system.webServer>
  11. </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

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;configuration&gt;
  3. &lt;system.webServer&gt;
  4. &lt;handlers&gt;
  5. &lt;add name=&quot;httpPlatformHandler&quot; path=&quot;*&quot; verb=&quot;*&quot; modules=&quot;httpPlatformHandler&quot; resourceType=&quot;Unspecified&quot; /&gt;
  6. &lt;/handlers&gt;
  7. &lt;httpPlatform processPath=&quot;%JAVA_HOME%\bin\java.exe&quot;
  8. arguments=&quot;-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &amp;quot;%HOME%\site\wwwroot\app.jar&amp;quot;&quot;&gt;
  9. &lt;/httpPlatform&gt;
  10. &lt;/system.webServer&gt;
  11. &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:

确定