Home  >  Article  >  Java  >  How to configure and switch Tomcat with SpringBoot

How to configure and switch Tomcat with SpringBoot

王林
王林forward
2023-05-27 22:29:271627browse

1. Basic introduction

WebServer supported by SpringBoot: Tomcat, Jetty, or Undertow

How to configure and switch Tomcat with SpringBoot

How to configure and switch Tomcat with SpringBoot

##SpringBoot application When starting a web application. web scene package - import tomcat

Supports the configuration and switching of Tomcat (can also be Jetty, Undertow)

2. Built-in Tomcat configuration

1.Through application. yml completed configuration

server:

#Configure port
port: 9999
#Configure tomcat
tomcat:
threads:
#Indicates the maximum of worker threads (the bank has 10 counters, each counter handles 200 requests 10*200=maximum number of connections), the default is 200
max: 10
#The default minimum worker thread is 10
min- spare: 5
#The thread started by tomcat reaches the maximum value, and the number of queued requests is accepted, the default is 100
accept-count: 200
#Maximum number of connections, number of concurrency
max-connections: 2000
#The timeout for establishing a connection, the default is 20 seconds, in milliseconds
connection-timeout: 10000

2. Configure Tomcat through classes

Configure through classes Tomcat (Note: The configuration file can be configured more fully.)

Log out application.yml to configure tomcat and complete the test

/**
 * 通过类来配置Tomcat
 */
@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
    @Override
    public void customize(ConfigurableServletWebServerFactory server) {
        server.setPort(10000); //我们设置了server的端口为10000
    }
}

3. Switch WebServer

Demonstrate how to switch to Undertow

1. Exclude the embedded tomcat dependency

<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>

2.Introduce the undertow dependency

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

3. Note: Because the tomcat dependency is removed, the project is used If you go to tomcat related classes/interfaces, an error will be reported. Just log out/delete this part of the code, run the project, and complete the test

The above is the detailed content of How to configure and switch Tomcat with SpringBoot. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete