在Spring Boot 2.1中,如何配置Netty以使用自定义EventLoopGroup?

时间:2019-03-21 17:44:46

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

我正在尝试将Netty配置为使用自定义EventLoopGroup和自定义端口。

根据文档,我看到我可以使用WebServerFactoryCustomizer

@Configuration
public class NettyCustomizer 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 = new NioEventLoopGroup(50);
            return httpServer.tcpConfiguration(tcpServer ->
                    tcpServer.bootstrap(serverBootstrap
                            -> serverBootstrap.group(eventLoopGroup))).port(9000);
        }
    }

但是当我应用此bean时,我在控制台中看到以下异常。

2019-03-21 19:38:18.872 ERROR 30981 --- [           main] o.s.boot.SpringApplication               : Application run failed

    org.springframework.boot.web.server.WebServerException: Unable to start Netty
        at org.springframework.boot.web.embedded.netty.NettyWebServer.start(NettyWebServer.java:75) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
        at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext$ServerManager.start(ReactiveWebServerApplicationContext.java:232) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
        at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.startReactiveWebServer(ReactiveWebServerApplicationContext.java:130) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
        at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.finishRefresh(ReactiveWebServerApplicationContext.java:122) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
        at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:67) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
        at com.refinitiv.keyvalue.train.Main.main(Main.java:10) [classes/:na]
    Caused by: java.lang.IllegalStateException: group set already
        at io.netty.bootstrap.AbstractBootstrap.group(AbstractBootstrap.java:86) ~[netty-transport-4.1.29.Final.jar:4.1.29.Final]

我在Stackoverflow中看到了这个post,但是我不知道该如何使它工作以及哪种方法是正确的。

0 个答案:

没有答案