简体   繁体   English

Tomcat生产/开发环境

[英]Tomcat production / dev environments

In PHP development, its possible to determine whether or not the app is running in a production or a development environment from the servers 'environment' variable. 在PHP开发中,可以从服务器的环境变量确定应用程序是在生产环境还是开发环境中运行。

Is there a similar variable available on tomcat servers, or is there a better way of targetting applications for production and development? tomcat服务器上是否有类似的变量可用,或者是否有更好的方法将应用程序用于生产和开发?

Every Tomcat instance we have has an isProduction flag defined in the GlobalNamingResources section of the server.xml file. 我们拥有的每个Tomcat实例都在server.xml文件的GlobalNamingResources部分中定义了一个isProduction标志。

server.xml : server.xml

<Server ...>
  ...
  <GlobalNamingResources>
    <Environment name="isProduction" value="false" type="java.lang.Boolean" override="false" /> 
  </GlobalNamingResources>

  <Service name="Catalina">
  ... etc ...
  </Service>
</Server>

This allows the property to be available throughout the app by creating a property in the context.xml that references the resource: 这允许通过在context.xml中创建引用资源的属性来在整个应用程序中使用该属性:

context.xml : context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context ...>
  <ResourceLink name="isProduction" global="isProduction" type="java.lang.Boolean" />
  ...
</Context>

To fetch the value: 要获取值:

public boolean isProduction() {
   Object o;
   try {
      o = (new InitialContext()).lookup("java:comp/env/isProduction");
   }  catch (NamingException e) {
      o = Boolean.FALSE; // assumes FALSE if the value isn't declared
   }
   return o == null ? Boolean.FALSE : (Boolean) o;
}

You can't do such thing by default. 默认情况下你不能这样做。 In any case, do not rely on the container to determine whenever the app is in the environment X. I'd say that you should do it using one of the following methods (in order of preference): 在任何情况下,不要依赖容器来确定应用程序何时在环境X中。我会说你应该使用以下方法之一(按优先顺序):

  1. Use whatever your build tool provides. 使用构建工具提供的任何内容。 Eg: use maven profiles. 例如:使用maven配置文件。 http://maven.apache.org/guides/introduction/introduction-to-profiles.html http://maven.apache.org/guides/introduction/introduction-to-profiles.html
  2. Use a property file for the app with a "mode" property. 使用具有“mode”属性的应用程序的属性文件。
  3. Pass a -Dmyproject.mode=XXXX property to the JVM 将-Dmyproject.mode = XXXX属性传递给JVM
  4. Use an OS system property 使用OS系统属性

I encourage you to use something like #1. 我鼓励你使用像#1这样的东西。 For sure you are using some kind of tool to build your app (Ant, SBT, etc). 当然,您正在使用某种工具来构建您的应用程序(Ant,SBT等)。

Imagine if by mistake somebody reinstall Tomcat, remove the OS properties or similar. 想象一下,如果有人错误重新安装Tomcat,删除操作系统属性或类似。 Your app might run in prod mode. 您的应用可能会以产品模式运行。

You could set up OS environment variables in tomcat startup scripts (run.sh at linux environment, for example) and read them from your program. 您可以在tomcat启动脚本(例如linux环境中的run.sh)中设置OS环境变量,并从程序中读取它们。 Also you could setup java environment variables (like this: Passing environment variables to a JVM, in a platform-independent manner ). 您还可以设置java环境变量(如下所示: 以与平台无关的方式将环境变量传递给JVM )。 I personally use different property files for dev/prod/etc and read this variable for property file. 我个人使用dev / prod / etc的不同属性文件,并为属性文件读取此变量。 Only required property file is deployed. 仅部署了必需的属性文件。

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

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