前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >netty编解码器_netty编程实战

netty编解码器_netty编程实战

作者头像
全栈程序员站长
发布2022-11-10 15:28:02
2030
发布2022-11-10 15:28:02
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

目录

​1 Java序列化的缺点

2 业界主流的编解码框架

2.1 Google的Protobuf介绍

2.2 Facebook的Thrift介绍

2.3 JBoss Marshalling介绍


第6章 编解码技术

netty编解码器_netty编程实战
netty编解码器_netty编程实战

1 Java序列化的缺点

java序列化通过实现Serializable接口来实现

  • 无法跨语言
  • 序列化后的码流太大
  • 序列化性能太低

java序列化的两个目的:网络传输和对象的持久化

对比的是java原生序列化和二进制序列化

java序列化方案:首先把对象信息写到ObjectOutputStream 中,然后再写到ByteArrayOutputStream 中,最后写到字节数组中;

二进制序列化方案:首先把name字段占用的大小写入,然后写入name的值,最后写入id;

用于测试序列化码流大小和序列化性能的对象

代码语言:javascript
复制
public class UserInfo implements Serializable {

	/**
	 * 默认的序列号
	 */
	private static final long serialVersionUID = 1L;

	private String userName;

	private int userID;

	public UserInfo buildUserName(String userName) {
		this.userName = userName;
		return this;
	}

	public UserInfo buildUserID(int userID) {
		this.userID = userID;
		return this;
	}

	/**
	 * @return the userName
	 */
	public final String getUserName() {
		return userName;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public final void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @return the userID
	 */
	public final int getUserID() {
		return userID;
	}

	/**
	 * @param userID
	 *            the userID to set
	 */
	public final void setUserID(int userID) {
		this.userID = userID;
	}

	public byte[] codeC() {
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		byte[] value = this.userName.getBytes();
		buffer.putInt(value.length);
		buffer.put(value);
		buffer.putInt(this.userID);
		buffer.flip();
		value = null;
		byte[] result = new byte[buffer.remaining()];
		buffer.get(result);
		return result;
	}

	public byte[] codeC(ByteBuffer buffer) {
		buffer.clear();
		byte[] value = this.userName.getBytes();
		buffer.putInt(value.length);
		buffer.put(value);
		buffer.putInt(this.userID);
		buffer.flip();
		value = null;
		byte[] result = new byte[buffer.remaining()];
		buffer.get(result);
		return result;
	}
}

测试码流大小:

代码语言:javascript
复制
public class TestUserInfo {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		UserInfo info = new UserInfo();
		info.buildUserID(100).buildUserName("Welcome to Netty");
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream os = new ObjectOutputStream(bos);
		os.writeObject(info);
		os.flush();
		os.close();
		byte[] b = bos.toByteArray();
		System.out.println("The jdk serializable length is : " + b.length);
		bos.close();
		System.out.println("-------------------------------------");
		System.out.println("The byte array serializable length is : " + info.codeC().length);

	}

}

输出:

The jdk serializable length is : 127 ————————————- The byte array serializable length is : 24

测试序列化性能:

代码语言:javascript
复制
public class PerformTestUserInfo {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		UserInfo info = new UserInfo();
		info.buildUserID(100).buildUserName("Welcome to Netty");
		int loop = 1000000;
		ByteArrayOutputStream bos = null;
		ObjectOutputStream os = null;
		long startTime = System.currentTimeMillis();
		for (int i = 0; i < loop; i++) {
			bos = new ByteArrayOutputStream();
			os = new ObjectOutputStream(bos);
			os.writeObject(info);
			os.flush();
			os.close();
			byte[] b = bos.toByteArray();
			bos.close();
		}
		long endTime = System.currentTimeMillis();
		System.out.println("The jdk serializable cost time is  : " + (endTime - startTime) + " ms");

		System.out.println("-------------------------------------");

		ByteBuffer buffer = ByteBuffer.allocate(1024);
		startTime = System.currentTimeMillis();
		for (int i = 0; i < loop; i++) {
			byte[] b = info.codeC(buffer);
		}
		endTime = System.currentTimeMillis();
		System.out.println("The byte array serializable cost time is : " + (endTime - startTime) + " ms");

	}

}

输出:

The jdk serializable cost time is : 2029 ms ————————————- The byte array serializable cost time is : 135 ms

2 业界主流的编解码框架

2.1 Google的Protobuf介绍

netty编解码器_netty编程实战
netty编解码器_netty编程实战
netty编解码器_netty编程实战
netty编解码器_netty编程实战

2.2 Facebook的Thrift介绍

netty编解码器_netty编程实战
netty编解码器_netty编程实战
netty编解码器_netty编程实战
netty编解码器_netty编程实战

2.3 JBoss Marshalling介绍

netty编解码器_netty编程实战
netty编解码器_netty编程实战
netty编解码器_netty编程实战
netty编解码器_netty编程实战

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月28日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 2 业界主流的编解码框架
    • 2.1 Google的Protobuf介绍
      • 2.2 Facebook的Thrift介绍
        • 2.3 JBoss Marshalling介绍
        相关产品与服务
        文件存储
        文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档