外部 tomcat 中的自定义上下文路径

Custom context path in external tomcat

我制作了一个 Spring- 我想要在外部 运行 中启动的应用程序 tomcat 8.

在 Spring-Boot 应用程序中,可以使用 application.properties 中的 属性 server.context-path 选择上下文路径,但因为我使用的是外部 tomcat8、这个属性没用

因此,我查看了 tomcat-8 documentation,其中指出:

If you want to deploy a WAR file or a directory using a context path that is not related to the base file name then one of the following options must be used to prevent double-deployment:

  • Disable autoDeploy and deployOnStartup and define all Contexts in server.xml
  • Locate the WAR and/or directory outside of the Host's appBase and use a context.xml file with a docBase attribute to define it.

因为我不想污染server.xml,所以我选择了第二个选项。因此,我在 /home/myuser/myapp/application-1.0.0.war 中找到了 war,并在 conf/Catalina/localhost 下放置了一个上下文文件名 application-1.0.0.xml。此文件仅包含这两行:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/myapp" docBase="/home/myuser/myapp"/>

我可以在日志中看到 tomcat8 成功启动,应用程序似乎部署在 tomcat 管理器中但是:

注意 :我知道 war 是正确的,因为如果我将它放在 webapp 目录中(虽然使用默认上下文路径),它会起作用).

注意 :如果我将 application-1.0.0.xml 重命名为 foo.xml,tomcat 管理器显示应用程序部署在上下文路径 /foo(但仍未启动)。

有什么想法吗?

找到答案:

  • 关于上下文路径,上下文文件的属性 path 确实被忽略了:

This attribute must only be used when statically defining a Context in server.xml. In all other circumstances, the path will be inferred from the filenames used for either the .xml context file or the docBase.

  • 关于 docBase 属性,我误解了以下句子:

Locate the WAR and/or directory outside of the Host's appBase and use a context.xml file with a docBase attribute to define it.

实际上,在 docBase 中,我将路径放入包含 war 的目录,而不是 war 本身的路径。

如果对某人有帮助,关注(放在 conf\Catalina\localhost\service-discovery.xml 内)对我有用 Tomcat 9

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/service-discovery" docBase="D:/Projects/codebase/apps/service-discovery/target/service-discovery.war"/>