安装
-
安装protobuf 进入网站github.com/protocolbuffers/protobuf的release页面,点击下载protoc-3.10.1-linux-x86_64.zip文件
-
安装protoc-gen-go和grpc
使用gRPC发送rpc消息的时候,这些消息在http2上,究竟是怎样编码的。
注意,wireshark中,50051端口默认不会被识别为http2,所以需要手动设置“解码为HTTP/2” * 选中某个报文,右键->解码为->将TCP port:50051的当前值改为HTTP2
- gRPC:支持多语言编程、基于HTTP/2通讯的中间件
+----------------+
.---<-Proto Request--|-+------------+ |
+--------------/-+ |-> gRPC Stub | |
| +-----------v+ | / +------------+ |
| | gRPC Server|-|->Proto Response>-`| Ruby Client |
| +-----------\+<-- +----------------+
| \ | \
| c++ Service \| \ +-------------------+
+----------------\ -<ProtoRequest--|-+------------+ |
------------------>->| gRPC Stub | |
ProtoResponse | +------------+ |
|Android-Java Client|
+-------------------+
- Protocol Buffers编码:消息结构
Message Struct
Field1 | Field2 | Field3 |
{Tag}{Value} |{Tag}{Value}|{Tag}{Value}|{} {} {}
^ | | |
|
(field_number<<3)|wire_type
-
Wire Type
Type Meaning Used For 0 Varint int32,int64,uint32,uint64,sint32,sint64,bool,enum 1 64-bit fixed64,sfixed64,double 2 Length-delimited string, bytes, embeded messages,packed repeated fields 3 Start group groups(deprecated) 4 End group groups(deprecated) 5 32-bit fixed32,sfixed32,float -
举例
Protocol Buffers
+------------------------------------------------------------+
field tag=1 type2(string) | length6 M a r t i n |
+-+-+-+-+-+-+-+-+ | +--+ +--+ +--+--+--+--+--+--+ |
|0 0 0 0 1|0 1 0|---------->|0a| |06| |4d 61 72 74 69 6e| |
+-+-+-+-+-+-+-+-+ 0a | +--+ +--+ +--+--+--+--+--+--+ |
| 1和0都表示序号,可以发现序号是逆序的 |
field tag=2 type0(varint) | / / |
+-+-+-+-+-+-+-+-+ | +--+ +-----+ +-+-------------+ +-+-------------+ |
|0 0 0 1 0|0 0 0|---------->|10| |b9 0a|<-------|1|0 1 1 1 0 0 1| |0|0 0 0 1 0 1 0| |
+-+-+-+-+-+-+-+-+ 10 | +--+ +-----+ +-+-------------+ +-+-------------+ |
| |
field tag=3 type2(string) | length11 d a y d r e a m i n g |
+-+-+-+-+-+-+-+-+ | +--+ +--+ +--+--+--+--+--+--+--+--+--+--+--+ |
|0 0 0 1 1|0 1 0|---------->|1a| |0b| |64 61 79 64 72 65 61 6d 69 6e 67| |
+-+-+-+-+-+-+-+-+ 1a | +--+ +--+ +--+--+--+--+--+--+--+--+--+--+--+ |
+------------------------------------------------------------+
grpc的使用
- package名必须和微服务名保持一致, grpc在调用的时候,路由规则是
/package_name.service_name/method_name/
syntax = "proto3";
option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
本文发表于 0001-01-01,最后修改于 0001-01-01。
本站永久域名「 jiavvc.top 」,也可搜索「 后浪笔记一零二四 」找到我。