我正在寻找一个服务器示例,它将端口80上的http处理程序和同一jar中另一个端口上的protobuf处理程序组合在一起.谢谢!
Dmi*_*kiy 10
对于我的观点,创建不同的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()
Run Code Online (Sandbox Code Playgroud)
同
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5994 次 |
| 最近记录: |