目录
01 protobuf语法回顾
## 编译 protobuf > 回顾:C++ 编译 命令: > > protoc --cpp_out=./ *.proto ---> xxx.pb.cc 和 xxx.pb.h 文件 - go 语言中 编译命令: `protoc --go_out=./ *proto` ---> xxx.pb.go 文件。 ## 添加 rpc 服务 - 语法: ```protobuf service 服务名 { rpc 函数名(参数:消息体) returns (返回值:消息) } message People { string name = 1; } message Student { int32 age = 2; } 例: service hello { rpc HelloWorld(People) returns (Student); } ```
02 protobuf的编译、和其他序列化比较
- 知识点: - 默认,protobuf,编译期间,不编译服务。 要想使之编译。 需要使用 gRPC。 - 使用的编译指令为: - `protoc --go_out=plugins=grpc:./ *.proto` - 生成的 xxx.pb.go 文件 与 我们自己封装的 rpc 对比: ```go 客户端: type bj38Client struct {} ----- type MyClient struct {} 类 func NewBj38Client() ----- InitCient() 函数 func (c *bj38Client) Say() ---- HelloWorld() 方法 服务端: type Bj38Server interface {} ---- type MyInterface interface{} 接口。 func RegisterBj38Server() ---- func RegisterService() 函数。
03 查看protoc编译文件对比自定义封装
## 作业:grpc 远程调用。 - 服务端 grpc 1. 初始一个 grpc 对象 2. 注册服务 3. 设置监听, 指定 IP、port 4. 启动服务。---- serve()
04 grpc安装简介
- 客户端 grpc 1. 连接 grpc 服务 2. 初始化 grpc 客户端 3. 调用远程服务。