前言
java的序列的机制,一种是默认的java序列化机制,这种方式效率太低。另外一种是谷歌的protobuf,但是这种我们还要写proto文件,并且我们还要使用工具来编译生成java文件,实在太麻烦。
但是protostuff却不一样,能够很好的解决上面两者的问题。
什么是Protostuff?
Protostuff也是谷歌的产品,它是基于Protobuf发展而来的,相对于Protobuf提供了更多的功能和更简易的用法,好处就是不用自己写.proto文件即可实现对象的序列化与反序列化。
编写Protostuff序列化工具类
在进行远程过程调用时,传输的对象并不唯一,这时就需要开发具有通用性的序列化工具类,即不管序列化的对象是什么类型,都可以使用该工具类进行序列化。
为了避免每次调用序列化方法和反序列化方法时都需要重新生成一个schema对象,所以把生成的schema对象保存起来,在下一次调用方法时就不需要重新生成这些schema对象,这样可以提高序列化和反序列化的性能。
工具类源码:
package cn.xpleaf.protostuff.netty.utils; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import com.dyuproject.protostuff.LinkedBuffer; import com.dyuproject.protostuff.ProtostuffIOUtil; import com.dyuproject.protostuff.runtime.RuntimeSchema; /** * 具备缓存功能的序列化工具类,基于Protostuff实现(其基于Google Protobuf实现)
*/ public class SerializationUtil { // 缓存schema对象的map private static Map<Class<?>, RuntimeSchema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, RuntimeSchema<?>>(); /** * 根据获取相应类型的schema方法 * * @param clazz * @return */ @SuppressWarnings({ "unchecked", "unused" }) private <T> RuntimeSchema<T> getSchema(Class<T> clazz) { // 先尝试从缓存schema map中获取相应类型的schema RuntimeSchema<T> schema = (RuntimeSchema<T>) cachedSchema.get(clazz); // 如果没有获取到对应的schema,则创建一个该类型的schema // 同时将其添加到schema map中 if (schema == null) { schema = RuntimeSchema.createFrom(clazz); if (schema != null) { cachedSchema.put(clazz, schema); } } // 返回schema对象 return schema; } /** * 序列化方法,将对象序列化为字节数组(对象 ---> 字节数组) * * @param obj * @return */ @SuppressWarnings("unchecked") public static <T> byte[] serialize(T obj) { // 获取泛型对象的类型 Class<T> clazz = (Class<T>) obj.getClass(); // 创建泛型对象的schema对象 RuntimeSchema<T> schema = getSchema(clazz); // 创建LinkedBuffer对象(分配一个缓存空间) LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); // 序列化 byte[] array = ProtostuffIOUtil.toByteArray(obj, schema, buffer); // 返回序列化对象 return array; } /** * 反序列化方法,将字节数组反序列化为对象(字节数组 ---> 对象) * * @param data * @param clazz * @return */ public static <T> T deserialize(byte[] data, Class<T> clazz) { // 创建泛型对象的schema对象 RuntimeSchema<T> schema = getSchema(clazz); // 根据schema实例化对象 T message = schema.newMessage(); // 将字节数组中的数据反序列化到message对象 ProtostuffIOUtil.mergeFrom(data, message, schema); // 返回反序列化对象 return message; } }
测试代码:
-
User.java
package cn.xpleaf.pojo; public class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [name=" + name + ", age=" + age + "]"; } }
-
TestUtil.java
package cn.xpleaf.protostuff.netty.utils; import static org.junit.Assert.*; import org.junit.Test; import cn.xpleaf.pojo.User; public class TestUtil { @Test public void testUtil() throws Exception { User user = new User("xpleaf", 10); System.out.println(user); // 序列化 byte[] array = SerializationUtil.serialize(user); // 反序列化 User user2 = SerializationUtil.deserialize(array, User.class); System.out.println(user2); // 判断值是否相等 System.out.println(user.toString().equals(user2.toString())); } }
- 输出结果
User [name=xpleaf, age=10] User [name=xpleaf, age=10] true
Protostuff与netty结合
在 Netty 数据传输过程中可以有很多选择,比如;字符串、json、xml、java 对象,但为了保证传输的数据具备;良好的通用性、方便的操作性和传输的高性能,我们可以选择 protostuff 作为我们的数据传输格式。
以下示例采用protostuff作为序列化框架,同时处理了粘包和折包的问题。
代码示例
POJO开发
- EchoRequest.java
package cn.xpleaf.protostuff.netty.pojo;
/**
* EchoRequest是client向server端发送数据的传输载体,将需要进行传输的pojo对象统一封装到EchoRequest对象中,
* 这样会为编解码工作带来很大的方便性和统一性,同时也可以携带其它信息, 对于后面对程序进行扩展会有非常大的帮助
*/
public class EchoRequest {
private String requestId;
private Object requestObj;
private Class<?> requestObjClass;
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public Object getRequestObj() {
return requestObj;
}
public void setRequestObj(Object requestObj) {
this.requestObj = requestObj;
}
public Class<?> getRequestObjClass() {
return requestObjClass;
}
public void setRequestObjClass(Class<?> requestObjClass) {
this.requestObjClass = requestObjClass;
}
}
- EchoResponse.java
package cn.xpleaf.protostuff.netty.pojo;
/**
* EchoResponse是server向client端发送数据的传输载体,将需要进行传输的pojo对象统一封装到EchoResponse对象中,
* 这样会为编解码工作带来很大的方便性和统一性,同时也可以携带其它信息, 对于后面对程序进行扩展会有非常大的帮助
*/
public class EchoResponse {
private String responseId;
private Object responseObj;
private Class<?> responseObjClass;
public String getResponseId() {
return responseId;
}
public void setResponseId(String responseId) {
this.responseId = responseId;
}
public Object getResponseObj() {
return responseObj;
}
public void setResponseObj(Object responseObj) {
this.responseObj = responseObj;
}
public Class<?> getResponseObjClass() {
return responseObjClass;
}
public void setResponseObjClass(Class<?> responseObjClass) {
this.responseObjClass = responseObjClass;
}
}
- User.java
package cn.xpleaf.protostuff.netty.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}
编解码器开发
- ProtostuffEncoder.java
package cn.xpleaf.protostuff.netty.utils; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; /** * ProtostuffEncoder继承自Netty中的MessageToByteEncoder类, * 并重写抽象方法encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) * 它负责将Object类型的POJO对象编码为byte数组,然后写入到ByteBuf中
*/ public class ProtostuffEncoder extends MessageToByteEncoder<Object> { @Override protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception { // 直接生成序列化对象 // 需要注意的是,使用protostuff序列化时,不需要知道pojo对象的具体类型也可以进行序列化时 // 在反序列化时,只要提供序列化后的字节数组和原来pojo对象的类型即可完成反序列化 byte[] array = SerializationUtil.serialize(msg);
int dataLength = array.length;
out.writeShort(MessageCodecUtil.messageMagic);
out.writeInt(datalength);
out.writeBytes(array);
}
}
- ProtostuffDecoder.java
package cn.xpleaf.protostuff.netty.utils; import java.util.List; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageDecoder; /** * PojoDecoder继承自Netty中的MessageToMessageDecoder类, * 并重写抽象方法decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) * 首先从数据报msg(数据类型取决于继承MessageToMessageDecoder时填写的泛型类型)中获取需要解码的byte数组 * 然后调用使用序列化工具类将其反序列化(解码)为Object对象 将解码后的对象加入到解码列表out中,这样就完成了解码操作 */ public class ProtostuffDecoder extends ByteToMessageDecoder<ByteBuf> {
private static final int HEAD_LENGTH = 6; // 需要反序列对象所属的类型 private Class<?> genericClass; // 构造方法,传入需要反序列化对象的类型 public ProtostuffDecoder(Class<?> genericClass) { this.genericClass = genericClass; } @Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { in.markReaderIndex(); if (!in.isReadable()) { logger.error("[MessageDecoder.decode]:ByteBuf is Unreadable."); in.resetReaderIndex(); return; } if (in.readableBytes() < HEAD_LENGTH) { logger.error("[MessageDecoder.decode]:Readable Bytes length(" + in.readableBytes() + ") less than 7 bytes(Head Length), ignored."); in.resetReaderIndex(); return; } short dataMagic = in.readShort();if (dataMagic != MessageCodecUtil.messageMagic) { logger.error("[MessageDecoder.decode]:Illegal message,dataMagic="+dataMagic); in.skipBytes(in.readableBytes()); ctx.close(); return; } int length = in.readInt();if (length < 0) { logger.error("[MessageDecoder.decode]:message length less than 0, channel closed."); in.skipBytes(in.readableBytes()); ctx.close(); return; } byte dataType = in.readByte(); if (in.readableBytes() < length) { in.resetReaderIndex(); return; } byte[] msgBody = new byte[length]; in.readBytes(msgBody);
// 反序列化对象 Object obj = SerializationUtil.deserialize(msgBody, this.genericClass); // 添加到反序列化对象结果列表 out.add(obj); } }
Netty服务端程序开发
-
NettyServer.java
package cn.xpleaf.protostuff.netty.echoservice; import cn.xpleaf.protostuff.netty.pojo.EchoRequest;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; public class NettyServer { public void bind(int port) throws Exception { // 配置服务端NIO线程组 EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { // 添加解码器 ch.pipeline().addLast(new ProtostuffDecoder(EchoRequest.class)); // 添加编码器 ch.pipeline().addLast(new ProtostuffEncoder()); // 添加业务处理handler ch.pipeline().addLast(new ServerHandler()); } }); // 绑定端口,同步等待成功,该方法是同步阻塞的,绑定成功后返回一个ChannelFuture ChannelFuture f = b.bind(port).sync(); // 等待服务端监听端口关闭,阻塞,等待服务端链路关闭之后main函数才退出 f.channel().closeFuture().sync(); } finally { // 优雅退出,释放线程池资源 bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port = 8080; if(args != null && args.length > 0) { try { port = Integer.valueOf(port); } catch (NumberFormatException e) { // TODO: handle exception } } new NettyServer().bind(port); } }
-
ServerHandler.java
package cn.xpleaf.protostuff.netty.echoservice; import java.util.UUID; import cn.xpleaf.protostuff.netty.pojo.EchoRequest; import cn.xpleaf.protostuff.netty.pojo.EchoResponse; import cn.xpleaf.protostuff.netty.pojo.User; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; public class ServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 接收到的对象的类型为EchoRequest EchoRequest req = (EchoRequest) msg; System.out.println(req.getRequestId() + " : " + req.getRequestObj()); // 创建需要传输的user对象 User user = new User(); user.setName("server"); user.setAge(10); // 创建传输的user对象载体EchoRequest对象 EchoResponse resp = new EchoResponse(); // 设置responseId resp.setResponseId(UUID.randomUUID().toString()); // 设置需要传输的对象 resp.setResponseObj(user); // 设置需要传输的对象的类型 resp.setResponseObjClass(resp.getResponseObj().getClass()); // 调用writeAndFlush将数据发送到socketChannel ctx.writeAndFlush(resp); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { ctx.close(); } }
Netty客户端程序开发
-
NettyClient.java
package cn.xpleaf.protostuff.netty.echoservice; import cn.xpleaf.protostuff.netty.pojo.EchoResponse; import cn.xpleaf.protostuff.netty.utils.EchoDecoder; import cn.xpleaf.protostuff.netty.utils.EchoEncoder; import io.netty.bootstrap.Bootstrap; 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.NioSocketChannel; public class NettyClient { public void connect(int port, String host) throws Exception { // 配置客户端NIO线程组 EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) // 设置TCP连接超时时间 .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { // 添加解码器 ch.pipeline().addLast(new ProtostuffDecoder(EchoResponse.class)); // 添加编码器 ch.pipeline().addLast(new ProtostuffEncoder()); // 添加业务处理handler ch.pipeline().addLast(new ClientHandler()); } }); // 发起异步连接操作(注意服务端是bind,客户端则需要connect) ChannelFuture f = b.connect(host, port).sync(); // 等待客户端链路关闭 f.channel().closeFuture().sync(); } finally { // 优雅退出,释放NIO线程组 group.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port = 8080; if(args != null && args.length > 0) { try { port = Integer.valueOf(port); } catch (NumberFormatException e) { // 采用默认值 } } new NettyClient().connect(port, "localhost"); } }
- ClientHandler.java
package cn.xpleaf.protostuff.netty.echoservice; import java.util.UUID; import cn.xpleaf.protostuff.netty.pojo.EchoRequest; import cn.xpleaf.protostuff.netty.pojo.EchoResponse; import cn.xpleaf.protostuff.netty.pojo.User; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; public class ClientHandler extends ChannelInboundHandlerAdapter { @Override public void channelActive(ChannelHandlerContext ctx) { // 创建需要传输的user对象 User user = new User(); user.setName("client"); user.setAge(10); // 创建传输的user对象载体EchoRequest对象 EchoRequest req = new EchoRequest(); // 设置requestId req.setRequestId(UUID.randomUUID().toString()); // 设置需要传输的对象 req.setRequestObj(user); // 设置需要传输的对象的类型 req.setRequestObjClass(req.getRequestObj().getClass()); // 调用writeAndFlush将数据发送到socketChannel ctx.writeAndFlush(req); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 接收到的对象的类型为EchoResponse EchoResponse resp = (EchoResponse) msg; System.out.println(resp.getResponseId() + " : " + resp.getResponseObj()); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } }
测试执行
分别执行NettyServer.java
和NettyClient.java
,服务端和客户端输出如下:
服务端:
4b76d70d-7a31-4738-8daa-ca4f40483e7e : User [name=client, age=10]
客户端:
e40b6e34-33a3-485e-bb8f-7157ee324e97 : User [name=server, age=10]
附录:pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.xpleaf</groupId> <artifactId>Chapter08</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java --> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.5.1</version> </dependency> <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.21.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/com.dyuproject.protostuff/protostuff-core --> <dependency> <groupId>com.dyuproject.protostuff</groupId> <artifactId>protostuff-core</artifactId> <version>1.1.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.dyuproject.protostuff/protostuff-runtime --> <dependency> <groupId>com.dyuproject.protostuff</groupId> <artifactId>protostuff-runtime</artifactId> <version>1.1.3</version> </dependency> </dependencies> <build> <plugins> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
引用: