SpringBoot内置Tomcat浅析
发布日期:2021-05-06 15:16:23 浏览次数:50 分类:精选文章

本文共 2162 字,大约阅读时间需要 7 分钟。

一、SpringBoot框架内置Tomcat,开发非常方便,随着SpringBoot的框架升级,内置Tomcat也更新版本。本文SpringBoot框架版本:2.2.10。

       1、如何查看SpringBoot的内置Tomcat的版本?

      2、SpringBoot的自动配置Tomcat在哪个包下?

           spring.boot.autoconfigure

 

       3、SpringBoot的内置Web容器是Tomca,还支持哪几种,是怎么设计扩展的?

      默认的属性都在ServerProperties中配置。

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true),忽略不识别的字段配置

WebServer接口定义Web容器的规范
public interface WebServer {	/**	 * Starts the web server. Calling this method on an already started server has no	 * effect.	 * @throws WebServerException if the server cannot be started	 */	void start() throws WebServerException;	/**	 * Stops the web server. Calling this method on an already stopped server has no	 * effect.	 * @throws WebServerException if the server cannot be stopped	 */	void stop() throws WebServerException;	/**	 * Return the port this server is listening on.	 * @return the port (or -1 if none)	 */	int getPort();}

看实现类:org.springframework.boot.web.embedded.tomcat

看一个TomcatWebServer的

跟进去看一下Tomcat的类,注入内置Tomcat后,是自动启动的。

public TomcatWebServer(Tomcat tomcat) {		this(tomcat, true);	}

在看一下初始化打出端口,使用的Apache的Logging组件打出的日志。

获取端口描述信息,如果不单独配置就获取本地默认的8080端口的。

      4、Tomcat的客户端访问日志如何配置?并保留一定时间

      application.yml中配置

tomcat:    basedir: /var/log/tomcat    accesslog:      enabled: true      directory: accessLog      pattern: '"%t","%{Http_X_Forwarded_For}i","%H","%m","%U%q","%s","%A","%D"'      prefix: access      suffix: .log      rename-on-rotate: true      max-days: 30      file-date-format: yyyy-MM-dd

       5、内置Tomcat如何切换Jetty等?

org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat

 引入Jetty包,一般都是用Tomcat的。

org.springframework.boot
spring-boot-starter-jetty

为啥可以根据倒包去依赖就可以实现Web容器的自动切换?Spring的条件注解,当类路径下存在Tomcat的类,就会条件注入这个Bean

源码参考:EmbeddedWebServerFactoryCustomizerAutoConfiguration

       6、优化Tomcat的最大连接数和线程数以及其他参数优化?

开启https连接,添加证书路径,以及生产环境证书密码加密等

后续使用重定向以及其他的加固项再整理吧,整体设计了解了。 

上一篇:单个java进程占用700%的CPU???
下一篇:Spring5框架工具类

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年04月02日 14时30分09秒