Talk about the configuration changes of springboot2 embeddedcontainer.

  springboot

Order

This article mainly studies the configuration changes of springboot2 embeded container.

springboot 1.x

import org.apache.catalina.connector.Connector;
import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;


@Component
public class EmbeddedTomcatConfig implements EmbeddedServletContainerCustomizer{

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        TomcatEmbeddedServletContainerFactory factory = (TomcatEmbeddedServletContainerFactory)container;
        factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
                protocol.setMaxConnections(200);
                protocol.setMaxThreads(200);
                protocol.setSelectorTimeout(3000);
                protocol.setSessionTimeout(3000);
                protocol.setConnectionTimeout(3000);
            }
        });
    }
}

The customization is realized by implementing the customization method of org.springframework.boot.context.embedded.embeddedservletcontainercustomizer.

springboot 2.x

import org.apache.catalina.connector.Connector;
import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;


@Component
public class EmbeddedTomcatConfig implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        ((TomcatServletWebServerFactory)factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
                protocol.setMaxConnections(200);
                protocol.setMaxThreads(200);
                protocol.setSelectorTimeout(3000);
                protocol.setSessionTimeout(3000);
                protocol.setConnectionTimeout(3000);
            }
        });
    }
}

In version 2.x, the customization method is changed to implement the org.spring framework.boot.web.server.webserverfactorycustomizer interface.

Summary

The relevant classes of springboot1.x are as follows:

  • org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer
  • org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer
  • org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer
  • org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory

The relevant classes of springboot2.x are as follows:

  • org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory
  • org.springframework.boot.web.server.WebServerFactoryCustomizer
  • org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer
  • org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory

The relevant changes can be clearly seen through comparison.

doc