springboot 2.X后内置tomcat自定义配置

目录

    使用Tomcat启用多个连接器

    配置springboot中Web服务器-自带默认tomcat配置


由官网可知,springboot在2.0以后,对于tomcat提供自定义接口发生了变化;如官网提供配置多个tomcat链接器:

    使用Tomcat启用多个连接器

 

可以添加org.apache.catalina.connector.Connector到 TomcatServletWebServerFactory,这可以允许多个连接器,包括HTTP和HTTPS连接器,显示在下面的例子:

@Bean
 public ServletWebServerFactory servletContainer(){
	TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(); 
	tomcat.addAdditionalTomcatConnectors(createSslConnector()); 返回 tomcat; 
}私有连接器createSslConnector(){
	连接器连接器=新连接器( “org.apache.coyote.http11.Http11NioProtocol”); 
	Http11NioProtocol protocol =(Http11NioProtocol)connector.getProtocolHandler(); 尝试 {
		File keystore = new ClassPathResource( “keystore”)。getFile(); 
		文件truststore = new ClassPathResource(“keystore”)。getFile(); 
		connector.setScheme(“https”); 
		connector.setSecure(真); 
		connector.setPort(8443); 
		protocol.setSSLEnabled(真); 
		protocol.setKeystoreFile(keystore.getAbsolutePath()); 
		protocol.setKeystorePass(“changeit”); 
		protocol.setTruststoreFile(truststore.getAbsolutePath()); 
		protocol.setTruststorePass(“changeit”); 
		protocol.setKeyAlias(“apitester”);
		返回连接器; 
	} catch(IOException ex){
		 throw new
	 IllegalStateException(“无法访问密钥库:[” + “keystore” 
				+ “]或truststore:[” + “keystore” + “]”,ex); 
	} 
}

1、所以用全新的ServletWebServerFactory类来构造Tomcat的配置如下:

    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addConnectorCustomizers(new GwsTomcatConnectionCustomizer());
        return tomcat;
    }

2、全部实现Tomcat配置案例如下:

package com.gws.configuration;

import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;


/**
 * 使用tomcat配置
 */
@Configuration
public class TomcatConfig {

    @Value("${spring.server.port}")
    private String port;
    @Value("${spring.server.acceptorThreadCount}")
    private String acceptorThreadCount;
    @Value("${spring.server.minSpareThreads}")
    private String minSpareThreads;
    @Value("${spring.server.maxSpareThreads}")
    private String maxSpareThreads;
    @Value("${spring.server.maxThreads}")
    private String maxThreads;
    @Value("${spring.server.maxConnections}")
    private String maxConnections;
    @Value("${spring.server.protocol}")
    private String protocol;
    @Value("${spring.server.redirectPort}")
    private String redirectPort;
    @Value("${spring.server.compression}")
    private String compression;
    @Value("${spring.server.connectionTimeout}")
    private String connectionTimeout;

    @Value("${spring.server.MaxFileSize}")
    private String MaxFileSize;
    @Value("${spring.server.MaxRequestSize}")
    private String MaxRequestSize;

    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addConnectorCustomizers(new GwsTomcatConnectionCustomizer());
        return tomcat;
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //  单个数据大小
        factory.setMaxFileSize(MaxFileSize); // KB,MB
        /// 总上传数据大小
        factory.setMaxRequestSize(MaxRequestSize);
        return factory.createMultipartConfig();
    }

    /**
     *
     * 默认http连接
     *
     *
     */
    public class GwsTomcatConnectionCustomizer implements TomcatConnectorCustomizer {

        public GwsTomcatConnectionCustomizer() {
        }

        @Override
        public void customize(Connector connector) {
            connector.setPort(Integer.valueOf(port));
            connector.setAttribute("connectionTimeout", connectionTimeout);
            connector.setAttribute("acceptorThreadCount", acceptorThreadCount);
            connector.setAttribute("minSpareThreads", minSpareThreads);
            connector.setAttribute("maxSpareThreads", maxSpareThreads);
            connector.setAttribute("maxThreads", maxThreads);
            connector.setAttribute("maxConnections", maxConnections);
            connector.setAttribute("protocol", protocol);
            connector.setAttribute("redirectPort", "redirectPort");
            connector.setAttribute("compression", "compression");
        }
    }
}

9、相应的application.properties配置文件

#嵌入tomcat配置
spring.server.port=8095
#和CPU数
spring.server.acceptorThreadCount=4
spring.server.minSpareThreads=50
spring.server.maxSpareThreads=50
spring.server.maxThreads=1000
spring.server.maxConnections=10000
#10秒超时
spring.server.connectionTimeout=10000
spring.server.protocol=org.apache.coyote.http11.Http11Nio2Protocol
spring.server.redirectPort=443
spring.server.compression=on
#文件请求大小
spring.server.MaxFileSize=300MB
spring.server.MaxRequestSize=500MB

    配置springboot中Web服务器-自带默认tomcat配置

由官网可知,springboot提供了自定义工厂模式。这里配置的将会覆盖springboot提供的配置

官网解释如下:

通常,您应首先考虑使用众多可用配置键中的一个,并通过在您的application.properties( application.yml或环境等)中添加新条目来自定义您的Web服务器,请参阅“ 第77.8节”,“发现外部属性的内置选项” “) 。该server.* 命名空间是非常有用的在这里,它包括命名空间一样server.tomcat.*, server.jetty.*和其他人,了解特定服务器的功能。请参阅附录A,常见应用程序属性列表。

前面的部分介绍了许多常见用例,例如压缩,SSL或HTTP / 2。但是,如果您的用例不存在配置密钥,则应该查看 WebServerFactoryCustomizer。您可以声明这样的组件并获得与您选择的服务器工厂相关的访问权限:您应该为所选服务器(Tomcat,Jetty,Reactor Netty,Undertow)和所选Web堆栈(Servlet或Reactive)选择变体。

以下示例适用于具有spring-boot-starter-web(Servlet堆栈)的Tomcat :

@Component
公共 类 MyTomcatWebServerCustomizer
		实现 WebServerFactoryCustomizer  {

	@Override
	 public  void customize(TomcatServletWebServerFactory factory){
		 //在这里自定义工厂
	}
}

另外Spring Boot提供:

服务器 Servlet堆栈 反应堆栈

Tomcat的

TomcatServletWebServerFactory

TomcatReactiveWebServerFactory

码头

JettyServletWebServerFactory

JettyReactiveWebServerFactory

暗潮

UndertowServletWebServerFactory

UndertowReactiveWebServerFactory

反应堆

N / A

NettyReactiveWebServerFactory

一旦您有权访问a WebServerFactory,您通常可以向其添加定制器以配置特定部件,例如连接器,服务器资源或服务器本身 - 所有这些都使用特定于服务器的API。

作为最后的手段,您还可以声明自己的WebServerFactory组件,它将覆盖Spring Boot提供的组件。在这种情况下,您不能再依赖server命名空间中的配置属性。

具体实现案例如下:

@Component
public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer{

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
                connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
                connector.setScheme("http");
                Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
                // 设置最大缓冲区
                protocol.setMaxHttpHeaderSize(1024000);
            }
        });
    }

参考:spring-boot定制和优化内嵌的Tomcat

你可能感兴趣的:(springboot,spring,源码分析)