作者:强毛 | 来源:互联网 | 2023-05-18 21:05
1.google的protobuf是什么?用于rpc的自定义协议,体积更小,序列化和反序列化的第三方库,和apachethrift是同一种技术。2.rpc库的介绍?(1)RMIre
1.google的protobuf是什么?
用于rpc的自定义协议,体积更小,序列化和反序列化的第三方库,和apache thrift是同一种技术。
2.rpc库的介绍?
(1) RMI remote method invocation
广泛用于EJB,实际上是一种跨机器的调用,通过网络传输,调用方A调用序列化字节码传输到B机器反序列化,调用B的方法,B回传结果后序列化网路传输,A反序列化成最终结果。
限制 : 只针对于Java语言。
特点 :网络传输代码自动生成 client --> stub (桩) , server --->skeleton(骨架)
传输机制:序列化和反序列化 也叫做 :编码与解码
(2)RPC remote procedure call 远程过程调用
特点: 服务端和客户端代码可跨语言 实现
编写模式 :
[1]定义一个接口说明文件:描述了一个对象(结构体),对象成员,接口方法等一系列信息。
[2]通过RPC框架所提供的编译器,将接口说明文件编译成具体语言文件。
[3]客户端和服务器端分别引入编译器所生成的文件,即可像调用本地方法一样调用远程方法。
RPC效率决定因素:编解码速度,数据压缩程度 ,基于Socket效率高 (webservice采用http性能不如Socket)
3.ProtoBuf介绍?
全称:Protocol Buffers
支持的语言越多,拥有的数据类型越少,因为数据类型必须是所有语言的交集。
message消息定义:
1 message Person {
2 required string name = 1; -- 1,2,3是表示参数的顺序而不是默认值
3 required int32 id = 2;
4 optional string email = 3;
5 }
编码-序列化:
1 Person john = Person.newBuilder()
2 .setId(1234)
3 .setName("John Doe")
4 .setEmail("jdoe@example.com")
5 .build();
6 output = new FileOutputStream(args[0]);
7 john.writeTo(output);
解码-反序列化:
//C++语言解码
Person john = Person.newBuilder()
.setId(1234)
.setName("John Doe")
.setEmail("jdoe@example.com")
.build();
output = new FileOutputStream(args[0]);
john.writeTo(output);
netty中使用的protobuf的gradle依赖:
1 group ‘com.netty‘
2 version ‘1.0-SNAPSHOT‘
3
4 apply plugin: ‘java‘
5
6 sourceCompatibility = 1.8
7 targetCompatibility = 1.8
8
9 repositories {
10 mavenCentral() //中央库
11 }
12
13 dependencies {
14 // testCompile group: ‘junit‘, name: ‘junit‘, version: ‘4.12‘
15 // testCompile("junit:junit:4.12")
16
17 //netty全部jar包
18 compile(
19 "io.netty:netty-all:4.1.10.Final",
20 //proobuf的包
21 "com.google.protobuf:protobuf-parent:3.3.1",
22 "com.google.protobuf:protobuf-java-util:3.3.1"
23 )
24
25 }
tips:google protobuf 的升级版 gRPC
Google 的Protobuf 的使用方式(序列化和反序列化工具-编解码)