springboot内嵌tomcat,开发者可以打成fat jar包独立运行;也可以打成war包放到tomcat容器中运行。为了区分开发环境、测试环境、生产环境,springboot允许指定spring.profiles.active参数,这样可以在同一份application.yml里配置出不同环境。详细做法,可以参考《Spring Boot 配置优先级顺序》。

        但是我认为这个做法不好。因为能看代码的人很多,前述做法将生产环境的参数与代码混在一起,很容易泄露。而且配置文件与网站文件放在一起,从安全角度看也不好。

        最理想的做法,配置文件与网站文件分离,开发环境、测试环境、生产环境使用同一份网站文件,使用各自的配置文件。springboot提供spring.config.location参数用于指定外部配置文件。

        如果是单独fat jar方式独立运行,spring.config.location的用法为  java -jar 网站.jar --spring.config.location=配置文件的路径

        例如            java -jar demo.jar --spring.config.location=/opt/config/application.properties


        如果是可以打成war包放到tomcat容器中运行,相对麻烦一些。

        首先,配置文件放在在哪里?按照参考资料中的说法:

It is NOT recommended to place <Context> elements directly in the server.xml file. This is 
because it makes modifying the Context configuration more invasive since the main conf/server.xml 
file cannot be reloaded without restarting Tomcat.
不推荐在server.xml中进行配置,而是在/conf/context.xml中进行独立的配置。因为server.xml是不可动态重加载
的资源,服务器一旦启动了以后,要修改这个文件,就得重启服务器才能重新加载。而context.xml文件则不然,
tomcat服务器会定时去扫描这个文件。一旦发现文件被修改(时间戳改变了),就会自动重新加载这个文件,而不需要重启服务器。

        所以考虑建单独的配置文件。

Individual Context elements may be explicitly defined:

    * In an individual file at /META-INF/context.xml inside the application files. Optionally (based on the Host's copyXML attribute) this may be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to application's base file name plus a ".xml" extension.
    * In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The context path and version will be derived from the base name of the file (the file name less the .xml extension). This file will always take precedence over any context.xml file packaged in the web application's META-INF directory.
    * Inside a Host element in the main conf/server.xml.

       首先修改server.xml,建立自己service、engine、host,然后创建 $CATALINA_BASE/conf/[enginename]/[hostname]/context.xml。其内容如下:

      

<?xml version='1.0' encoding='utf-8'?>
<Context path="虚拟路径" docBase="war包路径" reloadable="true">
    <Parameter name="spring.config.location"
         value="application.yml路径"
         override="false" />
</Context>

       

AttributeDescription
description

Optional, human-readable description of this context initialization parameter.

name

The name of the context initialization parameter to be created.

override

Set this to false if you do not want a <context-param> for the same parameter name, found in the web application deployment descriptor, to override the value specified here. By default, overrides are allowed.

value

The parameter value that will be presented to the application when requested by calling ServletContext.getInitParameter().


经过实测,在context.xml里指定的application.yml确实生效。






参考资料:

Apache Tomcat 8 Configuration Reference        http://tomcat.apache.org/tomcat-8.0-doc/config/context.html#Context_Parameters

Tomcat 的context.xml                                         http://blog.csdn.net/heqingsong1/article/details/8539163

Spring Boot 配置优先级顺序                              http://www.cnblogs.com/softidea/p/5759180.html








Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐