默认情况下,spring web Flux 使用 netty,它是单线程事件循环。如何配置 Spring Boot 以便为每个核心创建一个线程。
谢谢,
洛克什
默认情况下,spring web Flux 使用 netty,它是单线程事件循环。如何配置 Spring Boot 以便为每个核心创建一个线程。
谢谢,
洛克什
如Spring Boot 参考文档中所述,您可以使用NettyServerCustomizer
.
这是 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