简体   繁体   English

在 Tomcat 中为应用程序定义环境变量,在部署之外

[英]Define environment variable for app in Tomcat, outside of deployment

I need to provide a config file path to my Java web application that runs in Apache Tomcat 10.1.我需要为我的 Java web 应用程序提供一个配置文件路径,该应用程序在 Apache Tomcat 10.1 中运行。

The config path must be set outside of the deployment .配置路径必须在部署之外设置。 It's a property of the machine / stage on which the application is deployed.它是部署应用程序的机器/阶段的属性。 And not an attribute of the application.而不是应用程序的属性。 So it must not be set in the web.xml .所以一定不能设置在web.xml

For example /srv/myapp/prod/config.ini or C:\EclipseDeveloperOnWindows\config.ini .例如/srv/myapp/prod/config.iniC:\EclipseDeveloperOnWindows\config.ini

I've thought about multiple options:我考虑过多种选择:

  • Operating system environment variable MYAPPCONFIG , like PATH or JAVA_HOME : Independent of my application and even independent of the Tomcat container.操作系统环境变量MYAPPCONFIG ,如PATHJAVA_HOME :独立于我的应用程序,甚至独立于 Tomcat 容器。 Downside: May become error-prone if I want to run multiple distributions / Tomcats with different configurations as the same system user.缺点:如果我想以同一系统用户的身份运行具有不同配置的多个发行版/Tomcat,可能会变得容易出错。
  • Java system property as a command line option for Tomcat in CATALINA_OPTS , eg -DmyApp.config=/some/path.ini Java 系统属性作为CATALINA_OPTS中 Tomcat 的命令行选项,例如-DmyApp.config=/some/path.ini
  • Tomcat context parameter in <CATALINA_BASE>\conf\server.xml . <CATALINA_BASE>\conf\server.xml中的Tomcat上下文参数 This way, each Tomcat instance (one CATALINA_HOME , multiple CATALINA_BASE ) could define a different value for the config path property.这样,每个 Tomcat 实例(一个CATALINA_HOME ,多个CATALINA_BASE )都可以为配置路径属性定义不同的值。 (Do I have to include a reference to the server.xml context parameter in the web.xml ? And can I look it up from ServletContext ?) (我是否必须在 web.xml 中包含对server.xml上下文参数的web.xml ?我可以从ServletContext中查找它吗?)
  • Tomcat environment entry in <CATALINA_BASE>\conf\server.xml - what are the differences / pros and cons of Tomcat context parameters vs. Tomcat environment entries? <CATALINA_BASE>\conf\server.xml中的 Tomcat 环境条目 - Tomcat 上下文参数与 Tomcat 环境条目的区别/优缺点是什么? (Can I look it up from ServletContext ?) (我可以从ServletContext中查找它吗?)
  • Something else, that maybe can be looked up using JNDI?其他东西,也许可以使用 JNDI 查找?

Did I miss some options?我错过了一些选择吗? And which one is recommended?推荐哪一个? Which approach do you use for what reason?您出于什么原因使用哪种方法?

Putting the configuration into the Context is probably your best option.将配置放入上下文可能是您的最佳选择。 Note that Contexts can be defined in various ways, not just in server.xml .请注意,上下文可以通过多种方式定义,而不仅仅是在server.xml中。

See the Defining a context in the Tomcat 10.1 docs for details.有关详细信息,请参阅 Tomcat 10.1 文档中的定义上下文

For our product, the same war file is deployed to the staging environment and the production environment.对于我们的产品,同一个war文件被部署到登台环境和生产环境。 The differences between the environments are exhaustively described by the two Context descriptors (one for each environment).两个上下文描述符(每个环境一个)详尽地描述了环境之间的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM