SpringBoot optimized embedded Tomcat --- set MaxConnections

After a kill -9 to kill springboot application, immediately java -jar restart, the error will need to wait a while to start successfully,
the reason being given is: /tmp/tomcat-docbase.4749794910434376321.9086] is not valid
following is a detailed error message:

Copy the code
Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.webresources.StandardRoot@53001498]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:158) ~[tomcat-embed-core-8.5.4.jar!/:8.5.4]
at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4831) ~[tomcat-embed-core-8.5.4.jar!/:8.5.4]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4963) ~[tomcat-embed-core-8.5.4.jar!/:8.5.4]
... 7 more
Caused by: java.lang.IllegalArgumentException: The main resource set specified [/tmp/tomcat-docbase.4749794910434376321.9086] is not valid at org.apache.catalina.webresources.StandardRoot.createMainResourceSet(StandardRoot.java:725) ~[tomcat-embed-core-8.5.4.jar!/:8.5.4] at org.apache.catalina.webresources.StandardRoot.startInternal(StandardRoot.java:686) ~[tomcat-embed-core-8.5.4.jar!/:8.5.4] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:152) ~[tomcat-embed-core-8.5.4.jar!/:8.5.4] at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4831) ~[tomcat-embed-core-8.5.4.jar!/:8.5.4] at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4963) ~[tomcat-embed-core-8.5.4.jar!/:8.5.4] ... 7 more mealtime-report-[15:48:37:716] [ERROR] - org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:181) - A child container failed during start
Copy the code

 

Copy the code
[root@localhost tmp]# ls -dlt tomcat*
drwxr-xr-x 3 root root 4096 May 15 14:19 tomcat.2482651926943103152.4444 drwxr-xr-x 2 root root 4096 May 15 14:19 tomcat-docbase.1606353072223048027.4444 drwxr-xr-x 3 root root 4096 May 15 14:19 tomcat.6427937730432992105.4004 drwxr-xr-x 2 root root 4096 May 15 14:19 tomcat-docbase.9014354490424152602.4004 drwxr-xr-x 3 root root 4096 May 14 18:21 tomcat.365692316696650885.2000 drwxr-xr-x 2 root root 4096 May 14 18:21 tomcat-docbase.8871210702020412482.2000 drwxr-xr-x 3 root root 4096 May 14 18:21 tomcat.9194454391130879725.2222 drwxr-xr-x 2 root root 4096 May 14 18:21 tomcat-docbase.6449009990528406178.2222 drwxr-xr-x 3 root root 4096 May 11 16:31 tomcat.7533402776366547594.3333 drwxr-xr-x 2 root root 4096 May 11 16:31 tomcat-docbase.8347169054078560101.3333 drwxr-xr-x 3 root root 4096 May 11 16:31 tomcat.5755321441664547955.3004 drwxr-xr-x 2 root root 4096 May 11 16:31 tomcat-docbase.4368794759444711233.3004 drwxr-xr-x 3 root root 4096 May 11 16:31 tomcat.7487101127514666304.4444 drwxr-xr-x 2 root root 4096 May 11 16:31 tomcat-docbase.3505389582011000978.4444 drwxr-xr-x 3 root root 4096 May 11 16:30 tomcat.6483749742824168608.4004 drwxr-xr-x 2 root root 4096 May 11 16:30 tomcat-docbase.2936863713994400124.4004 drwxr-xr-x 3 root root 4096 May 11 16:30 tomcat.8814016287852028090.2000 drwxr-xr-x 2 root root 4096 May 11 16:30 tomcat-docbase.5759661693624441557.2000 drwxr-xr-x 3 root root 4096 May 11 16:30 tomcat.3084296173620137759.2222 drwxr-xr-x 2 root root 4096 May 11 16:30 tomcat-docbase.2913734529869658765.2222 drwxr-xr-x 3 root root 4096 May 11 16:30 tomcat.1261539854295462589.3333 drwxr-xr-x 2 root root 4096 May 11 16:30 tomcat-docbase.4327066414867055940.3333 drwxr-xr-x 3 root root 4096 May 11 16:30 tomcat.1652439996406851728.3004 drwxr-xr-x 2 root root 4096 May 11 16:30 tomcat-docbase.5233226104416503892.3004 drwxr-xr-x 3 root root 4096 May 11 11:38 tomcat.8505864110333115533.3333 drwxr-xr-x 2 root root 4096 May 11 11:38 tomcat-docbase.5036544091928181133.3333 drwxr-xr-x 3 root root 4096 May 11 11:37 tomcat.494020510980868278.3004 drwxr-xr-x 2 root root 4096 May 11 11:37 tomcat-docbase.468265602651303178.3004 drwxr-xr-x 3 root root 4096 May 11 11:36 tomcat.8093369488059063930.3004 drwxr-xr-x 2 root root 4096 May 11 11:36 tomcat-docbase.7679645853200594654.3004 drwxr-xr-x 3 root root 4096 May 11 11:31 tomcat.5040740781547956399.4444 drwxr-xr-x 2 root root 4096 May 11 11:31 tomcat-docbase.8256790475295084303.4444 drwxr-xr-x 3 root root 4096 May 11 11:31 tomcat.7344112750169248130.4004 drwxr-xr-x 2 root root 4096 May 11 11:31 tomcat-docbase.2016044219412775812.4004 drwxr-xr-x 3 root root 4096 May 11 11:30 tomcat.3866209351076068036.4444
Copy the code

 

 

 

SpringBoot test version

Copy the code
<parent>  
    <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> 
Copy the code

SpringBoot default Tomcat is used as a web server

 

springboot default Tomcat container may be provided by embedded port, set the parameters, the maximum number of threads, etc.

server.port=8081
server.tomcat.max-threads=1000

However, you can not set the maximum number of connections, and the maximum number of connections directly affects the performance of Tomcat, how to adjust the maximum number of connections it?

Here are three ways

One:

Copy the code
import org.apache.catalina.connector.Connector;  
import org.apache.coyote.http11.Http11NioProtocol;  
import org.springframework.boot.context.embedded.EmbeddedServletContainer; import org.springframework.boot.context.embedded.ServletContextInitializer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.stereotype.Component; @Component() public class MyEmbeddedServletContainerFactory extends TomcatEmbeddedServletContainerFactory { public EmbeddedServletContainer getEmbeddedServletContainer(ServletContextInitializer... initializers) { //Set the port this.setPort (8081 ); return Super .getEmbeddedServletContainer (from the initializers);} protected void customizeConnector (Connector Connector) { Super .customizeConnector (Connector); Http11NioProtocol = Protocol (Http11NioProtocol) connector.getProtocolHandler (); // set the maximum connection number protocol.setMaxConnections (2000 ); // set the maximum number of threads protocol.setMaxThreads (2000 ); protocol.setConnectionTimeout (30000 );}}
Copy the code

 

two:

Copy the code
import org.apache.catalina.connector.Connector;  
import org.apache.coyote.http11.Http11NioProtocol;  
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WebServerConfiguration { @Bean public EmbeddedServletContainerFactory createEmbeddedServletContainerFactory() { TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory(); tomcatFactory.setPort(8081); tomcatFactory.addConnectorCustomizers(new MyTomcatConnectorCustomizer()); return tomcatFactory; } } class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer { public void customize(Connector connector) { Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler(); //设置最大连接数 protocol.setMaxConnections(2000); //设置最大线程数 protocol.setMaxThreads(2000); protocol.setConnectionTimeout(30000); } } 
Copy the code

 

 

three:

Copy the code
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;  
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;  
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WebServerConfiguration { @Bean public EmbeddedServletContainerCustomizer createEmbeddedServletContainerCustomizer() { return new MyEmbeddedServletContainerCustomizer(); } } MyEmbeddedServletContainerCustomizer class the implements EmbeddedServletContainerCustomizer { public void Customize (ConfigurableEmbeddedServletContainer Container) = {TomcatEmbeddedServletContainerFactory tomcatFactory (TomcatEmbeddedServletContainerFactory) Container; tomcatFactory.setPort (8081 ); // The following operation may refer to the above method }}
Copy the code

 

Finally, if found not in force, application.properties put inside some configuration on the configuration of Tomcat (server. *) All deleted, OK.

http://blog.csdn.net/mn960mn/article/details/51306140

Guess you like

Origin www.cnblogs.com/leeego-123/p/11362328.html