SpringBoot優化內嵌的Tomcat

SpringBoot測試版本java

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

 

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

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

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

如下有三種方法tomcat

一:springboot

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

 

二:app

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

 

 

三:spring-boot

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了。性能

相關文章
相關標籤/搜索