如何区分应用程序中的测试和生产属性? [英] How to differentiate between test and production properties in an application?

查看:125
本文介绍了如何区分应用程序中的测试和生产属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一个大型J2ee电子销售解决方案。它有很多集成:CMS,ERP,邮件服务器等。所有这些系统分为测试和生产环境。

We're developing a big J2ee e-sales solution. It's got a lot of integrations: CMS, ERP, Mail server etc. All these systems are divided into test and production environments.

我们需要将我们的应用程序部署到具有测试配置的测试服务器,并且当部署到我们的生产服务器时,它应该使用生产配置。我们如何让我们的应用程序选择正确的属性?

We need to deploy our application to our test servers with test configuration and when deployed to our production servers it should use the production configuration. How do we make our application select the correct properties?

到目前为止我们尝试的是:

The thing we've tried so far is this:

我们所有的属性文件都包含测试属性和生产属性

All our property files contain test properties and production properties

test.mvxapi.server = SERV100TS
test.mvxapi.username = user
test.mvxapi.password = password
test.mvxapi.port = 6006
test.mvxapi.cono = 600

mvxapi.server = SERV10001
mvxapi.username = user
mvxapi.password = password
mvxapi.port = 6001
mvxapi.cono = 100

读取这些属性的Util有一个开关:isTest(),它在键的前面加上test。

The Util that reads these properties has a switch: isTest() which prefixes the key with "test."

public String getProperty(String property)
{
    return properties.getProperty(prefix + "" + property);
}

交换机由我们的构建服务器创建的另一个属性设置。构建.EAR时,我们的生产服务器的脚本将(输入到build.xml)isProduction = true注入到system.properties中。

The switch is set by another property which is created by our build server. When the .EAR is built the script for our production servers injects (input to build.xml) "isProduction=true" into system.properties.

<propertyfile file="${buildDir}/system.properties">
        <entry  key="isProduction" value="${systemType}"/>
    </propertyfile>

我不确定这是最好的方法。如果由于某种原因,isProduction = false被错误地提交到我们的生产环境,那么一切都很糟糕。

I'm not sure this is the best way to do it. If for some reason "isProduction=false" is committed wrongly to our production environment all hell is loose.

我读过人们在服务器上本地拥有属性。但我们真的不想让文件传播开来。我们有生产服务器集群。确保每个服务器都有正确的属性文件似乎不是故障安全的

I've read people have properties locally on the server. But we really don't want to have files spread around. We have cluster of production servers. Making sure every server has the right property file doesn't seem fail-safe

推荐答案

您要避免的是配置在EAR中的文件,问题在于您需要针对不同环境使用不同的EAR,并且更改配置文件需要重建。

What you want to avoid is having the config file inside the EAR, the problem with this is that you need different EAR's for different environments, and also, changing the config file requires a rebuild.

而是将相同的 EAR部署到每个服务器,但使用不同的URL资源配置每个服务器。 iow,将 JNDI URL资源添加到您部署到该资源的配置文件的所有服务器上。如果您只读取了对repo的SVN访问权限,则在svn repo上创建配置文件,或者通过URL访问任何repo。这里很酷的是,你的所有配置都是集中的,因此很容易管理它们。

Rather deploy the same EAR to every server but configure each server with a different URL resource. iow, add a JNDI URL resource to all the servers you deploy to that point to the config file for that resource. If you have read only SVN access to your repo then create the config files on the svn repo, or any repo you can access via a URL. The cool thing here is that all your configuration is centralized and thus managing them is easy.

我所做的(用spring定制)确保 JNDI URL资源可选。所以,如果它在那里,应用程序将使用它,如果没有,它将不会。该应用程序启动是否存在。这样,即使在没有 JNDI 资源的情况下运行,该应用仍然有效(例如开发环境)。

What I've done (by customizing with spring) is make sure that JNDI URL resource optional. So, if it's there, the app will use it, if not, it won't. The app starts up whether it's there or not. That way, even when running with no JNDI resource available, the app still works (development environment for example).

这篇关于如何区分应用程序中的测试和生产属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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