Netty 4.0 on multiple ports with multiple protocols?
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!
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.
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();
}
链接地址: http://www.djcxy.com/p/64772.html
上一篇: 带Netty 4的多端口服务器?