Netty4實戰 - 編解碼技術

weixin_33866037發表於2017-05-21

通常我們習慣將編碼(Encode)稱為序列化(serialization),它將物件序列化為位元組陣列,用於網路傳輸、資料持久化或者其它用途。

反之,解碼(Decode)稱為反序列化(deserialization),它把從網路、磁碟等讀取的位元組陣列還原成原始物件(通常是原始物件的拷貝),以方便後續的業務邏輯操作。

Java序列化

相信大多數Java程式設計師接觸到的第一種序列化或者編解碼技術就是Java預設提供的序列化機制,需要序列化的Java物件只需要實現java.io.Serializable介面並生成序列化ID,這個類就能夠通過java.io.ObjectInput和java.io.ObjectOutput序列化和反序列化。

其他序列化框架

Java預設的序列化機制效率很低、序列化後的碼流也較大,所以湧現出了非常多的優秀的Java序列化框架,例如:hessianprotobufthriftprotostuffkryomsgpackavrofst 等等。

擴充套件Netty 解碼器

Netty提供了 io.netty.handler.codec.MessageToByteEncoderio.netty.handler.codec.ByteToMessageDecoder介面,方便我們擴充套件編解碼。

為了擴充套件序列化框架更方便,我們首先定義Serializer介面:

import java.io.IOException;

/**
 * @author Ricky Fung
 */
public interface Serializer {

    byte[] encode(Object msg) throws IOException;

    <T> T decode(byte[] buf, Class<T> type) throws IOException;
}

定義Serializer工廠:

import com.mindflow.netty4.serialization.hessian.HessianSerializer;

/**
 * @author Ricky Fung
 */
public class SerializerFactory {

    public static Serializer getSerializer(){
        return new HessianSerializer();
    }
}

接下來,我們在Netty Decoder中使用上面定義的Serializer介面,如下:

import com.mindflow.netty4.serialization.Serializer;
import com.mindflow.netty4.serialization.SerializerFactory;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 */
public class NettyMessageDecoder<T> extends LengthFieldBasedFrameDecoder {
    private Logger logger = LoggerFactory.getLogger(getClass());

    //判斷傳送客戶端傳送過來的資料是否按照協議傳輸,頭部資訊的大小應該是 byte+byte+int = 1+1+4 = 6
    private static final int HEADER_SIZE = 6;

    private Serializer serializer = SerializerFactory.getSerializer();
    private Class<T> clazz;

    public NettyMessageDecoder(Class<T> clazz, int maxFrameLength, int lengthFieldOffset,
                               int lengthFieldLength) throws IOException {
        super(maxFrameLength, lengthFieldOffset, lengthFieldLength);
        this.clazz = clazz;
    }

    @Override
    protected Object decode(ChannelHandlerContext ctx, ByteBuf in)
            throws Exception {

        if (in.readableBytes() < HEADER_SIZE) {
            return null;
        }

        in.markReaderIndex();

        //注意在讀的過程中,readIndex的指標也在移動
        byte type = in.readByte();
        byte flag = in.readByte();

        int dataLength = in.readInt();

        //logger.info("read type:{}, flag:{}, length:{}", type, flag, dataLength);

        if (in.readableBytes() < dataLength) {
            logger.error("body length < {}", dataLength);
            in.resetReaderIndex();
            return null;
        }

        byte[] data = new byte[dataLength];
        in.readBytes(data);

        try{
            return serializer.decode(data, clazz);
        } catch (Exception e){
            throw new RuntimeException("serializer decode error");
        }
    }
}

NettyMessageEncoder.java

import com.mindflow.netty4.serialization.Serializer;
import com.mindflow.netty4.serialization.SerializerFactory;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 */
public final class NettyMessageEncoder<T> extends
        MessageToByteEncoder {
    private Logger logger = LoggerFactory.getLogger(getClass());

    private final byte type = 0X00;
    private final byte flag = 0X0F;

    private Serializer serializer = SerializerFactory.getSerializer();
    private Class<T> clazz;
    public NettyMessageEncoder(Class<T> clazz) {
        this.clazz = clazz;
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg,
                          ByteBuf out) throws Exception {

        try {
            out.writeByte(type);
            out.writeByte(flag);

            byte[] data = serializer.encode(msg);
            out.writeInt(data.length);
            out.writeBytes(data);

            //logger.info("write type:{}, flag:{}, length:{}", type, flag, data.length);
        } catch (Exception e){
            e.printStackTrace();
        }

    }
}

服務端:

import com.mindflow.netty4.serialization.model.Request;
import com.mindflow.netty4.serialization.model.Response;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;

/**
 * @author Ricky Fung
 */
public class NettyServer {
    private Logger logger = LoggerFactory.getLogger(getClass());

    public void bind() throws Exception {
        // 配置服務端的NIO執行緒組
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch)
                            throws IOException {
                        ch.pipeline().addLast(
                                new NettyMessageDecoder<>(Request.class,1<<20, 2, 4));
                        ch.pipeline().addLast(new NettyMessageEncoder(Response.class));
                        ch.pipeline().addLast(new NettyServerHandler());
                    }
                });

        // 繫結埠,同步等待成功
        ChannelFuture future = b.bind(Constants.HOST, Constants.PORT).sync();
        logger.info("Netty server start ok host:{}, port:{}"
                , Constants.HOST , Constants.PORT);

        future.channel().closeFuture().sync();
    }

    class NettyServerHandler extends SimpleChannelInboundHandler<Request> {

        @Override
        protected void channelRead0(ChannelHandlerContext context, Request request) throws Exception {

            logger.info("Rpc server receive request id:{}", request.getId());
            //處理請求
            processRpcRequest(context, request);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            logger.error("捕獲異常", cause);
        }
    }

    private void processRpcRequest(final ChannelHandlerContext context, final Request request) {

        Response response = new Response();
        response.setId(request.getId());
        response.setResult("echo "+request.getMessage());
        context.writeAndFlush(response);
    }

    public static void main(String[] args) throws Exception {
        new NettyServer().bind();
    }

}

客戶端:

import com.mindflow.netty4.serialization.model.Request;
import com.mindflow.netty4.serialization.model.Response;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 */
public class NettyClient {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private EventLoopGroup group = new NioEventLoopGroup();

    public void connect(int port, String host) throws Exception {

        // 配置客戶端NIO執行緒組
        try {
            Bootstrap b = new Bootstrap();
            b.group(group).channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch)
                                throws Exception {
                            ch.pipeline().addLast(
                                    new NettyMessageDecoder<Response>(Response.class, 1024 * 1024, 2, 4));
                            ch.pipeline().addLast(new NettyMessageEncoder<Request>(Request.class));
                            ch.pipeline().addLast(new NettyClientHandler());;
                        }
                    });
            // 發起非同步連線操作
            ChannelFuture future = b.connect(host, port).sync();

            if (future.awaitUninterruptibly(5000)) {
                logger.info("client connect host:{}, port:{}", host, port);
                if (future.channel().isActive()) {
                    logger.info("開始傳送訊息");
                    for(int i=0; i<100; i++){

                        Request req = new Request();
                        req.setId((long) i);
                        req.setMessage("hello world");

                        future.channel().writeAndFlush(req);
                    }
                    logger.info("傳送訊息完畢");
                }
            }

        } finally {

        }
    }

    class NettyClientHandler extends SimpleChannelInboundHandler<Response> {

        @Override
        protected void channelRead0(ChannelHandlerContext channelHandlerContext, Response msg) throws Exception {

            final Response response = msg;
            logger.info("Rpc client receive response id:{}", response.getId());
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            logger.error("捕獲異常", cause);
        }
    }

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        new NettyClient().connect(Constants.PORT, Constants.HOST);
    }
}

參考資料

Netty系列之Netty編解碼框架分析


Java深度歷險(十)——Java物件序列化與RMI

原始碼下載

https://github.com/TiFG/netty4-in-action/tree/master/netty4-serialization-demo

相關文章