web项目部署在不同环境中需要修改配置文件的解决方法

web项目部署中存在的配置文件问题:

web项目以war包的形式,部署在tomcat中,同时项目需要访问一些其他的东东,例如访问数据库,调用别的项目的API。在开发中,这些需要访问的外部地址通常以配置文件的形式存在于项目中。如下图:

在更换项目的部署环境后,或者调用的外部应用地址发生变化,就需要更改配置文件。

最直接的问题:

    发布了一个测试包,放在测试环境上,测试通过之后,要将测试包发布到生产环境中,此时测试环境与生产环境的各种配置的地址是不同的,解决方案有两种

(1)将配置文件修改为生产环境的配置,重新打包。

(2)将测试环境的包,升级到生产环境后,修改配置文件为生产环境的配置文件。

不管是哪种方式都是存在风险的。对于(1)来说升级到生产环境的包并不是测试通过的包;对于(2)来说增加了人工成本和出错概率。

解决方案:

如果配置文件中,需要变化的地址使用变量的形式,项目在被部署到不同的环境中后,读取各自环境中的变量,是不是就可以解决这个问题了呢。

步骤:

(1)在环境变量中增加一个参数配置,tomcat启动后或者项目启动后可以加载改参数

(2)将项目中的配置文件修改成已经定义的变量

(3)项目的代码部分在加载配置文件的时候,可以自动替换掉变量

具体实现:

(1)修改tomcat的Catalina.sh文件

在cygwin=false上面增加JAVA_OPTS="-DbaseDomain=xxxxxx"

设置了变量之后,在java的项目中可以使用System.getProperty("baseDomain")来获取。

(2)修改项目的配置文件,需要修改的内容使用${baseDomain}替换。

(3)项目中解析${baseDomain}

配置完成之后,即可初步实现build once,run any enviroment了。

--------------

附上解析配置文件中${baseDomain}的代码

import java.util.Properties;

import org.apache.log4j.helpers.LogLog;

public class OptionConverterUtil {
	 static String DELIM_START = "${";
	  static char   DELIM_STOP  = '}';
	  static int DELIM_START_LEN = 2;
	  static int DELIM_STOP_LEN  = 1;
	  
    public static
    String substVars(String val, Properties props) throws
                          IllegalArgumentException {

      StringBuffer sbuf = new StringBuffer();

      int i = 0;
      int j, k;

      while(true) {
        j=val.indexOf(DELIM_START, i);
        if(j == -1) {
  	// no more variables
  	if(i==0) { // this is a simple string
  	  return val;
  	} else { // add the tail string which contails no variables and return the result.
  	  sbuf.append(val.substring(i, val.length()));
  	  return sbuf.toString();
  	}
        } else {
  	sbuf.append(val.substring(i, j));
  	k = val.indexOf(DELIM_STOP, j);
  	if(k == -1) {
  	  throw new IllegalArgumentException('"'+val+
  		      "" has no closing brace. Opening brace at position " + j
  					     + '.');
  	} else {
  	  j += DELIM_START_LEN;
  	  String key = val.substring(j, k);
  	  // first try in System properties
  	  String replacement = getSystemProperty(key, null);
  	  // then try props parameter
  	  if(replacement == null && props != null) {
  	    replacement =  props.getProperty(key);
  	  }

  	  if(replacement != null) {
  	    // Do variable substitution on the replacement string
  	    // such that we can solve "Hello ${x2}" as "Hello p1" 
              // the where the properties are
  	    // x1=p1
              // x2=${x1}
  	    String recursiveReplacement = substVars(replacement, props);
  	    sbuf.append(recursiveReplacement);
  	  }
  	  i = k + DELIM_STOP_LEN;
  	}
        }
      }
    }
    
    public static String getSystemProperty(String key, String def) {
      try {
        return System.getProperty(key, def);
      } catch(Throwable e) { // MS-Java throws com.ms.security.SecurityExceptionEx
        LogLog.debug("Was not allowed to read system property ""+key+"".");
        return def;
      }
    }
}

在第三步配置文件参数解析操作过程中参考了log日志的做法,解析方法也是从log4j的源代码中copy出来的。

---------------

看了一下spring-boot,如果使用spring-boot,则不需要上述设置,直接配置不同的配置文件。spring-boot会根据不同的环境加载不同的配置文件,原理感觉和上述类似。

原文地址:https://www.cnblogs.com/yimiyan/p/8492967.html