当前位置: 首页 > 知识库问答 >
问题:

如何使用一个引导程序连接Netty中的多个服务器

夹谷茂
2023-03-14

我不知道如果我每次连接到远程服务器时都创建(新)一个引导程序,是否存在性能问题。所以我想使用一个单独的引导实例连接到多个服务器。我的代码如下:

Bootstrap b = new Bootstrap();
b.group(new NioEventLoopGroup()).handler(new TestHandler()).channel(NioSocketChannel.class)
        .option(ChannelOption.AUTO_READ, false);
ChannelFutureListener listener = new ChannelFutureListener() {
    @Override
    public void operationComplete(ChannelFuture future) throws Exception {
        if (future.isSuccess()) {
            // connection complete start to read first data
            System.out.println("connection established + channel: " + future.channel());
        } else {
            // Close the connection if the connection attempt has failed.
            System.out.println("connection failed");
        }
    }
};

String[] urls = { "www.google.com", "www.stackoverflow.com", "www.yahoo.com" };
for (String s : urls) {
    b = b.clone();
    b.remoteAddress(s, 80);
    b.connect().addListener(listener);
}
System.in.read();

不幸的是,它与:

connection established + channel: [id: 0x5df86eec, /192.168.126.136:60414 => www.google.com/173.194.127.209:80]
Exception in thread "main" java.lang.IllegalStateException: channel not registered to an event loop
    at io.netty.channel.AbstractChannel.eventLoop(AbstractChannel.java:107)
    at io.netty.channel.nio.AbstractNioChannel.eventLoop(AbstractNioChannel.java:102)
    at io.netty.channel.nio.AbstractNioChannel.eventLoop(AbstractNioChannel.java:41)
    at io.netty.channel.CompleteChannelFuture.executor(CompleteChannelFuture.java:48)
    at io.netty.util.concurrent.CompleteFuture.addListener(CompleteFuture.java:49)
    at io.netty.channel.CompleteChannelFuture.addListener(CompleteChannelFuture.java:56)
    at Test3.main(Test3.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

至少,我应该使用相同的NioEventLoopGroup,对吗?

共有1个答案

袁翔
2023-03-14

好吧,是我的错,我忘了添加ChannelInitializer。将其更改为下面的代码:

b.group(new NioEventLoopGroup()).handler(new ChannelInitializer<NioSocketChannel>() {

            @Override
            protected void initChannel(NioSocketChannel ch) throws Exception {
                ch.pipeline().addLast(new TestHandler());
            }
        })
 类似资料:
  • netty文档讨论TCP客户端。 但对UDP来说,这句话似乎无关紧要?!我找到了仅使用引导程序的UDP服务器的示例,如:1,2,3 我对此感到很困惑,所以我的两个问题是: null

  • Netty服务器,Fedora。我只是无法从远程主机连接到服务器,并且通过util没有显示监听套接字。但是我可以在同一台机器上建立运行客户端和服务器的连接。就像这样: 我已尝试仅使用端口、localhost IP、0.0.0.0 IP和网络IP初始化

  • 我知道在Netty4中,一旦建立了一个通道,该通道的所有事件处理都在分配给特定EventLoop的同一个线程上完成。此绑定在通道创建时完成。 我不明白的部分是,实际需要多少线程来接受新连接、创建新通道和进行绑定?

  • 我使用的是Netty 3.9.5,我有一个简单的客户机-服务器设置,我从http://en.wikipedia.org/wiki/Netty_(软件)#Netty\u TCP\u示例。我扩展了这个示例,将Java search plan对象从客户端发送到服务器。在这个网站上跟踪用户的帮助下,我已经能够让这个程序按预期运行。 现在,我想让我的读卡器/服务器程序同时接受多个客户端。我想我会使用下面列出

  • 这是否意味着我的整个应用程序只能连接到单个Kafka集群,或者KafkaStreams的每个实例只能连接到单个集群? 我可以创建多个连接到不同集群的具有不同属性的KafkaStreams实例吗?

  • 我希望netty服务器A在启动时连接netty服务器B,听起来像代理,所以我尝试了netty代理示例,但它只是在中启动netty客户端,只有一个新的连接处于活动状态,客户端才会被创建。当服务器A启动时,我需要通知服务器B做一些“注册”的事情,我该怎么办?