Java Netty:使用单个服务器引导监听多个地址/端口

Java Netty:使用单个服务器引导监听多个地址/端口,java,netty,Java,Netty,我目前不知道如何在多个地址/端口上使用netty进行监听。我想构建一个小型HTTP服务器,它服务于一个特殊的应用程序。我需要在几个地址(如IPv4和IPv6)和端口(443/80)上运行它 对于每个侦听器,我希望使用相同的处理程序。我当前的代码如下所示: public void run() { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEvent

我目前不知道如何在多个地址/端口上使用netty进行监听。我想构建一个小型HTTP服务器,它服务于一个特殊的应用程序。我需要在几个地址(如IPv4和IPv6)和端口(443/80)上运行它

对于每个侦听器,我希望使用相同的处理程序。我当前的代码如下所示:

public void run() {

    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try {

        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.option(ChannelOption.SO_BACKLOG, 1024);

        bootstrap.group(bossGroup, workerGroup)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .channel(NioServerSocketChannel.class)
            .childHandler(new ApplicationServerInitializer());

        Channel channel = bootstrap.bind(this.socketAddress).sync().channel();

        logger.info("Started HTTP Server on {}:{}", this.socketAddress.getHostName(), this.socketAddress.getPort());

        channel.closeFuture().sync();

    } catch(Throwable throwable) {
        logger.error("An error occurred while starting the HTTP- Server", throwable);
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}
只需多次调用
bind(…)

List<ChannelFuture> futures = new ArrayList<>();
futures.add(bootstrap.bind(this.socketAddress(IPV4, 80)));
futures.add(bootstrap.bind(this.socketAddress(IPV4, 443)));
futures.add(bootstrap.bind(this.socketAddress(IPV6, 80)));
futures.add(bootstrap.bind(this.socketAddress(IPV6, 443)));

for (ChannelFuture f: futures) {
    f.sync();
}
List futures=new ArrayList();
futures.add(bootstrap.bind(this.socketAddress(IPV4,80));
futures.add(bootstrap.bind(this.socketAddress(IPV4,443));
futures.add(bootstrap.bind(this.socketAddress(IPV6,80));
futures.add(bootstrap.bind(this.socketAddress(IPV6,443));
for(未来f:未来){
f、 sync();
}

感谢您的回答!我使用的是Netty 5.0.0.Alpha2,boostrap.bind(..)返回一个ChannelFuture,所以我不能再调用那里的bind(..)。因此,如果我在循环中执行此操作,我应该如何使用
channel.closeFuture().sync()关闭所有创建的通道
虽然这个函数是阻塞函数?@user3641396就我所见,循环只是为了防止程序退出。它在列表中的第一个位置开始阻塞,但是如果关闭ServerBootstrap,通道开始关闭。该循环确保所有这些都是闭合的。