Introduction

In all the netty knowledge we mentioned before, netty seems to be divided into two parts: client and server. The server listens for the connection and processes the messages in the connection. The client establishes a request connection to the server so that messages can be sent.

But all this has to be terminated in the UDT protocol, because UDT provides Rendezvous, an equal connection type, and the relationship between nodes is peer-to-peer.

There has never been a savior, no immortals or emperors, only good brothers who are also nodes.

Setting up a server that supports Rendezvous

Because it is a peer-to-peer relationship, there is no need to use ServerBootstrap here, it is enough to use ordinary Bootstrap.

The group is still needed. NioEventLoopGroup is used here. NioEventLoopGroup needs to provide SelectorProvider. UDT provides two providers, NioUdtProvider.BYTE_PROVIDER and NioUdtProvider.MESSAGE_PROVIDER, which represent two formats of stream and message:

final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1,
                connectFactory, NioUdtProvider.BYTE_PROVIDER);

final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1,
                connectFactory, NioUdtProvider.MESSAGE_PROVIDER);

The next step is to create Bootstrap, bind group and set channelFactory.

Of course, there are two kinds of channelFactory here, namely NiNioUdtProvider.BYTE_RENDEZVOUS and NioUdtProvider.BYTE_RENDEZVOUS.

Then there are two ways to create it, the first is byte stream:

 final Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(connectGroup)
                    .channelFactory(NioUdtProvider.BYTE_RENDEZVOUS)
                    .handler(new ChannelInitializer<UdtChannel>() {
                        @Override
                        protected void initChannel(UdtChannel ch) throws Exception {
                            ch.pipeline().addLast(
                                    new LoggingHandler(LogLevel.INFO),
                                    new UDTByteHandler(messageSize));

The second is the message:

final Bootstrap boot = new Bootstrap();
            boot.group(connectGroup)
                    .channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS)
                    .handler(new ChannelInitializer<UdtChannel>() {
                        @Override
                        public void initChannel(final UdtChannel ch)
                                throws Exception {
                            ch.pipeline().addLast(
                                    new LoggingHandler(LogLevel.INFO),
                                    new UDTMsgHandler(messageSize));
                        }
                    });

So far, two Rendezvous servers supporting different UDT types have been established.

The next step is to process the message.

handle different messages

With a server that supports both byte and message formats, the next step is how to process the corresponding messages.

For UDT in byte format, the message transmitted in the channel is ByteBuf. We only need to construct the ByteBuf message and transmit it in the channel:

private final ByteBuf message
message = Unpooled.buffer(messageSize);
message.writeBytes("www.flydean.com".getBytes(StandardCharsets.UTF_8));
ctx.writeAndFlush(message);

Corresponding to the UDT in message format, netty provides a special class UdtMessage to encapsulate it. UdtMessage inherits the value DefaultByteBufHolder, which is the encapsulation of ByteBuf.

We can create a UdtMessage and send it like this:

private final UdtMessage message;
final ByteBuf byteBuf = Unpooled.buffer(messageSize);
byteBuf.writeBytes("www.flydean.com".getBytes(StandardCharsets.UTF_8));
message = new UdtMessage(byteBuf);

ctx.writeAndFlush(message);

interaction between nodes

Above, we have established two nodes respectively. These two nodes are peer-to-peer, so how to connect these two nodes?

We call Bootstrap's connect method as follows:

final ChannelFuture f = boot.connect(peer, self).sync();
            f.channel().closeFuture().sync();

The connect here passes in two SocketAddress parameters, the first parameter is remoteAddress, and the second parameter represents localAddress.

Of course, another common use of connect is to connect to a remote server:

public ChannelFuture connect(String inetHost, int inetPort)

This is the kind of usage we most often use.

Summarize

The above is the use of Rendezvous in UDT.

Examples of this article can be found in: learn-netty4

This article has been included in http://www.flydean.com/41-netty-udt-byte-message/

The most popular interpretation, the most profound dry goods, the most concise tutorials, and many tricks you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", understand technology, understand you better!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com