Netty4實戰 - 編解碼技術
通常我們習慣將編碼(Encode)稱為序列化(serialization),它將物件序列化為位元組陣列,用於網路傳輸、資料持久化或者其它用途。
反之,解碼(Decode)稱為反序列化(deserialization),它把從網路、磁碟等讀取的位元組陣列還原成原始物件(通常是原始物件的拷貝),以方便後續的業務邏輯操作。
Java序列化
相信大多數Java程式設計師接觸到的第一種序列化或者編解碼技術就是Java預設提供的序列化機制,需要序列化的Java物件只需要實現java.io.Serializable介面並生成序列化ID,這個類就能夠通過java.io.ObjectInput和java.io.ObjectOutput序列化和反序列化。
其他序列化框架
Java預設的序列化機制效率很低、序列化後的碼流也較大,所以湧現出了非常多的優秀的Java序列化框架,例如:hessian、protobuf、thrift、protostuff、kryo、msgpack、avro、fst 等等。
擴充套件Netty 解碼器
Netty提供了 io.netty.handler.codec.MessageToByteEncoder
和io.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
相關文章
- 解碼知識圖譜:從核心概念到技術實戰
- 解碼注意力Attention機制:從技術解析到PyTorch實戰PyTorch
- 爬蟲技術實戰爬蟲
- 【經驗分享】RTC 技術系列之視訊編解碼
- 淺談RASP技術攻防之實戰[程式碼實現篇]
- Flutter核心技術與實戰Flutter
- 【備忘】《圖解Spark 核心技術與案例實戰》PDF圖解Spark
- 影像壓縮編碼碼matlab實現——算術編碼Matlab
- Flutter完整開發實戰詳解(二、 快速開發實戰篇) | 掘金技術徵文Flutter
- 【SpringBoot實戰】檢視技術-ThymeleafSpring Boot
- Java技術分享:NIO實戰教程!Java
- 技術管理實戰36講教程
- Hybrid App技術解析 — 實戰篇APP
- Hybrid App技術解析 -- 實戰篇APP
- 新媒體編碼時代的技術:編碼與傳輸
- kubebuilder實戰之五:operator編碼UI
- SpEL應用實戰|得物技術
- Jenkins技術概述與開發實戰Jenkins
- 深度學習DeepLearning核心技術實戰深度學習
- 《深度學習Python》核心技術實戰深度學習Python
- rust實戰系列-base64編碼Rust
- 機器學習 - 決策樹:技術全解與案例實戰機器學習
- 技術集錦 | 大資料雲原生技術實戰及最佳實踐系列大資料
- shell-【技術乾貨】工作中編寫shell指令碼實踐指令碼
- Docker容器編排技術解析與實踐Docker
- 音影片編解碼技術在直播平臺中是如何運用的?
- 一、視音訊編解碼技術零基礎(理論總結)音訊
- 提升編碼技能的 幾 種高階技術
- 深度解讀DBSCAN聚類演算法:技術與實戰全解析聚類演算法
- 技術解讀 | 基於fastText和RNN的語義消歧實戰ASTRNN
- 從RabbitMQ平滑遷移到RocketMQ技術實戰MQ
- 【Flutter實戰】移動技術發展史Flutter
- 《Python核心技術與實戰》筆記3Python筆記
- 深度強化學習核心技術實戰強化學習
- 編解碼再進化:Ali266 與下一代影片技術
- 影片與編解碼的技術邂逅,碰撞出的高畫質羅曼史
- 深度解讀GaussDB邏輯解碼技術原理
- 擁抱智慧,AI 影片編碼技術的新探索AI
- 解鎖機器學習-梯度下降:從技術到實戰的全面指南機器學習梯度