netty series: true equality – Rendezvous in UDT

brief introduction

In all the knowledge of netty we mentioned before, netty seems to be divided into client-side and server-side. The server listens to the connection and processes the messages in the connection. The client establishes a request connection to the server, so that it can send messages.

However, all this should be terminated in UDT protocol, because UDT provides Rendezvous, an equal connection type, and peer-to-peer relationship between nodes.

There has never been a savior, nor an immortal or an emperor, but only good brothers who are nodes together.

Establish a server that supports Rendezvous

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

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 represents stream and message formats respectively:

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

There are two types of nifactory. Nifactory, of course BYTE_ Rendezvous and nioudtprovider BYTE_ RENDEZVOUS.

There are two ways to create a byte stream. 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 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 the processing of messages.

Handling different messages

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

For UDT in byte format, the message transmitted in the channel is ByteBuf. We only need to build the message of ByteBuf and then 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 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 as follows:

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 relationship, so how to connect these two nodes?

We call the connect method of Bootstrap as follows:

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

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 also our most common usage.

summary

The above is the use of Rendezvous in UDT.

Examples of this article can refer to: 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 tutorial, and many tips you don't know are waiting for you to find!

Welcome to my official account: "those things in procedure", understand technology, know you better!

Keywords: Java Netty

Added by notsleepy on Fri, 11 Feb 2022 11:43:09 +0200