Tomcat 8 - context.xml 在数据源中使用环境变量 [英] Tomcat 8 - context.xml use Environment Variable in Datasource

查看:76
本文介绍了Tomcat 8 - context.xml 在数据源中使用环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用数据源的 Tomcat 8 项目(见下文)

I have a Tomcat 8 project that uses a datasource (see below)

<Resource auth="Container" 
          name="jdbc/JtmDS"  
          driverClassName="org.apache.derby.jdbc.EmbeddedDriver" 
          type="javax.sql.DataSource" 
          username="xfer"
          password="xfer10" 
          url="jdbc:derby:/home/PUID/tm/control/JtmDB"                    
          initialSize="25"
          maxTotal="100" 
          maxIdle="30" 
          maxWaitMillis="10000"                                      
          removeAbandonedOnBorrow="true"
          removeAbandonedTimeout="20" />

这很好用.

但是url是硬编码路径/home/PUID/tm/control/JtmDB

However the url is a hard-coded path /home/PUID/tm/control/JtmDB

当它投入生产时,路径的 PUID 部分将在众多系统中有所不同.我有一个环境变量集 export PUID=abcd应用程序的其余部分能够在适当的情况下使用诸如 System.getenv( )${env:PUID} 之类的东西.

When this gets into production the PUID part of the path will differ across numerous systems. I have an environment variable set export PUID=abcd The rest of the application is able to use things like System.getenv( ) or ${env:PUID} as and where appropriate.

这些都可以正常工作.

我的问题很简单:如何使 context.xml 中的 PUID 值成为可以从环境变量中读取的变量?

My question is very simply: How can I make the PUID value in my context.xml a variable that can be read from an environment variable?

推荐答案

我终于发现了我在这里真正需要做的事情......最后很简单.

I finally discovered what I actually needed to do here.... Quite simple in the end.

我在运行时向 Tomcat 传递了一个 java 参数,如下所示.

I passed in a java parameter to Tomcat at runtime as shown below.

我将以下位添加到 setenv.sh

export PUID=abcd

JAVA_OPTS=-Dpuid=${PUID} 

然后编辑我的 context.xml,如下所示

Then edited my context.xml as shown here

<Resource auth="Container" 
          name="jdbc/JtmDS"  
          driverClassName="org.apache.derby.jdbc.EmbeddedDriver" 
          type="javax.sql.DataSource" 
          username="xfer"
          password="xfer10" 
          url="jdbc:derby:/home/${puid}/tm/control/JtmDB"                    
          initialSize="25"
          maxTotal="100" 
          maxIdle="30" 
          maxWaitMillis="10000"                                      
          removeAbandonedOnBorrow="true"
          removeAbandonedTimeout="20" />

所以现在我的 Tomcat 安装将读取此内容并能够为每个不同的 PUID 使用不同的路径.

So now my Tomcat installation will read this and be able to use a different path for each different PUID.

背景:这是可行的,因为 Tomcat 会自动在其配置文件中执行变量替换:

Background: This works because Tomcat will automatically perform variable substition in its configuration files:

Tomcat 配置文件被格式化为无模式 XML;元素并且属性区分大小写.

Tomcat configuration files are formatted as schemaless XML; elements and attributes are case-sensitive.

Apache Ant 风格的变量支持替换;一个名为 propname 的系统属性可以使用语法 ${propname} 在配置文件中使用.全部系统属性可用,包括使用 -D 设置的属性语法,那些由 JVM 自动提供的和那些在 $CATALINA_BASE/conf/catalina.properties 文件中配置.

Apache Ant-style variable substitution is supported; a system property with the name propname may be used in a configuration file using the syntax ${propname}. All system properties are available including those set using the -D syntax, those automatically made available by the JVM and those configured in the $CATALINA_BASE/conf/catalina.properties file.

Apache Tomcat 9 配置参考 - 概述

部分:

JAVA_OPTS=-Dpuid=${PUID}

上面的描述是必要的,因为 Tomcat 只会读取 Java 系统属性(由 JVM 提供),但不是环境变量(由运行 JVM 的操作系统/运行时库提供).参数-D从同名的环境变量中设置一个Java系统属性.

describe above is necessary because Tomcat will only read Java system properties (which are provided by the JVM), but not environment variables (which are provided by the OS/runtime libraries that the JVM is running on). The parameter -D sets a Java system property from the environment variable of the same name.

这篇关于Tomcat 8 - context.xml 在数据源中使用环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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