【发布时间】:2018-03-16 00:31:20
【问题描述】:
默认情况下,spring web Flux 使用 netty,它是单线程事件循环。如何配置spring boot,以便为每个核心创建一个线程。
谢谢,
洛克希
【问题讨论】:
标签: spring-boot spring-webflux reactor-netty
默认情况下,spring web Flux 使用 netty,它是单线程事件循环。如何配置spring boot,以便为每个核心创建一个线程。
谢谢,
洛克希
【问题讨论】:
标签: spring-boot spring-webflux reactor-netty
如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)));
}
}
【讨论】:
你可以改变你的依赖:
<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
【讨论】: