spring-boot - 配置 Spring WebFlux WebClient 以使用自定义线程池

标签 spring-boot spring-webflux project-reactor spring-webclient

是否可以将 WebClient 配置为使用 reactor-http-nio 线程池以外的自定义线程池(使用 Netty 时)?如果可能,我们能否以某种方式限制该自定义线程池仅在特定处理器内核上运行?

最佳答案

是的。你可以。

  • 创建一些你自己的线程池和EventLoopGroup(或者创建NioEventLoopGroup bean)。例如:
    {
     Intger THREADS = 10;
    
     BasicThreadFactory THREADFACTORY = new BasicThreadFactory.Builder()
            .namingPattern("HttpThread-%d")
            .daemon(true)
            .priority(Thread.MAX_PRIORITY)
            .build();
    
     EXECUTOR = new ThreadPoolExecutor(
            THREADS,
            THREADS,
            0L,
            TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(),
            THREADFACTORY,
            new ThreadPoolExecutor.AbortPolicy());
    
     NioEventLoopGroup RESOURCE= new NioEventLoopGroup(THREADS,EXECUTOR);
    }
    
  • 注册你自己的 ReactorResourceFactory。并基于自定义线程Executor提供自己的EventLoopGrooup
    @Bean
    public ReactorResourceFactory reactorResourceFactory(NioEventLoopGroup RESOURCE) {
        ReactorResourceFactory f= new ReactorResourceFactory();
        f.setLoopResources(new LoopResources() {
            @Override
             public EventLoopGroup onServer(boolean b) {
                 return RESOURCE;
                }
            });
        f.setUseGlobalResources(false);
        return f;
    }
    
  • 然后注册 ReactorClientHttpConnector。在下面的示例中,它使用自定义 SSL 上下文
    @Bean
    public ReactorClientHttpConnector reactorClientHttpConnector(ReactorResourceFactory r) throws SSLException {
        SslContext sslContext = SslContextBuilder
            .forClient()
            .trustManager(InsecureTrustManagerFactory.INSTANCE)
            .build();
        return new ReactorClientHttpConnector(r, m -> m.secure(t -> t.sslContext(sslContext)));
    }
    
  • 最后构建WebClient
    @Bean
    public WebClient webClient(ReactorClientHttpConnector r) {
        return WebClient.builder().clientConnector(r).build();
    }
    

  • 如果你想对 WebServer 使用相同的。对 ReactiveWebServerFactory 做同样的配置。
        @Bean
        public ReactiveWebServerFactory reactiveWebServerFactory(NioEventLoopGroup RESOURCE) {
            NettyReactiveWebServerFactory factory = new NettyReactiveWebServerFactory();
            factory.addServerCustomizers(hs->hs.tcpConfiguration(s->s.runOn(RESOURCE)));
            return factory;
        }
    

    进口:
        import io.netty.channel.EventLoopGroup;
        import io.netty.channel.nio.NioEventLoopGroup;
        import io.netty.handler.ssl.SslContext;
        import io.netty.handler.ssl.SslContextBuilder;
        import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.DependsOn;
        import org.springframework.http.client.reactive.ReactorClientHttpConnector;
        import org.springframework.http.client.reactive.ReactorResourceFactory;
        import org.springframework.stereotype.Component;
        import org.springframework.web.reactive.function.client.WebClient;
        import reactor.netty.resources.LoopResources;
        import org.apache.commons.lang3.concurrent.BasicThreadFactory;
        import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
        import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
        import java.util.concurrent.*;
    

    关于spring-boot - 配置 Spring WebFlux WebClient 以使用自定义线程池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56764801/

    相关文章:

    spring-webflux - 因为我的基本URI不固定,所以在Webflux中一次又一次地创建Webclient是否明智?

    spring-boot - 解决启用Kotlin的项目中的Jhipster依赖项时出错

    java - 使用 Reactor Mono 记录重试

    spring-boot - Gradle 选择了错误的依赖版本

    java - 如何复用单声道助焊剂值(value)?

    java - 了解 Spring 的 Web 响应式框架

    project-reactor - 使用 Hooks 和 Lift 将 Context 插入 ThreadLocal

    java - 项目 react 堆 : How to retry mono with different argument until some condition is met

    postgresql - 迁移到 Hibernate 5.2 后出现缺少表错误

    java - 如何初始化 firebase 托管在 google app-engine 上的 spring-boot 应用程序