Netty 4.0 on multiple ports with multiple protocols?

前端 未结 2 1407
长情又很酷
长情又很酷 2021-02-06 13:48

I\'m looking for a server example that would combine a http handler on port 80 and a protobuf handler on another port in the same jar. Thanks!

相关标签:
2条回答
  • 2021-02-06 14:28

    For my perspective, create different ServerBootstraps are not fully right way, because it will lead to create unused entities, handlers, double initialization, possible inconsistence between them, EventLoopGroups sharing or cloning, etc.

    Good alternative is just to create multiple channels for all required ports in one Bootstrap server. If take "Writing a Discard Server" example from Netty 4.x "Getting Started", we should replace

        // 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()
    

    With

        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();
        }
    
    0 讨论(0)
  • 2021-02-06 14:31

    I don't know what exactly you are looking for. Its just about creating two different ServerBootstrap instances, configure them and call bind(..) thats it.

    0 讨论(0)
提交回复
热议问题