【Azure 应用服务】一个 App Service 同时部署运行两个及多个 Java 应用程序(Jar包)

简介: 【Azure 应用服务】一个 App Service 同时部署运行两个及多个 Java 应用程序(Jar包)

问题描述

如何在一个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 &quot;%HOME%\site\wwwroot\logdemo-1.0-SNAPSHOT.jar&quot;">
        </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 &quot;%HOME%\site\wwwroot\app3\app.jar&quot;">
        </httpPlatform>
    </system.webServer>
</configuration>

3: 在 App Service的配置项中,进入"路径映射 Path Mapping",配置Virtual applications

  1. 虚拟路径为:/app1, /app2,/app3
  2. 物理路径为:site/wwwroot/app1, site/wwwroot/app2, site/wwwroot/app3
  3. 默认Type被勾选为 Directory,一定要去掉勾选。

4:多应用访问效果如下

5: 特别注意 -- 因为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

目录
打赏
0
2
2
0
194
分享
相关文章
【Azure Function】Function App门户上的Test/Run返回错误:Failed to fetch
Running your function in portal requires the app to explicitly accept requests from https://portal.azure.cn. This is known as cross-origin resource sharing (CORS).Configure CORS to add https://portal.azure.cn to allowed origins.
【Azure App Service】基于Linux创建的App Service是否可以主动升级内置的Nginx版本呢?
基于Linux创建的App Service是否可以主动升级内置的Nginx版本呢?Web App Linux 默认使用的 Nginx 版本是由平台预定义的,无法更改这个版本。
126 77
|
19天前
|
C#
【Azure Function】Function App出现System.IO.FileNotFoundException异常
Exception while executing function: xxxxxxx,The type initializer for 'xxxxxx.Storage.Adls2.StoreDataLakeGen2Reading' threw an exception. Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the
114 64
|
1月前
|
【Azure App Service】对App Service中CPU指标数据中系统占用部分(System CPU)的解释
在Azure App Service中,CPU占比可在App Service Plan级别查看整个实例的资源使用情况。具体应用中仅能查看CPU时间,需通过公式【CPU Time / (CPU核数 * 60)】估算占比。CPU百分比适用于可横向扩展的计划(Basic、Standard、Premium),而CPU时间适用于Free或Shared计划。然而,CPU Percentage包含所有应用及系统占用的CPU,高CPU指标可能由系统而非应用请求引起。详细分析每个进程的CPU占用需抓取Windows Performance Trace数据。
94 40
|
2月前
|
API
【Azure Logic App】使用Logic App来定制Monitor Alert邮件内容遇见无法获取SearchResults的情况
Log search alert rules from API version 2020-05-01 use this payload type, which only supports common schema. Search results aren't embedded in the log search alerts payload when you use this version.
54 10
【Azure Container App】Container Apps 设置延迟删除 (terminationGracePeriodSeconds) 的解释
terminationGracePeriodSeconds : 这个参数的定义是从pod收到terminated signal到最终shutdown的最大时间,这段时间是给pod中的application 缓冲时间用来处理链接关闭,应用清理缓存的;并不是从idel 到 pod被shutdown之间的时间;且是最大时间,意味着如果application 已经gracefully shutdown,POD可能被提前terminated.
【Azure App Service】在App Service中调用Stroage SDK上传文件时遇见 System.OutOfMemoryException
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
【Azure App Service】在App Service上关于OpenSSH的CVE2024-6387漏洞解答
CVE2024-6387 是远程访问漏洞,攻击者通过不安全的OpenSSh版本可以进行远程代码执行。CVE-2024-6387漏洞攻击仅应用于OpenSSH服务器,而App Service Runtime中并未使用OpenSSH,不会被远程方式攻击,所以OpenSSH并不会对应用造成安全风险。同时,如果App Service的系统为Windows,不会受远程漏洞影响!

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等