C++GO语言微服务基础技术②
目录
01 protobuf语法回顾
02 protobuf的编译、和其他序列化比较
03 查看protoc编译文件对比自定义封装
04 grpc安装简介
05 grpc服务远程调用作业布置
06 作业-grpc-server端
07 作业-grpc-client端
01 protobuf语法回顾
## 编译 protobuf> 回顾:C++ 编译 命令: > > protoc --cpp_out=./ *.proto ---> xxx.pb.cc 和 xxx.pb.h 文件- go 语言中 编译命令:`protoc --go_out=./ *proto` ---> xxx.pb.go 文件。## 添加 rpc 服务- 语法:```protobufservice 服务名 {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、port4. 启动服务。---- serve()
04 grpc安装简介
- 客户端 grpc1. 连接 grpc 服务2. 初始化 grpc 客户端3. 调用远程服务。