简体   繁体   English

基于Heroku Tomcat的Deployment:context.xml环境变量

[英]Heroku Tomcat based Deployment : context.xml Environment variables

I have deployed a Java Tomat application to Heroku platform. 我已经在Heroku平台上部署了Java Tomat应用程序。 I needed a JDBC datasource. 我需要一个JDBC数据源。 So I have added a "local" context.xml in the "META-INF" directory through maven war plugin. 因此,我通过maven war插件在“ META-INF”目录中添加了一个“本地” context.xml。

Heroku provides a system environment variable JDBC_DATABASE_URL that should have served my purpose like below when added to context.xml : Heroku提供了一个系统环境变量JDBC_DATABASE_URL,当将其添加到context.xml时,应达到我的目的,如下所示:

<Resource name="jdbc/postgresqldb" auth="Container" type="javax.sql.DataSource"
    url="${JDBC_DATABASE_URL}"
    driverClassName="org.postgresql.Driver" initialSize="5" maxWait="5000"
    maxActive="120" maxIdle="5" validationQuery="select 1"
    poolPreparedStatements="true"/>

However, I could not make it work and have to resort to the following: 但是,我无法使其工作,因此必须采取以下措施:

<Resource name="jdbc/postgresqldb" auth="Container" type="javax.sql.DataSource"
    username="tzse*********"  password="bea7c190************************************************"
    url="jdbc:postgresql://ec2-54-163-227-202.compute-1.amazonaws.com:5432/****************?sslmode=require"
    driverClassName="org.postgresql.Driver" initialSize="5" maxWait="5000"
    maxActive="120" maxIdle="5" validationQuery="select 1"
    poolPreparedStatements="true"/>

I basically copied and hard coded the environment variables here. 我基本上在这里复制并硬编码了环境变量。 This works fine, but is not portable and absolutely ugly! 这可以正常工作,但不便于携带,而且绝对难看!

Any hint where I am going wrong and what I could do to get it right ? 有什么提示我哪里做错了,我该怎么做才能正确解决?

In Heroku console I can see: 在Heroku控制台中,我可以看到:

2017-08-30T04:38:19.523960+00:00 app[web.1]: Aug 30, 2017 4:38:19 AM org.apache.naming.NamingContext lookup 2017-08-30T04:38:19.523961+00:00 app[web.1]: WARNING: Unexpected exception resolving reference 2017-08-30T04:38:19.523981+00:00 app[web.1]: java.sql.SQLException: Cannot create JDBC driver of class 'org.postgresql.Driver' for connect URL '${JDBC_DATABASE_URL}' 2017-08-30T04:38:19.523983+00:00 app[web.1]: at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2160) 2017-08-30T04:38:19.523960 + 00:00 app [web.1]:2017年8月30日4:38:19 org.apache.naming.NamingContext查找2017-08-30T04:38:19.523961 + 00: 00 app [web.1]:警告:意外的异常解决参考2017-08-30T04:38:19.523981 + 00:00 app [web.1]:java.sql.SQLException:无法创建类'org.postgresql的JDBC驱动程序用于连接URL'$ {JDBC_DATABASE_URL}的.Driver'2017-08-30T04:38:19.523983 + 00:00 app [web.1]:位于org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java :2160)

Does this mean that the reference to environment variables inside context.xml is not referencing the environment variables at all? 这是否意味着对context.xml中的环境变量的引用根本没有引用环境变量?

As far as I know, context.xml does not support environment variables. 据我所知, context.xml不支持环境变量。 As an alternative, I would recommend writing the context.xml file on the fly when the app starts up. 作为替代方案,我建议在应用程序启动时即时编写context.xml文件。 You would do this by creating a special start.sh script you use to start your app, and putting the following code in it: 您可以通过创建用于启动应用程序的特殊start.sh脚本,然后在其中添加以下代码来做到这一点:

#!/usr/bin/env bash

cat << EOF > context.xml
<Resource name="jdbc/postgresqldb" auth="Container" type="javax.sql.DataSource"
url="${JDBC_DATABASE_URL}"
driverClassName="org.postgresql.Driver" initialSize="5" maxWait="5000"
maxActive="120" maxIdle="5" validationQuery="select 1"
poolPreparedStatements="true"/>
EOF

java -jar yourapp.jar

Of course, you'll need to replace the java -jar yourapp.jar command with the command you use to run your app (as seen in your Procfile or heroku ps ). 当然,您需要将java -jar yourapp.jar命令替换为用于运行应用程序的命令(如Procfileheroku ps )。 Then you're Procfile should contain: 然后,您Procfile应该包含:

web: start.sh

Because this shell script will have access to the JDBC_DATABASE_URL environment variable, the resulting context.xml file will contain the full URL. 因为此Shell脚本将有权访问JDBC_DATABASE_URL环境变量,所以生成的context.xml文件将包含完整的URL。

A bit late... but although context.xml might not support environment variables (such as $JDBC_DATABASE_URL ), it DOES support Java system properties. 有点晚了...但是尽管context.xml可能不支持环境变量(例如$JDBC_DATABASE_URL ),但它确实支持Java系统属性。 It's possible to pass an environment variable from Heroku as a system property to your app. 可以将环境变量作为系统属性从Heroku传递到您的应用程序。

Using a Procfile (which is basically a Java-command-with-prefix) and the webapp-runner : 使用Procfile (基本上是带前缀的Java命令)和webapp-runner

web: java -cp "target/dependency/*" $JAVA_OPTS -DDB_URL=$JDBC_DATABASE_URL webapp.runner.launch.Main --enable-naming --port $PORT target/*.war

Here, both $JAVA_OPTS and $JDBC_DATABASE_URL are environment variables, whereas -DDB_URL will create a system property. 这里, $JAVA_OPTS$JDBC_DATABASE_URL都是环境变量,而-DDB_URL将创建系统属性。 In context.xml, use ${DB_URL} : 在context.xml中,使用${DB_URL}

<Resource name="jdbc/postgresqldb" url="${DB_URL}" .../>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM