編解碼技術(shù)說白了就是java序列化技術(shù)淀歇,序列化的目的就兩個易核,一個是進行網(wǎng)絡(luò)傳輸,一個是對象持久化浪默。
我們可以說使用java序列化牡直,用netty進行傳輸,但是java序列化缺點太多纳决。例如:java序列化沒法跨語言碰逸、序列化后碼流太大、序列化性能太低等等阔加。
幾個主流的編解碼框架:
1.google的Protobuf
Protobuf是google開源的項目饵史,全稱 Google Protocol Buffers.特點:
1.結(jié)構(gòu)化數(shù)據(jù)存儲格式(xml,json等)
2.高性能編解碼技術(shù)
3.語言和平臺無關(guān),擴展性好
4.支持java,C++,Python三種語言掸哑。
2.faceBook的Thrift
Thrift源于faceBook约急,2007年facebook將Thrift做為一個開源項目交給了apache基金會零远。特點:
1.Thrift支持多種語言(C++,C#,Cocoa,Erlag,Haskell,java,Ocami,Perl,PHP,Python,Ruby,和SmallTalk)
2.Thrift適用了組建大型數(shù)據(jù)交換及存儲工具苗分,對于大型系統(tǒng)中的內(nèi)部數(shù)據(jù)傳輸,相對于Json和xml在性能上和傳輸大小上都有明顯的優(yōu)勢牵辣。
3.Thrift支持三種比較典型的編碼方式摔癣。(通用二進制編碼,壓縮二進制編碼纬向,優(yōu)化的可選字段壓縮編解碼)
3.JBoss Marshlling介紹
JBoss Marshalling是一個java對象的序列化API包择浊,修正了java自帶的序列化包的很多問題,但又保持跟java.io.Serializable接口的兼容逾条,同時又增加了一些可調(diào)的參數(shù)和附加特性琢岩,并且這些參數(shù)和特性可通過工廠類的配置,师脂,很強大啊有木有担孔。特點:
1.可拔插的類解析器,提供更加便捷的類加載定制策略吃警,通過一個接口即可實現(xiàn)定制糕篇。
2.可拔插的對象替換技術(shù),不需要通過繼承的方式酌心。
3.可拔插的預(yù)定義類緩存表拌消,可以減少序列化的字節(jié)數(shù)組長度,提升常用類型的對象序列化性能安券。
4.無須實現(xiàn)java.io.Serializable接口
5.通過緩存技術(shù)提升對象的序列化性能墩崩。
6.使用非常簡單
接下來就是netty和marshalling代碼 實現(xiàn)序列化了
server端
package com.netty.serialize;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
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;
public class Server {
public static void main(String[] args) throws Exception{
EventLoopGroup pGroup = new NioEventLoopGroup();
EventLoopGroup cGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.group(pGroup, cGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
sc.pipeline().addLast(new ServerHandler());
}
});
ChannelFuture cf = b.bind(8888).sync();
cf.channel().closeFuture().sync();
pGroup.shutdownGracefully();
cGroup.shutdownGracefully();
}
}
servlerHandler
package com.netty.serialize;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class ServerHandler extends ChannelInboundHandlerAdapter{
/**
* 通道激活
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("數(shù)據(jù)激活");
}
/**
* 通道數(shù)據(jù)讀取
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Req req = (Req) msg;
System.out.println("服務(wù)器收到數(shù)據(jù):"+ req.getId() + ", " + req.getName() + ", " + req.getRequestMessage());
Resp resp = new Resp();
resp.setId(req.getId());
resp.setName("resp" + req.getId());
resp.setResponseMessage("響應(yīng)內(nèi)容" + req.getId());
ctx.writeAndFlush(resp);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("數(shù)據(jù)讀取完畢");
}
}
client端
package com.netty.serialize;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.io.File;
import java.io.FileInputStream;
public class Client {
public static void main(String[] args) throws Exception{
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
sc.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture cf = b.connect("127.0.0.1", 8888).sync();
for(int i = 0; i < 5; i++ ){
Req req = new Req();
req.setId("" + i);
req.setName("pro" + i);
req.setRequestMessage("數(shù)據(jù)信息" + i);
cf.channel().writeAndFlush(req);
}
cf.channel().closeFuture().sync();
group.shutdownGracefully();
}
}
clientHandler
package com.netty.serialize;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
public class ClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("通道激活");
}
/**
* 通道數(shù)據(jù)讀取
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
Resp resp = (Resp)msg;
System.out.println("Client : " + resp.getId() + ", " + resp.getName() + ", " + resp.getResponseMessage());
} finally {
ReferenceCountUtil.release(msg);
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("接收返回信息完成");
}
}
MarshallingCodeCFactory
package com.netty.serialize;
import io.netty.handler.codec.marshalling.DefaultMarshallerProvider;
import io.netty.handler.codec.marshalling.DefaultUnmarshallerProvider;
import io.netty.handler.codec.marshalling.MarshallerProvider;
import io.netty.handler.codec.marshalling.MarshallingDecoder;
import io.netty.handler.codec.marshalling.MarshallingEncoder;
import io.netty.handler.codec.marshalling.UnmarshallerProvider;
import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;
/**
* Marshalling工廠
* @author(alienware)
*/
public final class MarshallingCodeCFactory {
/**
* 創(chuàng)建Jboss Marshalling解碼器MarshallingDecoder
* @return MarshallingDecoder
*/
public static MarshallingDecoder buildMarshallingDecoder() {
//首先通過Marshalling工具類的精通方法獲取Marshalling實例對象 參數(shù)serial標識創(chuàng)建的是java序列化工廠對象氓英。
final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
//創(chuàng)建了MarshallingConfiguration對象,配置了版本號為5
final MarshallingConfiguration configuration = new MarshallingConfiguration();
configuration.setVersion(5);
//根據(jù)marshallerFactory和configuration創(chuàng)建provider
UnmarshallerProvider provider = new DefaultUnmarshallerProvider(marshallerFactory, configuration);
//構(gòu)建Netty的MarshallingDecoder對象泰鸡,倆個參數(shù)分別為provider和單個消息序列化后的最大長度
MarshallingDecoder decoder = new MarshallingDecoder(provider, 1024 * 1024 * 1);
return decoder;
}
/**
* 創(chuàng)建Jboss Marshalling編碼器MarshallingEncoder
* @return MarshallingEncoder
*/
public static MarshallingEncoder buildMarshallingEncoder() {
final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
final MarshallingConfiguration configuration = new MarshallingConfiguration();
configuration.setVersion(5);
MarshallerProvider provider = new DefaultMarshallerProvider(marshallerFactory, configuration);
//構(gòu)建Netty的MarshallingEncoder對象债蓝,MarshallingEncoder用于實現(xiàn)序列化接口的POJO對象序列化為二進制數(shù)組
MarshallingEncoder encoder = new MarshallingEncoder(provider);
return encoder;
}
}
請求對象
package com.netty.serialize;
import java.io.Serializable;
public class Req implements Serializable{
private static final long SerialVersionUID = 1L;
private String id ;
private String name ;
private String requestMessage ;
private byte[] attachment;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRequestMessage() {
return requestMessage;
}
public void setRequestMessage(String requestMessage) {
this.requestMessage = requestMessage;
}
public byte[] getAttachment() {
return attachment;
}
public void setAttachment(byte[] attachment) {
this.attachment = attachment;
}
}
返回對象
package com.netty.serialize;
import java.io.Serializable;
public class Resp implements Serializable{
private static final long serialVersionUID = 1L;
private String id;
private String name;
private String responseMessage;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getResponseMessage() {
return responseMessage;
}
public void setResponseMessage(String responseMessage) {
this.responseMessage = responseMessage;
}
}
測試結(jié)果 server端
數(shù)據(jù)激活
服務(wù)器收到數(shù)據(jù):0, pro0, 數(shù)據(jù)信息0
服務(wù)器收到數(shù)據(jù):1, pro1, 數(shù)據(jù)信息1
服務(wù)器收到數(shù)據(jù):2, pro2, 數(shù)據(jù)信息2
服務(wù)器收到數(shù)據(jù):3, pro3, 數(shù)據(jù)信息3
服務(wù)器收到數(shù)據(jù):4, pro4, 數(shù)據(jù)信息4
數(shù)據(jù)讀取完畢
測試結(jié)果 client端
通道激活
Client : 0, resp0, 響應(yīng)內(nèi)容0
接收返回信息完成
Client : 1, resp1, 響應(yīng)內(nèi)容1
Client : 2, resp2, 響應(yīng)內(nèi)容2
Client : 3, resp3, 響應(yīng)內(nèi)容3
Client : 4, resp4, 響應(yīng)內(nèi)容4
接收返回信息完成