简介

netty中有很多适配不同协议的编码工具,对于流行的google出品的protobuf也不例外。netty为其提供了ProtobufDecoder和ProtobufEncoder两个工具还有对应的frame detection,接下来我们会通过一个例子来详细讲解如何在netty中使用protobuf。

定义protobuf

我们举个最简单的例子,首先定义一个需要在网络中进行传输的message,这里我们定义一个student对象,他有一个age和一个name属性,如下所示:

syntax = "proto3";

package com.flydean17.protobuf;

option java_multiple_files = true;
option java_package = "com.flydean17.protobuf";
option java_outer_classname = "StudentWrapper"; message Student {
optional int32 age = 1;
optional string name =2;
}

使用下面的命令,对其进行编译:

 protoc --experimental_allow_proto3_optional  -I=. --java_out=. student.proto

可以看到生成了3个文件,分别是Student,StudentOrBuilder和StudentWrapper。其中Student和StudentOrBuilder是我们真正需要用到的对象。

定义handler

在handler中,我们主要进行对消息进行处理,这里我们在clientHandler中进行消息的构建和发送,StudentClientHandler继承SimpleChannelInboundHandler并重新channelActive方法, 在该方法中我们使用protobuf的语法,构建一个新的Student实例,并给他设置好age和name两个属性。

然后使用ctx.write和ctx.flush方法将其发送到server端:

    public void channelActive(ChannelHandlerContext ctx) throws Exception {
// channel活跃
//构建一个Student,并将其写入到channel中
Student student= Student.newBuilder().setAge(22).setName("flydean").build();
log.info("client发送消息{}",student);
ctx.write(student);
ctx.flush();
}

StudentServerHandler也是继承SimpleChannelInboundHandler,并重写channelRead0方法,当server端读取到student消息的时候,日志输出,并将其回写到channel中,供clientHandler读取:

    public void channelRead0(ChannelHandlerContext ctx, Student student) throws Exception {
log.info("server收到消息{}",student);
// 写入消息
ChannelFuture future = ctx.write(student);
}

当client读取到消息之后,直接日志输出,不再做进一步处理,到此,一轮client和server端的交互就完成了:

    public void channelRead0(ChannelHandlerContext ctx, Student student) throws Exception {
log.info("client收到消息{}",student);
}

设置ChannelPipeline

在上一节,不管是在StudentClientHandler还是在StudentServerHandler中,我们都假设channel中传递的对象就是Student,而不是原始的ByteBuf。这是怎么做到的呢?

这里我们需要使用到netty提供的frame detection,netty为protobuf协议专门提供了ProtobufDecoder和ProtobufEncoder,用于对protobuf对象进行编码和解码。

但是这两个编码和解码器分别是MessageToMessageEncoder和MessageToMessageDecoder,他们是消息到消息的编码和解码器,所以还需要和frame detection配合使用。

netty同样提供了和protobuf配合使用的frame detector,他们是ProtobufVarint32FrameDecoder和ProtobufVarint32LengthFieldPrepender。

Varint32指的是protobuf的编码格式,第一个字节使用的是可变的varint。

有了frame detector和编码解码器,我们只需要将其顺序加入ChannelPipeline即可。

在客户端,StudentClientInitializer继承自ChannelInitializer,我们需要重写其initChannel方法:

    public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline(); p.addLast(new ProtobufVarint32FrameDecoder());
p.addLast(new ProtobufDecoder(Student.getDefaultInstance())); p.addLast(new ProtobufVarint32LengthFieldPrepender());
p.addLast(new ProtobufEncoder()); p.addLast(new StudentClientHandler());
}

在服务器端,同样StudentServerInitializer也继承自ChannelInitializer,也需要重写其initChannel方法:

    public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline(); p.addLast(new ProtobufVarint32FrameDecoder());
p.addLast(new ProtobufDecoder(Student.getDefaultInstance())); p.addLast(new ProtobufVarint32LengthFieldPrepender());
p.addLast(new ProtobufEncoder()); p.addLast(new StudentServerHandler());
}

这样ChannelPipeline也设置完成了。

构建client和server端并运行

最后好做的就是构建client和server端并运行,这和普通的netty客户端和服务器端并没有什么区别:

构建StudentClient:

   public static void main(String[] args) throws Exception {

        EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new StudentClientInitializer());
// 建立连接
Channel ch = b.connect(HOST, PORT).sync().channel();
// 等待关闭
ch.closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}

构建StudentServer:

   public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new StudentServerInitializer()); b.bind(PORT).sync().channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}

运行可得:

server端:
[nioEventLoopGroup-3-1] INFO c.f.protobuf.StudentServerHandler - server收到消息age: 22
name: "flydean" [nioEventLoopGroup-2-1] INFO c.f.protobuf.StudentClientHandler - client发送消息age: 22
name: "flydean" client端:
[nioEventLoopGroup-2-1] INFO c.f.protobuf.StudentClientHandler - client收到消息age: 22
name: "flydean"

可见Student消息已经发送和接收成功了。

总结

netty提供了很多和协议适配的工具类,这样我们就可以专注于业务逻辑,不需要考虑具体的编码转换的问题,非常好用。

本文的例子可以参考:learn-netty4

本文已收录于 http://www.flydean.com/17-netty-protobuf/

最通俗的解读,最深刻的干货,最简洁的教程,众多你不知道的小技巧等你来发现!

欢迎关注我的公众号:「程序那些事」,懂技术,更懂你!

netty系列之:在netty中使用protobuf协议的更多相关文章

  1. netty系列之:在netty中使用native传输协议

    目录 简介 native传输协议的依赖 netty本地传输协议的使用 总结 简介 对于IO来说,除了传统的block IO,使用最多的就是NIO了,通常我们在netty程序中最常用到的就是NIO,比如 ...

  2. netty系列之:使用netty搭建websocket服务器

    目录 简介 netty中的websocket websocket的版本 FrameDecoder和FrameEncoder WebSocketServerHandshaker WebSocketFra ...

  3. netty系列之:在netty中处理CORS

    目录 简介 服务端的CORS配置 CorsConfigBuilder CorsHandler netty对cors的支持 总结 简介 CORS的全称是跨域资源共享,他是一个基于HTTP-header检 ...

  4. netty系列之: 在netty中使用 tls 协议请求 DNS 服务器

    目录 简介 支持DoT的DNS服务器 搭建支持DoT的netty客户端 TLS的客户端请求 总结 简介 在前面的文章中我们讲过了如何在netty中构造客户端分别使用tcp和udp协议向DNS服务器请求 ...

  5. netty系列之:使用netty搭建websocket客户端

    目录 简介 浏览器客户端 netty对websocket客户端的支持 WebSocketClientHandshaker WebSocketClientCompressionHandler netty ...

  6. netty系列之:使用netty实现支持http2的服务器

    目录 简介 基本流程 CleartextHttp2ServerUpgradeHandler Http2ConnectionHandler 总结 简介 上一篇文章中,我们提到了如何在netty中配置TL ...

  7. netty系列之:请netty再爱UDT一次

    目录 简介 netty对UDT的支持 搭建一个支持UDT的netty服务 异常来袭 TypeUDT和KindUDT 构建ChannelFactory SelectorProviderUDT 使用UDT ...

  8. go微服务系列(四) - http api中引入protobuf

    1. protobuf相关依赖安装 2. 改造之前的client 2.1 新建proto文件 2.2 运行protoc命令生成go文件 2.3 然后把原来的map修改成具体的类型就可以了 3. 处理j ...

  9. 1. 彤哥说netty系列之开篇(有个问卷调查)

    你好,我是彤哥,本篇是netty系列的第一篇. 欢迎来我的公从号彤哥读源码系统地学习源码&架构的知识. 简介 本文主要讲述netty系列的整体规划,并调查一下大家喜欢的学习方式. 知识点 ne ...

随机推荐

  1. C语言代码段

    /* 功 能:将str字符串中的oldstr字符串替换为newstr字符串 * 参 数:str:操作目标 oldstr:被替换者 newstr:替换者 * 返回值:返回替换之后的字符串 */ char ...

  2. Flask(11)- 操作 Cookie

    前言 Cookie 详解:https://www.cnblogs.com/poloyy/p/12513247.html 这一节来瞧一瞧如何用 Flask 操作 Cookie 接下来就是 实战栗子!!! ...

  3. .NET Core/.NET5/.NET6 开源项目汇总11:WPF组件库1

    系列目录     [已更新最新开发文章,点击查看详细] WPF(Windows Presentation Foundation)是微软推出的基于Windows 的用户界面框架,属于.NET Frame ...

  4. ArchLinux安装步骤(一)

    本文为安装archlinux的教程,需要有硬盘分区,挂载等基础linux命令的了解还有vim的基本操作,不知道也没关系,这里有大神的视频教程ArchLinux指南. 确实是不是uefi模式 ls /s ...

  5. 【网络IO系列】 预备知识 操作系统之内核程序和用户程序

    一.概念 首先我们先来复习一下操作系统的概念和作用 操作系统是用户和硬件之间的一层媒介程序,为上提供编程接口,为下调用资源,管理驱动,以使用硬件. 从以上的表述我们可以看出OS的两点作用,第一个是对下 ...

  6. python 得到变量名的结果为名的变量的值locals()

    >>> a="1">>> b="a">>> print(a,b)1 a>>> print ...

  7. MQTT 1——物联网集成项目技术选型与说明

    最近做的JAVA项目与物联网设备有集成,记录一下从技术选型到实现,整合: 1.通信协议技术选型,MQTT技术介绍2.MQTT服务端安装,客户端测试3.MQTT客户端与Spring MVC整合 1.项目 ...

  8. Java基础00-Java概述1

    1. Java语言发展史 1.1 Java语言 语言:人与人交流沟通的表达方式 计算机语言:人与计算机之间进行信息交流沟通的一种特殊语言 Java语言是美国Sun公司(Stanford Univers ...

  9. Quick BI的复杂系统为例:那些年,我们一起做过的性能优化

    背景 一直以来,性能都是技术层面不可避开的话题,尤其在中大型复杂项目中.犹如汽车整车性能,追求极速的同时,还要保障舒适性和实用性,而在汽车制造的每个环节.零件整合情况.发动机调校等等,都会最终影响用户 ...

  10. java并发编程基础——线程的创建

    一.基础概念 1.进程和线程 进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1--n个线程.(进程是资源分配的最小单位) 线程:同一类线程共享代码和数据 ...