jtlgb

导航

统计
 

SpringBoot测试版本

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

 

springboot中默认可以通过配置如下参数设置内嵌容器Tomcat的端口,最大线程数等等

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

但是,无法设置最大连接数,而最大连接数直接影响到Tomcat的性能,如何调整这个最大连接数呢?

以下有三种方法

一:

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

 

二:

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

posted on 2019-06-06 16:29 jtlgb 阅读(...) 评论(...) 编辑 收藏