server.xml中的环境/系统变量

时间:2012-08-12 21:57:22

标签: tomcat environment-variables system-variable

如何在tomcat server.xml,context.xml等配置文件中使用环境/系统变量?

我尝试使用${ENV_VAR_NAME}(对于环境和系统变量),${env.ENV_VAR_NAME}(对于环境变量)。似乎没有任何工作。

2 个答案:

答案 0 :(得分:15)

它是如何在我的盒子里实现的。

启动的Bash脚本:

#!/bin/sh

SMEMORY=1G
XMEMORY=1G

if [ $ENV == DEV ]; then
  port_shutdown="8005"
  port_http="8080"
  port_https="8443"
elif
  [ $ENV == SIT ]; then
  port_shutdown="8006"
  port_http="8081"
  port_https="8444"
elif
  [ $ENV == UAT ]; then
  port_shutdown="8007"
  port_http="8082"
  port_https="8445"
else
  echo "Unknown ENV"
  exit 1
fi

export CATALINA_OPTS=" ${SYSTEM_PROPS} -d64 -server -Xms$SMEMORY -Xmx$XMEMORY \
 -XX:+UseCodeCacheFlushing -XX:ReservedCodeCacheSize=64M \
 -XX:+HeapDumpOnOutOfMemoryError -XX:MaxPermSize=1024M \
 -Dport.http=${port_http} -Dport.https=${port_https} -Dport.shutdown=${port_shutdown}"

exec $CATALINA_HOME/bin/startup.sh

server.xml

<Connector
  port="${port.http}"
  protocol="HTTP/1.1"
  connectionTimeout="20000"
  redirectPort="${port.https}"
/>

看看过程:

$ ps ux | grep tomcat
... -Xms1G -Xmx1G ... -Denv=KIEV_DEV... -Dport.http=8084 -Dport.https=8446 -Dport.shutdown=8008...

检查端口:

$ netstat -anp | grep java
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 :::8084                     :::*                        LISTEN      23343/java
tcp        0      0 :::8446                     :::*                        LISTEN      23343/java

答案 1 :(得分:1)

可以通过将系统属性org.apache.tomcat.util.IntrospectionUtils.PropertySource设置为值org.apache.tomcat.util.digester.Digester$EnvironmentPropertySource在server.xml等文件中引用环境变量。

该系统属性自7.0起就可用,但是直到8.5才在文档中提到EnvironmentPropertySource

https://tomcat.apache.org/tomcat-9.0-doc/config/systemprops.html