Java IO學習筆記八:Netty入門

Grey Zeng發表於2021-06-21

作者:Grey

原文地址:Java IO學習筆記八:Netty入門

多路複用多執行緒方式還是有點麻煩,Netty幫我們做了封裝,大大簡化了編碼的複雜度,接下來熟悉一下netty的基本使用。

Netty+最樸素的阻塞的方式來實現一版客戶端和服務端通訊的程式碼,然後再重構成Netty官方推薦的寫法。

第一步,引入netty依賴包。

<dependency>
   <groupId>io.netty</groupId>
   <artifactId>netty-all</artifactId>
   <version>4.1.65.Final</version>
</dependency>

準備傳送端

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * @author <a href="mailto:410486047@qq.com">Grey</a>
 * @since
 */
public class NettyClientSync {
    public static void main(String[] args) throws Exception {
        NioEventLoopGroup thread = new NioEventLoopGroup(1);
        NioSocketChannel client = new NioSocketChannel();
        thread.register(client);
        ChannelPipeline p = client.pipeline();
        p.addLast(new MyInHandler());
        ChannelFuture connect = client.connect(new InetSocketAddress("192.168.205.138", 9090));
        ChannelFuture sync = connect.sync();
        ByteBuf buf = Unpooled.copiedBuffer("hello server".getBytes());
        ChannelFuture send = client.writeAndFlush(buf);
        send.sync();
        sync.channel().closeFuture().sync();
        System.out.println("client over....");
    }

    static class MyInHandler extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRegistered(ChannelHandlerContext ctx) {
            System.out.println("client  register...");
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            System.out.println("client active...");
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            ByteBuf buf = (ByteBuf) msg;
            CharSequence str = buf.getCharSequence(0, buf.readableBytes(), UTF_8);
            System.out.println(str);
            ctx.writeAndFlush(buf);
        }
    }
}

這個客戶端主要就是給服務端(192.168.205.138:9090)傳送資料, 啟動一個服務端:

[root@io ~]# nc -l 192.168.205.138 9090

然後啟動客戶端,服務端可以接收到客戶端發來的資料:

[root@io ~]# nc -l 192.168.205.138 9090
hello server

這就是netty實現的一個客戶端,再來看服務端的寫法:

import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;

/**
 * @author <a href="mailto:410486047@qq.com">Grey</a>
 * @since
 */
public class NettyServerSync {
    public static void main(String[] args) throws Exception {
        NioEventLoopGroup thread = new NioEventLoopGroup(1);
        NioServerSocketChannel server = new NioServerSocketChannel();
        thread.register(server);
        ChannelPipeline p = server.pipeline();
        p.addLast(new MyAcceptHandler(thread, new NettyClientSync.MyInHandler()));
        ChannelFuture bind = server.bind(new InetSocketAddress("192.168.205.1",9090));
        bind.sync().channel().closeFuture().sync();
        System.out.println("server close....");
    }

    static class MyAcceptHandler extends ChannelInboundHandlerAdapter {


        private final EventLoopGroup selector;
        private final ChannelHandler handler;

        public MyAcceptHandler(EventLoopGroup thread, ChannelHandler myInHandler) {
            this.selector = thread;
            this.handler = myInHandler;
        }

        @Override
        public void channelRegistered(ChannelHandlerContext ctx) {
            System.out.println("server registered...");
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            SocketChannel client = (SocketChannel) msg;
            ChannelPipeline p = client.pipeline();
            p.addLast(handler);
            selector.register(client);
        }
    }
}

啟動這個服務端,然後通過一個客戶端來連線這個服務端,並且向這個服務端傳送一些資料

[root@io ~]# nc 192.168.205.1 9090
hello 
hello

服務端可以感知到客戶端連線並接收到客戶端發來的資料

client  register...
client active...
hello

但是,這樣的服務端如果再接收一個客戶端連線,客戶端繼續傳送一些資料進來,服務端就會報一個錯誤:

An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
io.netty.channel.ChannelPipelineException: git.snippets.io.netty.NettyClientSync$MyInHandler is not a @Sharable handler, so can't be added or removed multiple times.

原因在這個部落格裡面說的比較清楚:Netty ChannelHandler使用報錯

我們可以發現每當有新的資料可讀時都會往這個channel的pipeline里加入handler,這裡加的是childHander。值得注意的是,我們初始化的時候這個childHandler都是同一個例項,也就說會導致不同的channel用了同一個handler,這個從netty的設計角度來說是要避免的。因為netty的一大好處就是每一個channel都有自己繫結的eventloop和channelHandler,這樣可以保證程式碼序列執行,不必考慮併發同步的問題。所以才會有checkMultiplicity這個方法來檢查這個問題。那該怎麼辦呢?netty的這段程式碼:child.pipeline().addLast(childHandler)就是用了同一個handler啊,怎麼才能為每一個channel建立不同的handler呢?
很簡單,只要寫個類繼承ChannelInitializer就行了,ChannelInitializer這個類比較特殊,你可以把它想象成是很多channelhandler的集合體,而且這個類就是@Shareable的,繼承了這個類之後你可以為每一個channel單獨建立handler,甚至是多個handler。

解決方案也很簡單,只需要在服務端傳入的handler上加上@Sharable註解即可

@ChannelHandler.Sharable
static class MyInHandler extends ChannelInboundHandlerAdapter{
 ...
}

但是對於每次服務端的Handler,如果都要加@Sharable,就會非常不好擴充套件,Netty裡面提供了一個沒有任何業務功能的並且標註為@Sharable的類:ChannelInitializer, 每個業務handler只需要重寫其initChannel()方法即可,我們可以改造一下NettyClientSync和NettyServerSync的程式碼,並用Netty推薦的寫法來修改。

客戶端改成:

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * @author <a href="mailto:410486047@qq.com">Grey</a>
 * @since
 */
public class NettyClient {
    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup(1);
        Bootstrap bs = new Bootstrap();
        ChannelFuture fu = bs
                .group(group).channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        ChannelPipeline pipeline = nioSocketChannel.pipeline();
                        pipeline.addLast(new NettyClientSync.MyInHandler());
                    }
                }).connect(new InetSocketAddress("192.168.205.138", 9090));
        Channel client = fu.channel();
        ByteBuf buf = Unpooled.copiedBuffer("Hello Server".getBytes());
        ChannelFuture future = client.writeAndFlush(buf);
        future.sync();
    }
}

啟動一個服務端,然後啟動上述客戶端程式碼,服務端可以收到資訊

[root@io ~]# nc -l 192.168.205.138 9090
Hello Server

接下來改造服務端程式碼:

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * @author <a href="mailto:410486047@qq.com">Grey</a>
 * @since
 */
public class NettyServer {
    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup(1);
        ServerBootstrap bs = new ServerBootstrap();
        ChannelFuture bind = bs
                .group(group, group)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel nioServerSocketChannel) throws Exception {
                        ChannelPipeline pipeline = nioServerSocketChannel.pipeline();
                        pipeline.addLast(new NettyClientSync.MyInHandler());
                    }
                }).bind(new InetSocketAddress("192.168.205.1", 9090));
        bind.sync().channel().closeFuture().sync();
    }
}

啟動服務端程式碼,然後通過客戶端連線服務端併傳送一些資料:

[root@io ~]# nc 192.168.205.1 9090
sdfasdfas
sdfasdfas

可以正常接收。

原始碼:Github

相關文章