1. Spring Boot customization and optimization of built-in Tomcat containers.
> There are three built-in containers, namely Undertow, Jetty, Tomcat, and Spring Boot. These three containers are implemented separately. The upper interfaces are all EmbeddedServletContainerFactory, which is also the main core of this article.
There are two main ways to customize and optimize built-in containers. The first way is to configure them through configuration files, and the other way is to code 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 org.springframework.boot.autoconfigure.web.ServerProperties service attribute class. The following shows some configuration of tomcat
server: port: 8081 # tomcat set tomcat: accesslog: # Enable log access enabled: true # Log saving 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. Implement the configuration and optimization of built-in containers through code code
> There are two ways to display the built-in container optimization and customization using code. The first is to implement the built-in Servlet container customizer (org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer) and hand over the class to Spring container management. The other is to configure the EmbeddedServletContainerFactory interface implementation class in the Spring container. Here we mainly target the built-in Tomcat, that is, TomcatEmbeddedServletContainerFactory class
3.1. The first method is to implement the EmbeddedServletContainerCustomizer interface and hand it over to Spring container management
@Componentpublic class MyEmbeddedServletContainerCustomizer implements EmbeddedServletContainerCustomizer { @Override public void customize(ConfigurableEmbeddedServletContainer container) { //org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory //Instruction that the default is Tomcat container System.out.println(container.getClass()); TomcatEmbeddedServletContainerFactory factory = (TomcatEmbeddedServletContainerFactory) container; //Set port factory.setPort(8088); //Set Tomcat root directory factory.setBaseDirectory(new File("d:/tmp/tomcat")); //Set the access log storage directory factory.addContextValves(getLogAccessLogValue()); //Set Tomcat threads and connections factory.addConnectorCustomizers(new MyTomcatConnectorCustomizer()); //Initialize the servletContext object factory.addInitializers((servletContext) -> { System.out.println(" = = = = Get server information = = " + 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 processor 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
@SpringBootConfigurationpublic class WebServerConfiguration { @Bean public EmbeddedServletContainerFactory embeddedServletContainerFactory() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); //Set port factory.setPort(8089); //Set 404 error interface factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html")); //Set the factory.addInitializers((servletContext) -> { System.out.println(" = = = = Get server information = = " + servletContext.getServerInfo()); }); //Set the maximum number of connections and maximum number of threads factory.addConnectorCustomizers((connector) -> { Http11NioProtocol protocolHandler = (Http11NioProtocol) connector.getProtocolHandler(); protocolHandler.setMaxConnections(2000); protocolHandler.setMaxThreads(500); }); //Set the directory where the logging file is accessed 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
The above is a detailed explanation of the built-in Tomcat container examples of Spring Boot customization and optimization introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!