如何在 Tomcat 8.5 中部署 Spring boot 时使用外部配置文件 [英] How to use external configuration files with Spring boot deployed in Tomcat 8.5

查看:77
本文介绍了如何在 Tomcat 8.5 中部署 Spring boot 时使用外部配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Spring Boot 应用程序 (2.1.1.RELEASE) 作为 WAR 部署在 Debian 9 系统下的 Tomcat 8.5 服务器中.它使用以下文件来配置应用程序:

My Spring Boot application (2.1.1.RELEASE) is deployed as a WAR in a Tomcat 8.5 server under a Debian 9 system. It uses, among others, the following files to configure the application :

  • myApplication.properties(Spring 的主要配置文件)
  • log4j2.xml

两者都在 src/main/resources 下.

我的问题是关于如何以允许我拥有包含这两个文件的目录 /home/oliver/conf 的方式配置 Tomcat 和 Spring Boot,以便覆盖默认值在 src/main/resources 下定义(然后在分解的 WAR 中的 WEB-INF/classes 中).

My question is about how to configure Tomcat and Spring Boot in a way that allow me to have a directory /home/oliver/conf which contains both of these files, in order to override the defaults defined under src/main/resources (which are then in WEB-INF/classes in the exploded WAR).

以下是我采取的步骤.

首先,作为我正在处理的项目的要求,我通过编辑 /etc/init.d/tomcat8 将默认的 Tomcat 基目录更改为指向另一个位置:>

First, and as a requirement for the project I'm working on, I changed the default Tomcat base directory to point to another place by editing /etc/init.d/tomcat8 :

CATALINA_HOME=/usr/share/tomcat8
CATALINA_BASE=/home/oliver

我的 /home/oliver/conf 文件夹,其中包含 Tomcat 和 Spring 配置,如下所示:

My /home/oliver/conf folder, which holds the Tomcat and Spring configs, looks like this :

- Catalina/
- context.xml
- web.xml
- server.xml
- ...
- myApplication.properties
- log4j2.xml
- otherAppConfFile.properties
- ...

因为 Spring 默认查找 application.properties,所以我使用 @PropertySource 注释来指定另一个文件:

Because Spring looks for application.properties by default, I'm using the @PropertySource annotation to specify another file :

@SpringBootApplication
@PropertySource({classpath: myApplication.properties})
public class MyApp extends SpringBootServletInitializer {...}

我尝试将 -Dspring.config.location=file:/home/oliver/conf/myApplication.properties 添加到 /中定义的 JAVA_OPTSetc/default/tomcat8,它可以工作(正确覆盖嵌入的文件),但是例如,如果我尝试将 file:/etc/oliver/conf/log4j2.xml 添加到以前的 JVM 参数,它不起作用.

I've tried to add -Dspring.config.location=file:/home/oliver/conf/myApplication.properties to JAVA_OPTS defined in /etc/default/tomcat8, and it works (overrides the embedded file correctly), but for instance if I try to add file:/etc/oliver/conf/log4j2.xml to the previous JVM parameter, it doesn't work.

我阅读了一些关于 Spring环境配置文件"的内容,但如果可能的话,我不想使用它们.

I read a bit about Spring "environment profiles" but don't wish to use them if possible.

当我启动 Tomcat 并发出 ps aux |grep tomcat 命令,我看到按预期定义的所有 JAVA_OPTS 参数,我还看到以下内容:

When I launch Tomcat and issue a ps aux | grep tomcat command, I see all the JAVA_OPTS parameters defined as expected, and I also see the following :

-classpath :/home/oliver/conf:/usr/share/tomcat8/bin/.... -Dcatalina.base=/home/oliver -Dcatalina.home=/usr/share/tomcat8

我对 Tomcat 的类路径和 Spring 的相关方式以及我应该如何解决这个问题感到有些困惑.

I'm a bit confused about the way Tomcat's classpath and Spring's are related, and how I should solve this issue.

如果我在启动时看到的类路径包含 /home/oliver/conf 目录,为什么里面的文件没有覆盖嵌入的属性文件(myApplication.properties、log4j2.xml ...)?文件夹是否可见并添加到 Spring 的类路径中?

If the classpath I see at launch includes the /home/oliver/conf directory, why are the files inside not overriding the embedded property files (myApplication.properties, log4j2.xml ...) ? Is the folder seen and added to Spring's classpath ?

作为旁注,/home/oliver/conf 下可能有各种文件需要考虑,例如 log4j2.xml+ myApplication.properties + keystore.jks 所以我不确定我是否可以依赖 -Dspring.config.location-Dlogging.config 完全.

As a side note, there might be a variety of files under /home/oliver/conf which would need to be taken into account, for instance log4j2.xml + myApplication.properties + keystore.jks so I'm not sure I can rely on -Dspring.config.location and -Dlogging.config entirely.

推荐答案

我从你的问题中了解到你正在尝试做这样的事情.

What I understand from your question is that you are trying to do something like this.

-Dspring.config.location=file:/etc/oliver/conf/log4j2.xml

我认为属性 spring.config.location 是提供用于配置的属性文件的位置,而不是 log4j2.xml.您可以在 myApplication.properties 中通过设置 logging.config 的值来设置日志文件的位置,例如

I think the property spring.config.location is to provide the location of a properties file for configuration and not log4j2.xml. You can set the location of the log file in myApplication.properties by setting the value for logging.config like

logging.config=file:/etc/oliver/conf/log4j2.xml

或者你可以试试

-Dlogging.config=file:/etc/oliver/conf/log4j2.xml

更新

这就是我在我的生产系统中所做的.创建文件 setenv.sh 并输入以下命令.

This is what I do in my production systems. Create a file setenv.sh and enter below command.

export JAVA_OPTS="$JAVA_OPTS -Djavax.net.ssl.trustStore=/path/to/keystore/keystore.jks -Djavax.net.ssl.trustStorePassword=changeit -Dspring.profiles.active=qa -Dspring.config.location=/path/to/config/ -Dfws_log=/path/to/logfile/location -Xms512m -Xmx1024m -Dsecret.key=somesecretkey"

您可以在此文件中添加任意数量的键值映射,并且所有这些映射都将在您的 tomcat 启动时加载.

You can add any number of key value mappings in this file and all of them will be loaded when your tomcat starts.

这篇关于如何在 Tomcat 8.5 中部署 Spring boot 时使用外部配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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