安装 trpc-cmdline
本文为自己的安装笔记,如需要详细安装信息请访问链接!!
本次需要用到 go、git,请自行安装!!
- 修改gitconfig问题
vim ~/.gitconfig 添加: [url "ssh://git@github.com/"] insteadOf = https://github.com/
- 执行命令安装
go install trpc.group/trpc-go/trpc-cmdline/trpc@latest
安装依赖
使用 trpc setup 一键安装所有依赖,只需要运行 trpc setup 便可安装所有依赖;
但是大部分应该都会出现问题;所以我们还是进行手动安装吧:
Install protoc
$ # Reference: https://grpc.io/docs/protoc-installation/
$ PB_REL="https://github.com/protocolbuffers/protobuf/releases"
$ curl -LO $PB_REL/download/v3.15.8/protoc-3.15.8-linux-x86_64.zip
$ unzip -o protoc-3.15.8-linux-x86_64.zip -d $HOME/.local
$ export PATH=~/.local/bin:$PATH # Add this to your `~/.bashrc`.
$ protoc --version
libprotoc 3.15.8
Install flatc
$ # Reference: https://github.com/google/flatbuffers/releases
$ wget https://github.com/google/flatbuffers/releases/download/v23.5.26/Linux.flatc.binary.g++-10.zip
$ unzip -o Linux.flatc.binary.g++-10.zip -d $HOME/.bin
$ vim ~/.bashrc
$ export PATH=~/.bin:$PATH # 将此命令写入 `~/.bashrc`.
$ source ~/.bashrc
$ flatc --version
flatc version 23.5.26
Install protoc-gen-go
$ # Reference: https://grpc.io/docs/languages/go/quickstart/
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
Install goimports
$ go install golang.org/x/tools/cmd/goimports@latest
Install mockgen
$ # Reference: https://github.com/uber-go/mock
$ go install go.uber.org/mock/mockgen@latest
Install protoc-gen-validate and protoc-gen-validate-go
$ # Please download the binaries in https://github.com/bufbuild/protoc-gen-validate/releases
$ # Or:
$ go install github.com/envoyproxy/protoc-gen-validate@latest
$ go install github.com/envoyproxy/protoc-gen-validate/cmd/protoc-gen-validate-go@latest
全部安装后调用 trpc setup 查看是否全部安装成功!
恭喜,快速搭建就已经成功了!!
创建PB文件
创建一个新的文件夹,写入helloworld.proto
syntax = "proto3";
package helloworld;
option go_package = "github.com/some-repo/examples/helloworld";
// HelloRequest is hello request.
message HelloRequest {
string msg = 1;
}
// HelloResponse is hello response.
message HelloResponse {
string msg = 1;
}
// HelloWorldService handles hello request and echo message.
service HelloWorldService {
// Hello says hello.
rpc Hello(HelloRequest) returns(HelloResponse);
}
执行快速构建
注意:-l指定language为cpp语言;
# 其中trpc为你在环境搭建中安装的trpc-cmdline工具,-l指定language为cpp语言,protofile为用于生成桩代码的proto文件
# 选择你的语言!!
trpc create -l cpp --protofile=helloworld.proto # cpp
trpc create -p helloworld.proto -o out # go
成功啦!!
注意:如果出现了报错,使用 trpc setup 查看环境是否搭建成功;
文件说明:
(编译和运行使用sudo进行)
build.sh 和 clean.sh表示构建和清理项目;
run_client.sh 和 run_server.sh表示启动客户端和服务端测试;
Server目录下的代码是此服务相关实现的代码;
server 为rpc服务注册实现
service 为rpc响应具体业务逻辑
Client目录下的代码是用于本地测试服务的客户端代码;
fiber_client.cc 为同步处理案例;
future_client.cc 为异步处理案例;
成功运行
注意事项
在安装 flatc 时必须将 export PATH=~/.bin:$PATH 写入.bashrc;