在多个协议的多个端口上的Netty 4.0?
我正在寻找一个服务器示例,它将端口80上的http处理程序与同一个jar中另一个端口上的protobuf处理程序结合起来。 谢谢!
我不知道你到底在找什么。 它只是创建两个不同的ServerBootstrap实例,配置它们并调用bind(..)就是这样。
对我来说,创建不同的ServerBootstraps不是完全正确的方式,因为它会导致创建未使用的实体,处理程序,双重初始化,它们之间可能存在不一致,EventLoopGroups共享或克隆等。
好的选择是为一个Bootstrap服务器中的所有必需端口创建多个通道。 如果从Netty 4.x“入门”中选择“编写丢弃服务器”示例,我们应该更换
    // Bind and start to accept incoming connections.
    ChannelFuture f = b.bind(port).sync(); // (7)
    // Wait until the server socket is closed.
    // In this example, this does not happen, but you can do that to gracefully
    // shut down your server.
    f.channel().closeFuture().sync()
同
    List<Integer> ports = Arrays.asList(8080, 8081);
    Collection<Channel> channels = new ArrayList<>(ports.size());
    for (int port : ports) {
        Channel serverChannel = bootstrap.bind(port).sync().channel();
        channels.add(serverChannel);
    }
    for (Channel ch : channels) {
        ch.closeFuture().sync();
    }
