preface

When we use SpringBoot to develop Web applications, we will introduce the Spring-boot-starter – Web component, which contains Jetty, Tomcat, and Undertow servlet containers for you to choose. The default is the Tomcat container, so when you start a new project, you will see some information about Tomcat. As shown in the figure below, you can see that Spring Boot is started using the Tomcat container by default.

In daily work, the default configuration recommended by Spring Boot does not necessarily apply to all cases. According to the project configuration and environment, select the appropriate container to build a better project. Both Tomcat and Jetty are Servlet engines. Compare them to the Relationship between China and the United States, and while Jetty has grown to be a good Servlet engine, Tomcat is still in a strong position. In comparison, they all have their own advantages and disadvantages. After a long period of development, Tomcat has been widely accepted and recognized in the market. Compared with Jetty, Tomcat is still relatively stable and mature, especially in enterprise applications, Tomcat is still the first choice. But as Jetty has grown, so has its market share. The architecture of Jetty is simpler than that of Tomcat. The architecture of Jetty is based on Handler, and the main extension functions can be implemented using Handler, which makes the extension simple. However, The Tomcat architecture is designed based on containers, so you need to understand the overall design structure of Tomcat to extend it, which is not easy to extend. There is little difference in performance between Jetty and Tomcat. Jetty can handle a large number of connections simultaneously and stay connected for long periods of time, making it ideal for Web chat applications and more. The simplicity of Jetty’s architecture means that as a server, Jetty can load components on demand, reducing unneeded components and reducing server memory overhead, thus improving server performance. By default, Jetty uses NIO (non-blocking IO) ends to give it an advantage in handling I/O requests and performance when handling static resources. Tomcat is suitable for handling a small number of very busy links, which means that the overall performance of Tomcat is higher with a shorter link life cycle. In addition, Tomcat uses BIO (which blocks I/O) to process I/O requests by default and has poor performance when handling static resources. As a standard Servlet engine, they both support standard servlets and Java EE specifications. Jetty’s application is faster, easy to modify, and has better support for the new Servlet specification. Tomcat is currently widely used, with more comprehensive support for JavaEE and servlets, and many features will be directly integrated.

Rely on the import

By default, because The Spring-boot-starter – Web comes with Tomcat, if we want to use another container, we need to remove its dependencies and reintroduce the new container. In the pom. XML file, add the spring-boot-starter-jetty dependencies, and we need to exclude the default spring-boot-starter-tomcat dependencies for the spring-boot-starter-web, as shown below:

<dependencies>
    <dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-web</artifactid>
        <exclusions>
            <! -- Remove Tomcat container -->
            <exclusion>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-starter-tomcat</artifactid>
            </exclusion>
        </exclusions>
    </dependency>
    <! -- Add a Jetty container -->
    <dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-jetty</artifactid>
    </dependency>
</dependencies>
Copy the code

Jetty property configuration

attribute The default value instructions
server.jetty.accesslog.enabled false Whether to enable Jetty logging
server.jetty.accesslog.dir Directory where access logs are stored
server.jetty.threads.max Maximum thread count
server.jetty.threads.min Minimum number of threads
server.jetty.threads.max-queue-capacity Maximum queue size
server.jetty.threads.idle-timeout Maximum idle time of a thread

Write tests

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/** * Take today's best performance as the latest starting point for tomorrow... ~ * <p> * Today the best performance as tomorrow newest starter! * * @ Class description: Start class *@author<a href="mailto:[email protected]"> </a> * @@since: the JDK 1.8 * /
@SpringBootApplication
public class JettyApplication {
    public static void main(String[] args) { SpringApplication.run(JettyApplication.class,args); }}Copy the code

Run the boot class and the effect is as follows: