【优化】Springboot 修改 tomcat连接池

【优化】Springboot 修改 tomcat连接池

        factory.setTomcatProtocolHandlerCustomizers(tomcatProtocolHandlerCustomizers);
 

可以更换为 虚拟线程连接池

package org.config.init;

import org.apache.catalina.Context;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.tomcat.util.scan.StandardJarScanner;
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executors;

/**
 * @Description: TomcatFactoryConfig
 */
@Configuration
public class TomcatFactoryConfig {
    /**
     * tomcat-embed-jasper引用后提示jar找不到的问题
     */
    @Bean
    public TomcatServletWebServerFactory tomcatFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                ((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
            }
        };
        factory.addConnectorCustomizers(connector -> {
//            connector.setProperty("relaxedPathChars", "[]{}");
//            connector.setProperty("relaxedQueryChars", "[]{}");

            connector.setProperty("relaxedPathChars", "\"#<>[\\]^`{|}/");
            connector.setProperty("relaxedQueryChars", "\"#<>[\\]^`{|}/");

        });


        //支持APR  AIO
        //factory.setProtocol("org.apache.coyote.http11.Http11AprProtocol");
        //factory.addContextLifecycleListeners(new AprLifecycleListener());
        //支持APR  AIO

        Collection> tomcatProtocolHandlerCustomizers = factory.getTomcatProtocolHandlerCustomizers();
        tomcatProtocolHandlerCustomizers.add(protocolHandlerCustomizer());
        factory.setTomcatProtocolHandlerCustomizers(tomcatProtocolHandlerCustomizers);

        return factory;
    }

    public static TomcatProtocolHandlerCustomizer protocolHandlerCustomizer(){
        return protocolHandler -> {
            protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
        };
    }
    
}

你可能感兴趣的:(技巧心得,spring,boot,tomcat,后端)