发现一个好东西Protostuff, 据说可以不写proto文件来把对象序列化成二进制格式,用了一下果然好使。
public class Protostuff {
@SuppressWarnings("unchecked")
public static <T> byte[] serial(T obj) {
Class<T> clazz = (Class<T>) obj.getClass();
// RuntimeSchema会缓存类信息,不用自己实现缓存了,爽
Schema<T> schema = RuntimeSchema.getSchema(clazz);
// 缓存buff, 不够会自动增长
LinkedBuffer buffer = LinkedBuffer.allocate(1024);
// 序列化成protobuf的二进制数据
byte[] data = null;
try {
data = ProtobufIOUtil.toByteArray(obj, schema, buffer);
} finally {
buffer.clear();
}
return data;
}
public static <T> T deserial(byte[] data, Class<T> clazz) {
Schema<T> schema = RuntimeSchema.getSchema(clazz);
T message = schema.newMessage();
ProtobufIOUtil.mergeFrom(data, message, schema);
return message;
}
}
测试对象可以使用注解指定字段序号,排除字段等(另外还有Rpc,Request和Response等注解指定rpc服务):
public class Person{ @Tag(1) public int id; @Tag(2) public String name; @Exclude public String email; @Tag(3) public int age; @Tag(4) public Student student; @Tag(5) public List<Student> list = new ArrayList<Student>(); @Tag(6) public Map<String, Student> map = new HashMap<String, Student>(); @Tag(7) public String s; } public class Student { @Tag(1) private String email; @Tag(2) private int age; public Student(){ } public Student(String email, int age) { super(); this.email = email; this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
测试:
public static void main(String[] args)throws Exception { Person person1 = new Person(); person1.id = 10086; person1.name = "ken"; person1.email = "ken@iamcoding.com"; person1.age = 10; person1.student = new Student("yuan@aliyun.com", 20); person1.list.add(new Student("yuan2@aliyun.com", 30)); for(int i=0; i<2000; i++){ person1.list.add(new Student("yuan3@aliyun.com", 40)); } person1.map.put("1", new Student("yuan4@aliyun.com", 400)); person1.map.put("2", new Student("yuan5@aliyun.com", 500)); // 序列化成protobuf的二进制数据 byte[] data = Protostuff.serial(person1); Files.write(Paths.get("/data/Persion.data"), data); // 反序列化 Person person2 = Protostuff.deserial(Files.readAllBytes(Paths.get("/data/Persion.data")), Person.class); System.out.println(person2.id); System.out.println(person2.name); System.out.println(person2.email); System.out.println(person2.age); System.out.println(person2.student.getAge()); System.out.println(person2.student.getEmail()); System.out.println("***" + person2.list.size()); System.out.println("***" + person2.list.get(0).getAge()); System.out.println("***" + person2.list.get(0).getEmail()); System.out.println("===" + person2.map.get("1").getAge()); System.out.println("===" + person2.map.get("1").getEmail()); }
Protostuff使用还是比较简单的,但是没有发现序列化之前的回调方法注解和反序列化后的回调方法注解,不过这好实现不算问题。
另外对象增加字段后,仍然能够反序列化老数据,这个做的也不错。
Protostuff只能在JAVA中使用,完全可以替代FST,msgpack, 等一些二进制协议,用来将对象存储到缓存或者db中。
不过对于通信协议来说 写proto文件会更清晰, 所以通信使用protobuf更好!
相关推荐
提供prostuff序列化源码,直接将需要的源码拷进项目即可。
今天小编就为大家分享一篇关于Protostuff序列化和反序列化的使用说明,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
包括了序列化和反序列化功能,将对应包拷入工程即可使用
支持jdk1.6以上版本,protostuff源码,拷入项目即可使用
protostuff fastjson gson 高性能序列化jar包
protostuff 是一个支持各种格式的一个序列化Java类库,包括 JSON、XML、YAML等格式。示例代码:public class UserSchema implements Schema { public boolean isInitialized(User user) { return...
所依赖的所有jar包; threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: com/dyuproject/protostuff/MapSchema$MessageFactory] with root cause
protostuff 是一个支持各种格式的一个序列化Java类库,包括 JSON、XML、YAML等格式
默认Redis作为缓存,并使用ProtoStuff序列化/反序列化对象 统一的异常处理和优雅的接口输出 RESTful接口风格 可持续集成其他所需的服务 【代码结构】 ─common 公共库 │ ├─data 数据访问处理 │ ├─exception ...
基于SpringBoot搭建 Redis操作 包括String、Hash、List、Set、ZSet 使用protostuff序列化和反序列化 注释齐全欢迎大家下载使用
可以对数据进行序列化和反序列化提高传输的效率,实现款平台传输。比protobuf使用简单
Redis缓存(ProtoStuff序列化) Redis Sentinel主从高可用方案 Redis Cluster集群高可用方案 Druid(数据源配置 sql防注入 sql性能监控) 前后端分离(Html替代Jsp) Nginx静态加载、负载均衡 基于keepalived的nginx...
JedisSerialization实现利用Jedis向Redis中写入和得到Java对象的几种序列化工具:JDK、XML、JSON、Protostuff
java实现的RPC框架,使用技术:JDK动态代理、Socket通信(NIO方式)、反射、注解、Protostuff序列化、zookeeper、Spring
简单后台管理系统 基于maven管理,整合最新spring mvc 4.3.3.RELEASE版整合了mybatis 3.4.1,涵盖了目前互联网web系统最流行的组件,log4j2日志、freemarker模板、protostuff序列化、fastjson、redis、spring-data-...
支持基于Protostuff序列化框架实现 支持基于XSON序列化框架实现 提供基于Feign的RestService动态创建 提供基于easyjson的编码器,解码器 集成负载均衡功能 提供多种param Expander 筛选器: 访问日志过滤器 编码...
Netty + Protostuff + ZooKeeper 实现了一个轻量级 RPC 框架,使用 Spring 提供依赖注入与参数配置,使用 Netty 实现 NIO 方式的数据传输,使用 Protostuff 实现对象序列化,使用 ZooKeeper 实现服务注册与发现。...
Spring:它是最强大的依赖注入框架,也...Protostuff:它基于 Protobuf 序列化框架,面向 POJO,无需编写 .proto 文件。 ZooKeeper:提供服务注册与发现功能,开发分布式系统的必备选择,同时它也具备天生的集群能力。
SNRPC -- 一个简单的 netty RPC 框架,序列化器使用 protostuff-1.07,nio 使用 netty-3.2.1。 ##如何使用例如1、服务器类; 接口和实现者 // 定义一个接口: public interface SnRpcInterface { public String ...
1、使用protostuff序列化(.proto文件编写恶心,与Protocol Buffer性能几乎接近) 2、使用Netty进行通讯(同节点RPC不走网络,直接入收件箱队列); 3、路由策略:随机路由、指定Key路由、资源Id路由、强制路由 4、...