Netty使用Google Protocol Buffer完成服务器高性能数据传输
一、什么是Google Protocol Buffer(protobuf官方网站)
下面是官网给的解释:
Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data. – think XML, but smaller, faster, and simpler.
协议缓冲区是一种和语言无关、平台无关的可扩展机制,用于序列化结构化的数据。相比于xml,它更小,更快,更简单。数据缓冲区常用语通信协议和数据存储。
二、ProtoBuf的性能
序列化测试对比:
Ser Time + Deser Time(ns)
下面两个网站是效率测试实验:
- https://code.google.com/archive/p/thrift-protobuf-compare/wikis/Benchmarking.wiki
- https://github.com/eishay/jvm-serializers/wiki
三、使用Intellij IDEA插件自动生成Java类
参考我的另一篇文章:Google Protocol Buffer 的使用(一)
四、Netty和protobuf整合
准备我们的proto文件
syntax = "proto3";
package com.netty.protobuf;
option java_outer_classname = "UserInfoProto";
//用户信息
message UserInfo{
//姓名
string name = 1;
//住址
repeated Address address= 2;
//年龄
uint32 age = 3;
}
//用户常用住址
message Address{
string addressname = 1;
uint32 adressno = 2;
}
服务器中设置protobuf编码器和解码器
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//protobuf解码器
pipeline.addLast(new ProtobufVarint32FrameDecoder());
pipeline.addLast(new ProtobufDecoder(UserInfoProto.UserInfo.getDefaultInstance()));
//protobuf编码器
pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast(new ProtobufEncoder());
pipeline.addLast(new NettyServerHandler());
}
- ProtobufVarint32FrameDecoder是protobuf方式的解码器,用于解决TCP粘包和拆包问题
- ProtobufDecoder 中设置我们的proto文件生成的实例,其实就是我们的目标Java类,设置方式为:UserInfoProto.UserInfo.getDefaultInstance()
- ProtobufVarint32LengthFieldPrepender和ProtobufEncoder是protobuf方式的编码器
处理类中写protobuf数据
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
LOGGER.info("client {} connected.", ctx.channel().remoteAddress());
UserInfoProto.UserInfo user = UserInfoProto.UserInfo.newBuilder()
.setName("server")
.setAge(18)
.addAddress(
UserInfoProto.Address.newBuilder()
.setAddressname("beijing 001")
.setAdressno(911))
.build();
ctx.writeAndFlush(user);
}
- 这里通过UserInfoProto.UserInfo.newBuilder()使用的时间其类的建造者模式设置用户相关信息。
- 再通过ChannelHandlerContext的writeAndFlush方法写用户数据。
处理器中读protobuf数据
private int count = 0;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
UserInfoProto.UserInfo message = (UserInfoProto.UserInfo) msg;
LOGGER.info("server received message {}:{}", ++count, message);
}
- channelRead中的Object对象通过解码之后就是一个protobuf类对象,所以可以强转:UserInfoProto.UserInfo message = (UserInfoProto.UserInfo) msg;
五、注意事项(TCP读半包处理)
这里我们只是简单使用了netty自带的ProtobufVarint32FrameDecoder解码器来处理读半包问题,我们还可以自己继承ByteToMessageDecoder类实现一个定制化的解码器。比如我们使用Java客户端和C++服务器通过protobuf协议来通信时,就需要自己实现,同时还需要考虑大端、小端模式的转换问题。
Netty使用Google Protocol Buffer完成服务器高性能数据传输的更多相关文章
- 前端后台以及游戏中使用Google Protocol Buffer详解
前端后台以及游戏中使用Google Protocol Buffer详解 0.什么是protoBuf protoBuf是一种灵活高效的独立于语言平台的结构化数据表示方法,与XML相比,protoBuf更 ...
- Google protocol buffer的配置和使用(Linux&&Windows)
最近自己的服务器做到序列化这一步了,在网上看了下,序列化的工具有boost 和google的protocol buffer, protocol buffer的效率和使用程度更高效一些,就自己琢磨下把他 ...
- Google Protocol Buffer 的使用(一)
一.什么是Google Protocol Buffer下面是官网给的解释:Protocol buffers are a language-neutral, platform-neutral exten ...
- 详解Python Google Protocol Buffer
为什么要使用PB? PB(Protocol Buffer)是 Google 开发的用于结构化数据交换格式,作为腾讯云日志服务标准写入格式.因此用于写入日志数据前,需要将日志原始数据序列化为 PB 数据 ...
- Google Protocol Buffer 的使用和原理[转]
本文转自: http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构 ...
- Google Protocol Buffer 的使用
简介 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 ...
- 学习Google Protocol buffer之概述
XML这种属于非常强大的一种格式,能存储任何你想存的数据,而且编辑起来还是比较方便的.致命的缺陷在于比较庞大,在某些情况下,序列化和解析都会成为瓶颈.这种对于实时性很强的应用来说,就不太适合了,想象下 ...
- Google Protocol Buffer的安装与.proto文件的定义
什么是protocol Buffer呢? Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准. 我理解的就是:它是一种轻便高效的结构 ...
- Google Protocol Buffer 的使用和原理
Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它 ...
随机推荐
- Index of /android/repository
放这里了,总是记不住... https://mirrors.zzu.edu.cn/android/repository/
- Docker笔记03-docker 网络模式
docker网络模式分为5种 Nat (Network Address Translation) Host other container none overlay 第一种 Nat模式 docker的 ...
- FMX 动态创建 和 销毁(释放free) 对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- Delphi 10.2 Linux 程序开发环境部署的基本步骤(网络连接方式要选择桥接或者是Host Only)
Delphi 10.2 Linux 程序开发环境部署的基本步骤 http://blog.qdac.cc/?p=4477 升級到 Delphi 10.2 Tokyo 笔记http://www.cnblo ...
- Delphi 的RTTI机制浅探3(超长,很不错)
转自:http://blog.sina.com.cn/s/blog_53d1e9210100uke4.html 目录========================================== ...
- Realm_King 之 .NET操作XML完整类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;u ...
- jquery中的DOM操作集锦
1,查找节点: 1 2 var $li = $("ul li:eq(1)");//查找元素 $li.attr("title"); //查找元素的属性值 2, ...
- 条款09:绝不在构造和析构过程中调用virtual函数
不该在构造函数和析构函数期间调用virtual函数,这一点是C++与jave/C#不同的地方之一. 假设有一个class继承体系,用来模拟股市交易如买进.卖出的订单等等.这样的交易一定要经过审计,所以 ...
- 关于Git GUI的使用方式
1.选择Clone Existing Repository 2.选择clone地址和存放位置,然后clone 3失败 4如果失败,让对方去这里(github的界面)邀请下,如果是自己就不用 5然后等待 ...
- 04 body中的相关标签
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...