Spring Boot 中 Tomcat 是怎么启动的

Spring Boot一个非常突出的优点就是不需要我们额外再部署Servlet容器,它内置了多种容器的支持。我们可以通过配置来指定我们需要的容器。

本文以我们平时最常使用的容器Tomcat为列来介绍以下两个知识点:

  • Spring Boot是怎么整合启动Tomcat容器的;
  • 在Spring Boot中,怎么进行Tomcat的深度配置。

Spring Boot整合启动Tomcat的流程

对于看源代码,每个人都有自己的方法。我自己在看源代码的时候喜欢结合IDEA的Debug功能一起看。比如说现在我们要研究Spring Boot是在哪个环节点启动Tomcat的,
我的思路是:Tomcat在启动时会调用各个组件的init方法和start方法,那么我只需要在这些方法上打上端点,然后就能在调用栈上看出Spring Boot是在哪个环节点启用
Tomcat的了。

按照这个思路,我在Tomcat的Connector组件的init方法上打了端点,通过调用栈能很清楚的看出Spring Boot是在容器的onRefresh方法中调用Tomcat的。

protected void onRefresh() {
    super.onRefresh();
    try {
        this.createWebServer();
    } catch (Throwable var2) {
        throw new ApplicationContextException("Unable to start web server",var2);
    }
}

找到了调用点,那么一切都好办了。从上面的方法中可以看出,重点内容就在this.createWebServer()这个方法中。

在Spring Boot中使用的容器类是ServletWebServerApplicationContext系列的容器,这个系列的容器可以内嵌Web容器。这个我们
可以从这个容器的属性和方法中可以看出来。

public class ServletWebServerApplicationContext extends GenericWebApplicationContext implements ConfigurableWebServerApplicationContext {
    //...省略部分代码
    public static final String DISPATCHER_SERVLET_NAME = "dispatcherServlet";
    //内嵌容器
    private volatile WebServer webServer;
    private ServletConfig servletConfig;
   
    //...省略部分代码
    //创建Web容器
    private void createWebServer() {
        WebServer webServer = this.webServer;
        ServletContext servletContext = this.getServletContext();
        //webServer和servletContext都是null,表示还没创建容器,进入创建容器的逻辑
        if (webServer == null && servletContext == null) {
            //获取创建容器的工厂,可以通过WebServerFactoryCustomizer接口对这个工厂进行自定义设置
            ServletWebServerFactory factory = this.getWebServerFactory();
            //具体的创建容器的方法,我们进去具体看下
            this.webServer = factory.getWebServer(new ServletContextInitializer[]{this.getSelfInitializer()});
        } else if (servletContext != null) {
            try {
                this.getSelfInitializer().onStartup(servletContext);
            } catch (ServletException var4) {
                throw new ApplicationContextException("Cannot initialize servlet context",var4);
            }
        }
        this.initPropertySources();
    }
}

下面是TomcatServletWebServerFactory的getWebServer方法。

public class TomcatServletWebServerFactory的getWebServer{
//...

@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
    //创建Tomcat容器
    Tomcat tomcat = new Tomcat();
    File baseDir = (this.baseDirectory != null ? this.baseDirectory
            : createTempDir("tomcat"));
    tomcat.setBaseDir(baseDir.getAbsolutePath());
    //创建连接器,默认NIO模式,可以通过WebServerFactoryCustomizer改变具体模式
    Connector connector = new Connector(this.protocol);
    tomcat.getService().addConnector(connector);
    //自定义连接器
    customizeConnector(connector);
    tomcat.setConnector(connector);
    tomcat.getHost().setAutoDeploy(false);
    configureEngine(tomcat.getEngine());
    //可以通过WebServerFactoryCustomizer添加额外的连接器,这边将这些连接器绑定到Tomcat
    for (Connector additionalConnector : this.additionalTomcatConnectors) {
        tomcat.getService().addConnector(additionalConnector);
    }
    //组测Listener、Filter和Servlet,自定义Context等操作
    //这个方法可以重点看下
    prepareContext(tomcat.getHost(),initializers);
    //创建TomcatWebServer,并调用start方法
    return getTomcatWebServer(tomcat);
}

//内嵌的Tomcat容器
public TomcatWebServer(Tomcat tomcat,boolean autoStart) {
		Assert.notNull(tomcat,"Tomcat Server must not be null");
		this.tomcat = tomcat;
		this.autoStart = autoStart;
        //这边触发Tomcat的启动流程,是Tomcat启动的入口点
		initialize();
}
//...省略部分代码
}

至此Spring Boot内嵌的Tomcat已将顺序启动了。那么Spring Boot是在什么时候注册DispatchServlet的呢?

配置Listener、Filter和Servlet

Spring Boot配置Listener、Filter和Servlet可以參考我之前写的文章Spring Boot使用嵌入式容器,那怎么配置自定义Filter呢

推荐使用ServletListenerRegistrationBean、FilterRegistrationBean和ServletRegistrationBean的方式注册Listener、Filter和Servlet。

Spring Boot注册DispatcherServlet

在传统的Spring MVC项目中,我们都会在web.xml中注册DispatcherServlet这个入口类,那么在Spring Boot中是在哪里注册的呢?

大家如果看Spring Boot的源代码,这边有个小技巧大家可以参考下。就是Spring Boot把之前传统项目中的配置项都通过AutoConfig的形式
做配置了。所以这边在寻找DispatcherServlet是在哪里配置的也可以顺着这个思路去寻找。

在IDEA的类查找功能中输入DispatcherServlet关键字,我们能看到一个DispatcherServletAutoConfiguration类。从名字上就能看出这个
类是DispatcherServlet的自动配置类,我们点进去看下是否是在这个类内部注册的DispatcherServlet?

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(DispatcherServlet.class)
@AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class)
@EnableConfigurationProperties(ServerProperties.class)
public class DispatcherServletAutoConfiguration {

	/*
	 * The bean name for a DispatcherServlet that will be mapped to the root URL "/"
	 */
	public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet";

	/*
	 * The bean name for a ServletRegistrationBean for the DispatcherServlet "/"
	 */
	public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration";

	@Configuration
	@Conditional(DefaultDispatcherServletCondition.class)
	@ConditionalOnClass(ServletRegistration.class)
	@EnableConfigurationProperties(WebMvcProperties.class)
	protected static class DispatcherServletConfiguration {

		private final WebMvcProperties webMvcProperties;

		public DispatcherServletConfiguration(WebMvcProperties webMvcProperties) {
			this.webMvcProperties = webMvcProperties;
		}

		@Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
		public DispatcherServlet dispatcherServlet() {
			DispatcherServlet dispatcherServlet = new DispatcherServlet();
			dispatcherServlet.setDispatchOptionsRequest(
					this.webMvcProperties.isDispatchOptionsRequest());
			dispatcherServlet.setDispatchTraceRequest(
					this.webMvcProperties.isDispatchTraceRequest());
			dispatcherServlet.setThrowExceptionIfNoHandlerFound(
					this.webMvcProperties.isThrowExceptionIfNoHandlerFound());
			return dispatcherServlet;
		}

		@Bean
		@ConditionalOnBean(MultipartResolver.class)
		@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
		public MultipartResolver multipartResolver(MultipartResolver resolver) {
			// Detect if the user has created a MultipartResolver but named it incorrectly
			return resolver;
		}

	}

	@Configuration
	@Conditional(DispatcherServletRegistrationCondition.class)
	@ConditionalOnClass(ServletRegistration.class)
	@EnableConfigurationProperties(WebMvcProperties.class)
	@Import(DispatcherServletConfiguration.class)
	protected static class DispatcherServletRegistrationConfiguration {

		private final ServerProperties serverProperties;

		private final WebMvcProperties webMvcProperties;

		private final MultipartConfigElement multipartConfig;

		public DispatcherServletRegistrationConfiguration(
				ServerProperties serverProperties,WebMvcProperties webMvcProperties,ObjectProvider<MultipartConfigElement> multipartConfigProvider) {
			this.serverProperties = serverProperties;
			this.webMvcProperties = webMvcProperties;
			this.multipartConfig = multipartConfigProvider.getIfAvailable();
		}

        //很熟悉的代码有没有,ServletRegistrationBean就是我们上一节中介绍的注册Servlet的方式
        //只不过这边注册的是DispatcherServlet这个特殊的Servlet
		@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
		@ConditionalOnBean(value = DispatcherServlet.class,name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
		public ServletRegistrationBean<DispatcherServlet> dispatcherServletRegistration(
				DispatcherServlet dispatcherServlet) {
			ServletRegistrationBean<DispatcherServlet> registration = new ServletRegistrationBean<>(
					dispatcherServlet,this.serverProperties.getServlet().getServletMapping());
			registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
			registration.setLoadOnStartup(
					this.webMvcProperties.getServlet().getLoadOnStartup());
			if (this.multipartConfig != null) {
				registration.setMultipartConfig(this.multipartConfig);
			}
			return registration;
		}

	}

//...省略部分代码
}

好了通过这边的介绍,我们直到DispatcherServlet是通过DispatcherServletAutoConfiguration这个自动配置类注册的。

Spring Boot中关于Tomcat的一些其他配置

server.tomcat.accept-count 100.0 Maximum queue length for incoming connection requests when all possible request processing threads are in use.(backlog的长度)
server.tomcat.accesslog.buffered true Whether to buffer output such that it is flushed only periodically.
server.tomcat.accesslog.check-exists false Whether to check for log file existence so it can be recreated it if an external process has renamed it.
server.tomcat.accesslog.condition-if Whether logging of the request will only be enabled if "ServletRequest.getAttribute(conditionIf)" does not yield null.
server.tomcat.accesslog.condition-unless Whether logging of the request will only be enabled if "ServletRequest.getAttribute(conditionUnless)" yield null.
server.tomcat.accesslog.directory logs Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.
server.tomcat.accesslog.enabled false Enable access log.
server.tomcat.accesslog.encoding Character set used by the log file. Default to the system default character set.
server.tomcat.accesslog.file-date-format .yyyy-MM-dd Date format to place in the log file name.
server.tomcat.accesslog.ipv6-canonical false Whether to use IPv6 canonical representation format as defined by RFC 5952.
server.tomcat.accesslog.locale Locale used to format timestamps in log entries and in log file name suffix. Default to the default locale of the Java process.
server.tomcat.accesslog.max-days -1.0 Number of days to retain the access log files before they are removed.
server.tomcat.accesslog.pattern common Format pattern for access logs.
server.tomcat.accesslog.prefix access_log Log file name prefix.
server.tomcat.accesslog.rename-on-rotate false Whether to defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.request-attributes-enabled false Set request attributes for the IP address,Hostname,protocol,and port used for the request.
server.tomcat.accesslog.rotate true Whether to enable access log rotation.
server.tomcat.accesslog.suffix .log Log file name suffix.
server.tomcat.additional-tld-skip-patterns Comma-separated list of additional patterns that match jars to ignore for TLD scanning. The special '?' and '*' characters can be used in the pattern to match one and only one character and zero or more characters respectively.
server.tomcat.background-processor-delay 10s Delay between the invocation of backgroundProcess methods. If a duration suffix is not specified,seconds will be used.
server.tomcat.basedir Tomcat base directory. If not specified,a temporary directory is used.
server.tomcat.connection-timeout Amount of time the connector will wait,after accepting a connection,for the request URI line to be presented.
server.tomcat.max-connections 8192.0 Maximum number of connections that the server accepts and processes at any given time. Once the limit has been reached,the operating system may still accept connections based on the "acceptCount" property.
server.tomcat.max-http-form-post-size 2MB Maximum size of the form content in any HTTP post request.
server.tomcat.max-swallow-size 2MB Maximum amount of request body to swallow.
server.tomcat.mbeanregistry.enabled false Whether Tomcat's MBean Registry should be enabled.
server.tomcat.processor-cache 200.0 Maximum number of idle processors that will be retained in the cache and reused with a subsequent request. When set to -1 the cache will be unlimited with a theoretical maximum size equal to the maximum number of connections.
server.tomcat.redirect-context-root true Whether requests to the context root should be redirected by appending a / to the path.
server.tomcat.relaxed-path-chars Comma-separated list of additional unencoded characters that should be allowed in URI paths. Only "< > [ \ ] ^ ` { | }" are allowed.
server.tomcat.relaxed-query-chars Comma-separated list of additional unencoded characters that should be allowed in URI query strings. Only "< > [ \ ] ^ ` { | }" are allowed.
server.tomcat.remoteip.host-header X-Forwarded-Host Name of the HTTP header from which the remote host is extracted.
server.tomcat.remoteip.internal-proxies Regular expression that matches proxies that are to be trusted.
server.tomcat.remoteip.port-header X-Forwarded-Port Name of the HTTP header used to override the original port value.
server.tomcat.remoteip.protocol-header Header that holds the incoming protocol,usually named "X-Forwarded-Proto".
server.tomcat.remoteip.protocol-header-https-value https Value of the protocol header indicating whether the incoming request uses SSL.
server.tomcat.remoteip.remote-ip-header Name of the HTTP header from which the remote IP is extracted. For instance,X-FORWARDED-FOR.
server.tomcat.resource.allow-caching true Whether static resource caching is permitted for this web application.
server.tomcat.resource.cache-ttl Time-to-live of the static resource cache.
server.tomcat.threads.max 200.0 Maximum amount of worker threads.
server.tomcat.threads.min-spare 10.0 Minimum amount of worker threads.
server.tomcat.uri-encoding UTF-8 Character encoding to use to decode the URI.
server.tomcat.use-relative-redirects false Whether HTTP 1.1 and later location headers generated by a call to sendRedirect will use relative or absolute redirects.

这边给出一个配置的列子

server:
  port: ${port:9999}
  tomcat:
    accept-count: 200
    #最好进行这段配置,默认会在tmp目录下创建,Linux有时会有定时任务删除tmp目录下的内容
    basedir: my-tomcat
    accesslog:
      enabled: true
      pattern: '%t %a "%r" %s %S (%b M) (%D ms)'
      max-http-post-size: 2MB
      max-swallow-size: 2M
    uri-encoding: GBK
    threads:
      max: 100
      min-spare: 10

具体使用时可以参考Spring Boo官网关于Tomcat的配置

一些其他类

Spring Boot还提供了很多自定义类,让用户对Tomcat的组件做自定义配置。这个符合Spring的设计哲学:只提供选择,而不是强制用户使用某项技术。

关于Tomcat的自定义配置类还有以下几个,大家可以按需使用。

  • WebServerFactoryCustomizer接口:自定义Web容易工厂
  • WebServerFactoryCustomizerBeanPostProcessor处理类:WebServerFactoryCustomizer类通过WebServerFactoryCustomizerBeanPostProcessor类生效
  • TomcatConnectorCustomizer:连接器自定义处理类
  • TomcatContextCustomizer:Context自定义接口

原文地址:https://www.cnblogs.com/54chensongxia

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


&lt;servlet&gt; &lt;servlet-name&gt;tomcatpooljsp&lt;/servlet-name&gt; &lt;jsp-file&gt;/WEB-INF/tomcatpool.jsp&lt;/jsp-file&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;tomcatpooljsp&lt;/servlet-nam...
遵循Java Servlet 规范第4节中的建议 ,Apache Tomcat实现了系统地重新加载Java类的方法,以允许在不重新启动整个服务器的情况下更新应用程序的组件。 此功能对于开发非常重要,因为事实证明,随着服务器启动和重启时间的延长,这会严重浪费开发人员的时间。实际上,Java EE堆栈应用服务器的服务器重新启动时间很慢,这是Tomcat广泛用于个人和企业级项目的推动力之一。但是,即使Tomcat也无法 像运行时重新加载应用程序一样快地启动。通过仅重新加载隔离的应用程序的更改的类,开发人员..
JMX(Java管理扩展)是一项非常强大的技术,可让您管理,监视和配置Tomcat MBean。如果您是Tomcat管理员,那么您应该熟悉如何在tomcat中启用JMX来监视堆内存,线程,CPU使用率,类以及配置各种MBean。在本文中,我将讨论如何使用JConsole启用并连接到Tomcat。我假设您已经安装了Tomcat(如果没有);您可以参考安装指南。转到安装了Tomcat的路径 转到bin文件夹 将文件创建为“ setenv.sh” 使用vi编辑器修改文件并添加以下内容
总览介绍 建立 取得Java 获取TomCat 将TomCat安装为Windows服务 将TomCat设置为Linux服务(系统化) 使用Nginx作为反向代理 基本用法 手动启动和停止TomCat 验证TomCat服务器正在运行 服务静态文件 服务Java服务器页面(JSP) 修改设定 部署网络应用 使用管理网页界面 创建一个TomCat管理员用户 访问管理网络应用 管理网络应用 结论 参考链接介绍在最简单的概念中,To.
PSI Probe是Lambda Probe的社区驱动分支,使用相同的开源许可证(GPLv2)分发。它旨在替换和扩展Tomcat Manager,从而使管理和监视Apache Tomcat实例更加容易。与许多其他服务器监视工具不同,PSI Probe不需要对现有应用程序进行任何更改。它通过可访问Web的界面提供所有功能,只需将其部署到服务器即可使用。这些功能包括:请求:即使在每个应用程序的基础上,实时监视流量。 会话:浏览/搜索属性,查看上一个IP,到期,估计大小。 JSP:浏览,查看源代码,进
监视和管理Tomcat目录介绍 启用JMX远程 使用JMX远程Ant任务管理Tomcat JMXAccessorOpenTask-JMX打开连接任务 JMXAccessorGetTask:获取属性值Ant任务 JMXAccessorSetTask:设置属性值Ant任务 JMXAccessorInvokeTask:调用MBean操作Ant任务 JMXAccessorQueryTask:查询MBean Ant任务 JMXAccessorCreateTask:远程创建MBean Ant任
1.tomcat与jetty都是一种servlet引擎,他们都支持标准的servlet规范和javaEE规范
“The origin server did not find a current representation for the target resource...
Artifacts是maven中的一个概念,表示某个module要如何打包,例如war exploded、war、jar、ear等等这种打包形式;
使用 IDEA 编辑器开发项目十分便捷,这里介绍使用 IDEA 编辑器添加 Tomcat
这篇“servlet和tomcat的知识点有哪些”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅...
这篇文章主要讲解了“Tomcat管理平台实例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Tomcat管理平...
本文小编为大家详细介绍“tomcat虚拟主机怎么配置”,内容详细,步骤清晰,细节处理妥当,希望这篇“tomcat虚拟主机怎么配置”文章能帮助大家解决疑惑,下面跟
今天小编给大家分享一下tomcat相关配置与eclipse集成的方法的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家
这篇“Tomcat之web应用的目录组成结构是怎样的”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,
今天小编给大家分享一下tomcat目录映射的方法的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大...
这篇“tomcat的环境怎么配置”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文...
环境:tomcat:apache-tomcat-7.0.35 cactiEZ:10.1系统:centos5.6_x64一、配置tomcat服务器1、添加账号vim tomcat-users.xml 重启tomcat2、安装snmp协议yum...
一、 软环下载地址软件链接地址https://files.cnblogs.com/files/jinrf/openssl-1.0.2-latest.tar.gzhttps://files.cnblogs.com/files/jinrf/apr-util-1.6...