【问题标题】:How to configure netty in spring boot 2如何在spring boot 2中配置netty
【发布时间】:2018-03-16 00:31:20
【问题描述】:

默认情况下,spring web Flux 使用 netty,它是单线程事件循环。如何配置spring boot,以便为每个核心创建一个线程。

谢谢,

洛克希

【问题讨论】:

    标签: spring-boot spring-webflux reactor-netty


    【解决方案1】:

    Spring Boot reference documentation 中所述,您可以使用NettyServerCustomizer 自定义Reactor Netty Web 服务器。

    以下是 Spring Boot 2.1 的示例:

    @Component
    public class MyNettyWebServerCustomizer
            implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
    
        @Override
        public void customize(NettyReactiveWebServerFactory factory) {
            factory.addServerCustomizers(new EventLoopNettyCustomizer());
        }
    }
    
    class EventLoopNettyCustomizer implements NettyServerCustomizer {
    
        @Override
        public HttpServer apply(HttpServer httpServer) {
            EventLoopGroup eventLoopGroup = //...;
            return httpServer.tcpConfiguration(tcpServer ->
                    tcpServer.bootstrap(serverBootstrap
                            -> serverBootstrap.group(eventLoopGroup)));
        }
    }
    

    【讨论】:

    • 当然。我先在这里发帖。由于我没有得到任何回复,我提出了一个问题。
    【解决方案2】:

    你可以改变你的依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <!-- Exclude the Tomcat dependency -->
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Use Jetty instead -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    

    https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-web-servers.html

    【讨论】:

    • 问题不在于更换服务器。如何在每个处理器上运行单线程网络连接
    猜你喜欢
    • 2018-01-18
    • 2019-04-04
    • 2018-05-11
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    相关资源
    最近更新 更多