gpt4 book ai didi

具有多个协议(protocol)的多个端口上的 Netty 4.0?

转载 作者:行者123 更新时间:2023-12-04 16:29:35 28 4
gpt4 key购买 nike

我正在寻找一个服务器示例,它将端口 80 上的 http 处理程序和同一个 jar 中另一个端口上的 protobuf 处理程序结合起来。
谢谢!

最佳答案

在我看来,创建不同的 ServerBootstrap 并不完全正确,因为它会导致创建未使用的实体、处理程序、双重初始化、它们之间可能的不一致、EventLoopGroups 共享或克隆等。

一个好的选择是在一个 Bootstrap 服务器中为所有需要的端口创建多个 channel 。
如果以 Netty 4.x "Getting Started" 中的“Writing a Discard Server”为例, 我们应该替换

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

关于具有多个协议(protocol)的多个端口上的 Netty 4.0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12243932/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com