You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
2.7 KiB
58 lines
2.7 KiB
gRPC https://grpc.org.cn/docs/guides/cancellation/
|
|
|
|
|
|
go get google.golang.org/grpc
|
|
go get google.golang.org/protobuf/cmd/protoc-gen-go
|
|
go get google.golang.org/grpc/cmd/protoc-gen-go-grpc
|
|
|
|
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
|
|
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
|
|
|
|
生成代码出错,问题解决:
|
|
查找
|
|
go list -m all | findstr grpc
|
|
go list -m all | findstr protobuf
|
|
|
|
移除不必要的依赖
|
|
go get -u github.com/gogo/protobuf@nonego get -u github.com/golang/protobuf@none
|
|
go get -u github.com/matttproud/golang_protobuf_extensions@none
|
|
|
|
更新 go.mod
|
|
go mod tidy
|
|
|
|
重新生成代码,打开命令行执行以下命令:
|
|
cd et_rpc
|
|
protoc --proto_path=proto --go_out=./pb --go_opt=paths=source_relative --go-grpc_out=./pb --go-grpc_opt=paths=source_relative proto/*.proto
|
|
|
|
==============================================================
|
|
|
|
1. 使用 Protobuf 编译器 protoc 生成 Go 代码
|
|
基本的 Protobuf 编译指令 protoc --proto_path=IMPORT_PATH --go_out=OUTPUT_PATH --go_opt=paths=source_relative your_proto_file.proto
|
|
参数说明
|
|
--proto_path=IMPORT_PATH:指定 .proto 文件的搜索路径。可以使用多个 --proto_path 选项来指定多个路径。
|
|
--go_out=OUTPUT_PATH:指定生成的 Go 代码的输出路径。可以使用 . 表示当前目录。
|
|
--go_opt=paths=source_relative:这个选项使生成的 Go 文件的包路径与 .proto 文件的相对路径一致,通常推荐使用。
|
|
your_proto_file.proto:要编译的 .proto 文件的名称。
|
|
|
|
***** ET-GO 系统的编译指令(cd 致 et-go/et_rpc 下执行)
|
|
1) 只生成消息类型的Go代码、序列化和反序列化方法、其他辅助方法:
|
|
protoc --proto_path=proto --go_out=./pb --go_opt=paths=source_relative proto/*.proto
|
|
2)生成消息类型Go代码、序列化和反序列化方法、gRPC服务的Go代码、gRPC方法的实现:
|
|
protoc --proto_path=proto --go_out=./pb --go_opt=paths=source_relative --go-grpc_out=./pb --go-grpc_opt=paths=source_relative proto/*.proto
|
|
|
|
参数说明
|
|
--proto_path=proto:指定 .proto 文件的搜索路径为 proto 目录。
|
|
--go_out=./pb:指定生成的 Go 代码输出到 pb 目录。
|
|
--go_opt=paths=source_relative:确保生成的 Go 文件的包路径与 .proto 文件的相对路径一致。
|
|
proto/*.proto:使用通配符 *.proto 来编译 proto 目录下的所有 .proto 文件。
|
|
|
|
/et-go
|
|
├── et_rpc
|
|
│ ├── pb # 存放生成的 Go 文件
|
|
│ │ └── iota_data.pb.go # 生成的 Go 文件
|
|
│ └── proto # 存放 Protobuf 文件
|
|
│ └── iota_data.proto # IotaData Protobuf 文件
|
|
└── 其他
|
|
|
|
|
|
|
|
|