【Azure 應用服務】一個 App Service 同時部署執行兩個及多個 Java 應用程式(Jar包)
阿新 • • 發佈:2022-01-08
問題描述
如何在一個AppService下同時部署執行多個Java 應用程式呢?
問題解答
因為App Service的預設根目錄為 wwwroot。如果需要執行多個Java 應用程式,需要在 wwwroot目錄中建立獨立資料夾,用於部署 Jar包 和 web.config 檔案,特別注意的時:需要在web.config中指定jar包的啟動指令。
如正常部署一個jar包,App Service 根目錄下的檔案結構如下:
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 "%HOME%\site\wwwroot\logdemo-1.0-SNAPSHOT.jar""> </httpPlatform> </system.webServer> </configuration>
Spring Boot的程式碼為:
App.Java
package com.example; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { private static final Logger logger = LoggerFactory.getLogger(App.class); public static void main(String[] args) { SpringApplication.run(App.class, args); logger.info("test java logs : info"); logger.error("test java logs : error"); logger.warn("test java logs : warn"); logger.trace("test java logs : trace" ); } }
HelloController.java
package com.example; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/") String hello() { return "Hello World!"; } @RequestMapping("/newhello") String hello2() { return "Hello World,this is hello2 result!"; } }
PS: 以上Spring Boot程式碼為Spring 框架預設生成的程式碼,只是添加了一個hello2的新介面用於測試。
使用 mvn clean package 打包為 logdemo-1.0-SNAPSHOT.jar檔案,直接通過拖拽的方式,放入App Service wwwroot 目錄中。 當檔案上傳完成後,直接訪問App Service URL檢視效果:
# 部署多個Spring Boot應用的辦法
1: 在wwwroot目錄下建立多個應用資料夾,如app1,app2,app3
2: 把對應的app jar包訪問對應資料夾中,然後修改web.config中的路徑, 如 %HOME%\site\wwwroot\app3\app.jar
- httpPlatformHandler 的名稱在整個App Service中需要保持唯一,如app3的web.config中handlers的名稱為:httpPlatformHandlerapp3
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandlerapp3" 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 "%HOME%\site\wwwroot\app3\app.jar"">
</httpPlatform>
</system.webServer>
</configuration>
3: 在 App Service的配置項中,進入"路徑對映 Path Mapping",配置Virtual applications
- 虛擬路徑為:/app1, /app2,/app3
- 物理路徑為:site/wwwroot/app1, site/wwwroot/app2, site/wwwroot/app3
- 預設Type被勾選為 Directory,一定要去掉勾選。
4:多應用訪問效果如下
特別注意:因為app1,app2,app3等應用的訪問url為 host/app1等,所以在 Controller 程式碼中,RequestMapping的路徑必須根據第三步中配置路徑名相匹配
如/app3的 Request Mapping設定必須為:
@RequestMapping("/app3")
String hello() {
return "Hello World!";
}
@RequestMapping("/app3/newhello")
String hello2() {
return "Hello World,this is hello2 result!";
}
參考資料
快速入門:在 Azure 應用服務中建立 Java 應用: https://docs.azure.cn/zh-cn/app-service/quickstart-java?tabs=javase&pivots=platform-windows
當在複雜的環境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 雲中,恰是如此!