How to configure netty in spring boot 2

Lokesha S picture Lokesha S · Oct 4, 2017 · Viewed 13.6k times · Source

By default spring web flux uses netty which is single threaded event loop. How to configure spring boot so that a thread will be created for each core.

Thanks,

Lokesh

Answer

Brian Clozel picture Brian Clozel · Oct 6, 2017

As described in the Spring Boot reference documentation, you can customize the Reactor Netty web server with a NettyServerCustomizer.

Here's an example with 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)));
    }
}