【优化】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<TomcatProtocolHandlerCustomizer<?>> tomcatProtocolHandlerCustomizers = factory.getTomcatProtocolHandlerCustomizers();
        tomcatProtocolHandlerCustomizers.add(protocolHandlerCustomizer());
        factory.setTomcatProtocolHandlerCustomizers(tomcatProtocolHandlerCustomizers);

        return factory;
    }

    public static TomcatProtocolHandlerCustomizer<?> protocolHandlerCustomizer(){
        return protocolHandler -> {
            protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
        };
    }
    
}
相关推荐
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭1 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
超爱吃士力架1 小时前
邀请逻辑
java·linux·后端
AskHarries3 小时前
Spring Cloud OpenFeign快速入门demo
spring boot·后端
isolusion4 小时前
Springboot的创建方式
java·spring boot·后端
Yvemil74 小时前
《开启微服务之旅:Spring Boot Web开发举例》(一)
前端·spring boot·微服务
zjw_rp4 小时前
Spring-AOP
java·后端·spring·spring-aop
TodoCoder5 小时前
【编程思想】CopyOnWrite是如何解决高并发场景中的读写瓶颈?
java·后端·面试
凌虚6 小时前
Kubernetes APF(API 优先级和公平调度)简介
后端·程序员·kubernetes
星河梦瑾6 小时前
SpringBoot相关漏洞学习资料
java·经验分享·spring boot·安全
机器之心6 小时前
图学习新突破:一个统一框架连接空域和频域
人工智能·后端