实践:在tomcat中为springboot应用配置context

        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








  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,你可以通过以下步骤来配置Tomcat: 1. 首先,在你的Spring Boot项目的pom.xml文件中,确保你已经添加了Tomcat作为依赖项。通常,Spring Boot默认使用嵌入式的Tomcat容器,所以这个依赖项应该已经存在了。如果没有,请添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <version>9.0.37</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>9.0.37</version> </dependency> ``` 2. 然后,在你的Spring Boot主类(带有@SpringBootApplication注解的类)中,添加以下代码来配置嵌入式的Tomcat容器: ```java import org.apache.catalina.connector.Connector; import org.apache.coyote.http11.Http11NioProtocol; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.annotation.Bean; @SpringBootApplication public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } @Bean public TomcatServletWebServerFactory tomcatEmbedded() { TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(); tomcat.addAdditionalTomcatConnectors(createStandardConnector()); return tomcat; } private Connector createStandardConnector() { Connector connector = new Connector(Http11NioProtocol.class.getName()); connector.setPort(8080); // 设置Tomcat的端口号 return connector; } } ``` 在上述示例中,我们创建了一个TomcatServletWebServerFactory bean,并将它添加到Spring Boot主类中。然后,我们使用createStandardConnector()方法来创建一个Connector,设置Tomcat的端口号为8080。你可以根据你的需求来修改这些配置。 3. 最后,你可以在application.properties或者application.yml文件中进行更多的Tomcat配置。例如,你可以配置Tomcat的线程池大小、连接超时等。以下是一些常见的配置项: ``` # application.properties server.tomcat.max-threads=200 server.tomcat.accept-count=200 server.tomcat.max-connections=1000 server.tomcat.uri-encoding=UTF-8 # application.yml server: tomcat: max-threads: 200 accept-count: 200 max-connections: 1000 uri-encoding: UTF-8 ``` 这些配置项将会覆盖默认的Tomcat配置。 这样,你就成功地将Tomcat配置到了你的Spring Boot应用程序中。在运行你的应用程序时,它将使用你配置Tomcat容器来提供服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值