SpringBoot Web容器之Tomcat配置

1.前言

SpringBoot 项目中,可以内置 TomcatJettyUndertowNetty 等服务器容器。当我们添加了 spring-boot-starter-web 依赖后,默认会使用 Tomcat 作为 Web 容器。

2.Tomcat常用配置项

如果需要对Tomcat进行定制化配置,可以在springboot项目中的application.yml或application.properties文件中进行配置。

server:
  port: 8080 #配置程序端口,默认为8080
  servlet:
    context-path: / #配置访问路径,默认为/
    session:
      timeout: 30m #session失效时间,30m表示30分钟,如果不写单位,默认单位是秒
  tomcat:
    uri-encoding: utf-8 #配置Tomcat编码,默认为UTF-8,用于解码URI的字符编码
    connection-timeout: 60000ms #socket调用read()等待读取的时间,如果超出时间范围,主动断开连接
    max-connections: 8192 #服务器接受和处理的最大连接数,默认是8192
    accept-count: 100 #等待队列的最大队列长度,当前服务器最大线程数用完后,传入的连接请求会进入到等待队列中进行等待
    threads:
      max: 200 #最大工作线程数,max-connections为服务器最大连接数,但并不是同时在工作
      min-spare: 10 #最小工作线程数
    max-http-form-post-size: 2MB #tomcat限制了POST请求的大小,默认为2M,-1表示不限制
    max-swallow-size: 2MB #请求正文的大小
  compression:
    enabled: true #tomcat是否开启压缩,默认是关闭false
3.Tomcat所有配置项

要想知道SpringBoot支持Tomcat哪些配置项,我们可以通过SpingBoot的源码去查找。

3.1.进入ServletWebServerFactoryAutoConfiguration

在ServletWebServerFactoryAutoConfiguration这个类中,我们可以看到,相关配置信息是放在ServerProperties这个类中。

3.2.进入ServerProperties

在ServerProperties中,我们可以看到以server开头的一些相关配置属性,如果还想看到tomcat独有相关配置,需要进入ServerProperties.Tomcat这个内部类中。

3.3.进入ServerProperties.Tomcat

可以根据类中提供的相关参数含义在配置文件中定制化修改相关配置属性的默认值。

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Springboot使用内置tomcat作为默认web容器,无需额外安装Tomcat,只需在服务部署时直接启动jar包即可。这是因为在spring-boot-starter-web中,默认导入的是tomcat,所以启动时使用的web容器就是tomcat。如果你想配置使用其他的web容器,比如Undertow,可以按照以下步骤进行操作: 1. 修改pom.xml文件,排除默认引入的spring-boot-starter-tomcat依赖,并添加Undertow依赖: ```xml <dependencies> <!-- 导入web场景启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 排除tomcat starter --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- 引入undertow --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> </dependencies> ``` 通过这样的配置,你就可以使用Undertow作为springboot的内置web容器了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot如何使用内嵌Tomcat](https://blog.csdn.net/u011523825/article/details/126114624)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot内置Tomcat配置和切换](https://blog.csdn.net/weixin_39158966/article/details/129745702)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值