我有一个 context.xml,用于连接到数据库。
<?xml version='1.0' encoding='utf-8'?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<Resource name="jdbc/SS"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
username="a***b"
password="C********1"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://**********:****/a***b"
maxActive="100"
maxIdle="50"
minIdle="10"
testWhileIdle="true"
maxWait="30000"
maxAge="60000"
removeAbandoned="true"
removeAbandonedTimeout="600" />
</Context>
我需要从 secret 管理器获取数据库凭据,并通过替换硬编码的数据库凭据将值传递到 context.xml 中。
有什么办法可以实现这一点吗?
最佳答案
如果您想动态加载数据库凭据。可能仅在 Tomcat 加载 context.xml
时执行一次(因为 Tomcat 在启动时仅读取一次环境变量)。
请注意,在运行时,每当 contxt.xml
更改时,相关的 Web 应用程序都会重新加载。 Tomcat 未重新启动。
因此,技巧是将数据库凭据作为 JVM 参数/参数传递,如上面的 ${catalina.base}
共有 3 个阶段:
声明环境变量并设置值:
export DB_CREDENTIALS='*****'
好地方是Tomcat的用户登录脚本
.bash_profile
,或者Tomcat环境setenv.sh
为环境变量创建 JVM 参数(系统变量):将以下行添加到
setenv.sh
最后一行之前。-Denvironment.db.credentials=${DB_CREDENTIALS} \
使用/调用 context.xml 中声明的 JVM 参数。例如:
url="jdbc:postgresql://**********:****/${environment.db.credentials}"
关于java - 如何将数据库AWS secret 传递到tomcat context.xml中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64440020/