我可以在多个环境中使用单个 war 文件吗?我是不是该? [英] Can I use a single war file in multiple environments? Should I?

查看:17
本文介绍了我可以在多个环境中使用单个 war 文件吗?我是不是该?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作中有一个 Java Web 应用程序,我想简化我们部署到我们的 DEV、QA 和 PROD 环境的方式.

I have a Java web application at my work and I'd like simplify how we deploy to our DEV, QA, and PROD environments.

应用在启动时读入了一系列的属性,dev、qa、prod的属性文件是不同的.每当我想部署到某个环境时,我都会将特定于环境的属性文件放入我的应用程序文件夹中,构建战争,然后将其部署到三个 tomcat 5.5 服务器之一.

The application reads in a series of properties at startup, and the properties files are different for dev, qa, and prod. Whenever I want to deploy to a certain environment I drop the environment-specific properties file into my app folder, build the war, and then deploy it to one of the three tomcat 5.5 servers.

我想要做的是拥有一个包含所有环境属性的 .war 文件,并让应用程序在初始化过程中询问网络服务器以确定应用程序所在的环境,以及哪些属性装载.有没有一种简单的方法(或者,如果失败了,一种标准的方法)来做到这一点?

What I would like to do is have to have a single .war that has the properties for all environments, and have the app interrogate the webserver during the init process to figure out which environment the app is in, and hence which properties to load. Is there an easy way (or, failing that, a standard way) to do that?

推荐答案

这实际上取决于您使用这些属性的目的.

This really depends on what you are using those properties for.

有些(例如数据源)可以在容器本身中配置(Tomcat 5.5.JNDI 资源,另请参阅 JDBC 资源部分).

Some (like data source, for example) can be configured in the container itself (Tomcat 5.5. JNDI Resources, see JDBC sources section as well).

其他(特定于应用程序)可能确实需要是属性.在这种情况下,您的选择是:

Others (application-specific) may indeed need to be properties. In which case your choices are:

  1. 在 WAR 文件中捆绑属性并根据某些外部开关(环境变量或 JVM 属性)加载适当的子集
  2. 在解压 war 的每台服务器上设置部署过程,并将属性文件(位于该服务器上的预定义位置并特定于该服务器)复制到 WEB-INF/classes(或其他合适的地方).
  1. Bundle properties within WAR file and load the appropriate subset based on some external switch (either environment variable or JVM property)
  2. Setup a deployment process on each of your servers where war is unpacked and a property file (located in a predefined location on that server and specific to that server) is copied over to WEB-INF/classes (or other appropriate place).

至于这是一个理想的目标吗"——是的,我认为是的.使用单个 WAR 在 QA/暂存中进行测试,然后部署到生产中会减少中间步骤,从而减少出错的机会.

As far as "is this a desirable goal" goes - yes, I think so. Having a single WAR to test in QA / staging and then deploy to production cuts out an intermediate step and thus leaves less chances for mistakes.

更新(基于评论):

上面的第 1 项是指实际的环境变量(例如,您在 Windows 中通过 SET ENV_NAME=QA 或在 Linux 中通过 ENV_NAME=QA; export ENV_NAME 设置的内容).您可以使用 System.getenv() 从您的代码中读取它的值并加载适当的属性文件:

Item #1 above refers to an actual environment variable (e.g. something that you set via SET ENV_NAME=QA in Windows or ENV_NAME=QA; export ENV_NAME in Linux). You can the read its value from your code using System.getenv() and load the appropriate properties file:

String targetEnvironment = System.getenv("TARGET_ENV");
String resourceFileName = "/WEB-INF/configuration-" + targetEnvironment + ".properties";
InputStream is = getServletContext().getResourceAsStream(resourceFileName);
Properties configuration = new Properties();
configuration.load(is);

但是是的,您可以改为通过 JNDI 定义标量值(参见 Tomcat 文档中的环境条目):

But yes, you can instead define a scalar value via JNDI (see Environment Entries in Tomcat doc) instead:

<Context ...>
  <Environment name="TARGET_ENV" value="DEV" type="java.lang.String" override="false"/>
</Context>

并通过

Context context = (Context) InitialContext().lookup("java:comp/env");
String targetEnvironment = (String) context.lookup("TARGET_ENV");
// the rest is the same as above

问题是,如果您无论如何都将使用 JNDI,您不妨放弃您的属性文件并通过 JNDI 配置所有内容.您的数据源将作为实际资源可供您使用,基本属性将保持标量(尽管它们将是类型安全的).

The thing is, if you will be using JNDI anyway, you might as well forgo your property files and configure everything via JNDI. Your data sources will be available to you as actual resources and basic properties will remain scalars (though they will be type safe).

最终由您决定哪种方式更适合您的特定需求;两者各有利弊.

Ultimately it's up to you to decide which way is better for your specific needs; both have pros and cons.

这篇关于我可以在多个环境中使用单个 war 文件吗?我是不是该?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆