Spring Boot customizes and optimizes the built-in Tomcat container

1. Spring Boot  customizes and optimizes the built-in Tomcat container.

> There are three built-in containers: Undertow, Jetty, Tomcat, Spring Boot implements these three containers respectively, and their upper interface is EmbeddedServletContainerFactory, which is also the main core of this article.

There are two main ways to customize and optimize the built-in container. The first way is to configure it through configuration files, and the other way is to use code. Next, the above two ways are mainly implemented.

2. Customize and optimize Tomcat through configuration files

> For the core content of the configuration, refer to the service property class org.springframework.boot.autoconfigure.web.ServerProperties. The following shows some of the configuration of tomcat

server:
  port: 8081
  # tomcat settings
  tomcat:
    accesslog:
    # Enable log access
      enabled: true
    # log save path
      directory: e:/tmp/logs

For more configuration content, refer to the built-in properties of the org.springframework.boot.autoconfigure.web.ServerProperties class.

3. Configure and optimize the built-in container through code

> There are two ways to optimize and customize the built-in container with code. The first is to implement the built-in Servlet container customizer (org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer), and hand over the class to the Spring container for management. One is to configure the EmbeddedServletContainerFactory interface implementation class in the Spring container. Here we mainly focus on the built-in Tomcat, that is, the TomcatEmbeddedServletContainerFactory class

3.1. The first way is to implement the EmbeddedServletContainerCustomizer interface and hand it over to the Spring container for management

@Component
public class MyEmbeddedServletContainerCustomizer implements EmbeddedServletContainerCustomizer {
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        //org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory 
        //Describe the default Tomcat container
        System.out.println(container.getClass());
        TomcatEmbeddedServletContainerFactory factory = (TomcatEmbeddedServletContainerFactory) container;
        //set the port
        factory.setPort(8088);
        //Set the root directory of Tomcat
        factory.setBaseDirectory(new File("d:/tmp/tomcat"));
        //Set the access log storage directory
        factory.addContextValves(getLogAccessLogValue());
        //Set the number of Tomcat threads and connections
        factory.addConnectorCustomizers(new MyTomcatConnectorCustomizer());
        //Initialize the servletContext object
        factory.addInitializers((servletContext) -> {
            System.out.println(" = = = = get server info = = " + servletContext.getServerInfo());
        });

    }
    private AccessLogValve getLogAccessLogValue() {
        AccessLogValve accessLogValve = new AccessLogValve();
        accessLogValve.setDirectory("d:/tmp/tomcat/logs");
        accessLogValve.setEnabled(true);
        accessLogValve.setPattern(Constants.AccessLog.COMMON_PATTERN);
        accessLogValve.setPrefix("springboot-access-log");
        accessLogValve.setSuffix(".txt");
        return accessLogValve;
    }
}

/**
 * Customize the number of connections and threads of tomcat
 */
class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer {
    @Override
    public void customize(Connector connector) {
        //Connection protocol HTTP/1.1
        System.out.println(connector.getProtocol());
        //Connection protocol handler org.apache.coyote.http11.Http11NioProtocol
        System.out.println(connector.getProtocolHandler().getClass());
        //Http11NioProtocol
        Http11NioProtocol protocolHandler = (Http11NioProtocol) connector.getProtocolHandler();
        // set the maximum number of connections
        protocolHandler.setMaxConnections(2000);
        // set the maximum number of threads
        protocolHandler.setMaxThreads(500);
    }
}

3.1. Configure the EmbeddedServletContainerFactory implementation class in the Spring container

@SpringBootConfiguration
public class WebServerConfiguration {
    @Bean
    public EmbeddedServletContainerFactory embeddedServletContainerFactory() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        //set the port
        factory.setPort(8089);
        //Set 404 error interface
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
        //Set to trigger when the container is initialized
        factory.addInitializers((servletContext) -> {
            System.out.println(" = = = = get server info = = " + servletContext.getServerInfo());
        });
        //Set the maximum number of connections and the maximum number of threads
        factory.addConnectorCustomizers((connector) -> {
            Http11NioProtocol protocolHandler = (Http11NioProtocol) connector.getProtocolHandler();
            protocolHandler.setMaxConnections(2000);
            protocolHandler.setMaxThreads(500);
        });
        //Set the directory for the access logging file
        factory.addContextValves(getLogAccessLogValue());
        return factory;
    }

    private AccessLogValve getLogAccessLogValue() {
        AccessLogValve accessLogValve = new AccessLogValve();
        accessLogValve.setDirectory("d:/tmp/logs");
        accessLogValve.setEnabled(true);
        accessLogValve.setPattern(Constants.AccessLog.COMMON_PATTERN);
        accessLogValve.setPrefix("SpringBoot-Access-Log");
        accessLogValve.setSuffix(".txt");
        return accessLogValve;
    }
}

4. Summary

This article mainly records the way to optimize and customize the built-in container, so as to deepen your understanding of SpringBoot.

Article source: https://my.oschina.net/serve/blog/1581862

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326212162&siteId=291194637