SpringBoot優化內嵌的Tomcat ---設置MaxConnections

使用kill -9殺掉springboot應用後,立馬java -jar重啓,會報錯,須要等待一段時間才能啓動成功,
報錯的緣由是:/tmp/tomcat-docbase.4749794910434376321.9086] is not valid
如下是詳細錯誤信息:java

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

 

[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

 

 

 

SpringBoot測試版本web

<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>  

SpringBoot默認使用的是Tomcat做爲web serverspring

 

springboot中默承認以經過配置以下參數設置內嵌容器Tomcat的端口,最大線程數等等apache

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

可是,沒法設置最大鏈接數,而最大鏈接數直接影響到Tomcat的性能,如何調整這個最大鏈接數呢?tomcat

如下有三種方法springboot

一:app

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)  
    {  
        //設置端口  
        this.setPort(8081);  
        return super.getEmbeddedServletContainer(initializers);  
    }  
      
    protected void customizeConnector(Connector connector)  
    {  
        super.customizeConnector(connector);  
        Http11NioProtocol protocol = (Http11NioProtocol)connector.getProtocolHandler();  
        //設置最大鏈接數  
        protocol.setMaxConnections(2000);  
        //設置最大線程數  
        protocol.setMaxThreads(2000);  
        protocol.setConnectionTimeout(30000);  
    }  
}  

 

二:spring-boot

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);  
    }  
}  

 

 

三:性能

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();  
    }  
}  
  
class MyEmbeddedServletContainerCustomizer implements EmbeddedServletContainerCustomizer  
{  
    public void customize(ConfigurableEmbeddedServletContainer container)  
    {  
        TomcatEmbeddedServletContainerFactory tomcatFactory = (TomcatEmbeddedServletContainerFactory)container;  
        tomcatFactory.setPort(8081);  
        //下面的操做能夠參照上面的方法  
    }  
}  

 

最後,若是發現沒有生效,就把application.properties配置裏面關於Tomcat的一些配置(server.*)所有刪掉,就OK了。測試

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

相關文章
相關標籤/搜索