SpringBoot内置Tomcat的配置和切换

文章介绍了SpringBoot支持的webServer包括Tomcat、Jetty和Undertow,其中默认使用Tomcat。通过`application.yml`或自定义类可以配置Tomcat的各项参数,如端口、线程数等。若要切换到Undertow,需在`pom.xml`中排除Tomcat依赖并引入Undertow依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.基本介绍

  1. SpringBoot支持的webServer:Tomcat,Jetty,Undertow

  2. 因为在spring-boot-starter-web中,默认导入的是tomcat,因此启动时使用的web容器就是tomcat。

  3. 同时 SpringBoot 也支持对Tomcat(或者Jetty、Undertow)的配置和切换。

2.内置Tomcat的配置

2.1通过application.yml完成配置

application.properties配置大全

内置Tomcat的配置和ServerProperties.java关联,可以通过查看源码得知有哪些属性配置。

例如:

 
server:
port: 9090 #端口,默认8080
tomcat:
threads:
max: 100 # web容器最大工作线程数,默认200
min-spare: 5 # 最小工作线程数,默认10
accept-count: 200 # tomcat启动的线程达到最大值后,接受排队的请求个数,默认100
max-connections: 2000 #最大连接数(并发数),默认8192
connection-timeout: 10000 #建立连接的超时时间,单位为ms

2.2通过类来配置tomcat

配置文件可以配置得更全面,通过类来配置灵活性没有那么好

 
package com.li.thymeleaf.config;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;
/**
* @author 李
* @version 1.0
* 通过类来配置Tomcat
*/
@Component
public class TomcatInItConfig implements
WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory server) {
server.setPort(9090);
//还可以进行其他设置...
}
}

3.切换WebServer

演示如何将默认配置的webServer切换为Undertow。

(1)修改pom.xml,因为默认引入了spring-boot-starter-tomcat,因此要排除tomcat,再加入Undertow依赖

 
<!--导入SpringBoot父工程-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.3</version>
</parent>
<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>

(2)启动项目,后台输出:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值