springboot-内置Tomcat的配置和切换

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

基本介绍

1.SpringBoot支持的webServer:TomcatJettyor Undertow

2.SpringBoot应用启动是Web应用时。web场景包-导入tomcat

3.支持对Tomcat(也可以是Jetty、Undertow)的配置和切换


配置Tomcat

方法一通过application.yml完成配置(推荐方式更全)

配置和ServerProperties.java关联通过查看源码得知有哪些属性配置

可以查看springboot笔记搜索“tomcat服务器配置”查看完整配置。

下面仅列出几个常用配置配置在resources.application.yml

server:
  #配置端口
  port: 9999
  tomcat: #对tomcat配置
    threads:
      max: 10 #最大的工作线程 默认是200
      min-spare: 5 #最小工作线程, 默认是10
    accept-count: 200 #tomcat启动的线程达到最大值, 接受排队的请求个数,默认100
    max-connections: 2000 #最大连接数, 并发数
    connection-timeout: 10000 #建立连接的超时时间, 单位是毫秒

方法二通过类配置Tomcat

演示通过server设置端口其他设置类似。该方法了解即可。

@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(10000); //我们设置了server的端口为10000
    }
}

切换WebServer,演示如何切换成Undertow

  • 在pom.xml先取消tomcat的引用

        <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>
  • 再引用undertow

      <!--引入undertow-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: SpringTomcat