0.Protobuf简介
Protobuf是用来将对象序列化的,相类似的技术还有Json序列化等等。它是一种高效的结构化数据存储格式,可以用于结构化数据串行化(序列化)。它很适合做数据存储或RPC(远程过程调用)数据交换格式。目前很多公司 http+json → tcp+protobuf
- Protobuf 是以 message 的方式来管理数据的
- 支持跨平台、跨语言,即[客户端和服务器端可以是不同的语言编写的] (支持目前绝大多数语言,例如 C++、C#、Java、python 等
- 高性能,高可靠性
- 使用 protobuf 编译器能自动生成代码,Protobuf 是将类的定义使用.proto 文件进行描述。说明,在idea 中编写 .proto 文件时,会自动提示是否下载 .ptotot 编写插件. 可以让语法高亮。
- 然后通过 protoc.exe 编译器根据.proto 自动生成.java 文件
1.实例
1.1 导包
<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.6.1</version>
</dependency>
1.2 下载protoc.exe
下载解压后bin找到protoc.exe
1.3 创建Student.proto文件
syntax = "proto3"; //版本
option java_outer_classname = "StudentPOJO"; //生成的外部类名,同时也是文件名
message Student { //会在StudentPojo 外部类生成一个内部类Student,他是真正发送的pojo对象
int32 id = 1; //Student类中有一个属性名字为ID,类型为int32(protobuf类型),1表示序号,不是值
string name = 2;
}
1.4 使用protoc.exe生成Java文件
将Student.proto放在protoc.exe同文件夹下使用命令行执行以下
protoc.exe --java_out=. Student.proto
即可生成StudentPOJO.java之后放入程序中使用
1.5 修改原来的服务端和客户端
1.5.1 客户端Handler修改channelActive
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// 发送一个Student对象到服务器
StudentPOJO.Student student = StudentPOJO.Student.newBuilder().setId(10).setName("jumper").build();
ctx.writeAndFlush(student);
}
1.5.2 服务端Handler修改channelRead
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 读取从客户端发送的StudentPOJO.Student
StudentPOJO.Student student = (StudentPOJO.Student) msg;
System.out.println("客户端发送的数据 ID = " + student.getId() + "名字" + student.getName());
}
1.5.3 客户端的initChannel
protected void initChannel(SocketChannel socketChannel) throws Exception {
// 在pipeline中加入ProtoBufEncoder
socketChannel.pipeline().addLast("encoder",new ProtobufEncoder());
socketChannel.pipeline().addLast(new NettyClientHandler()); //加入自己的处理器
}
1.5.4 服务端的initChannel
protected void initChannel(SocketChannel socketChannel) throws Exception {
// 在pipeline加入ProtoBufDecoder
// 指定对哪种对象进行解码
socketChannel.pipeline().addLast("decoder",new ProtobufDecoder(StudentPOJO.Student.getDefaultInstance()));
socketChannel.pipeline().addLast(new NettyServerHandler());
}
1.6 测试
先后启动服务端和客户端
结果如下: