|
| 1 | +[English](README.md) | 中文 |
| 2 | + |
| 3 | +# trpc-cmdline |
| 4 | + |
| 5 | +[](https://pkg.go.dev/github.com/trpc.group/trpc-cmdline) |
| 6 | +[](https://goreportcard.com/report/github.com/trpc.group/trpc-go/trpc-cmdline) |
| 7 | +[](https://github.com/trpc.group/trpc-cmdline/blob/main/LICENSE) |
| 8 | +[](https://github.com/trpc.group/trpc-cmdline/releases) |
| 9 | +[](http://test.trpc.group.woa.com/docs/) |
| 10 | +[](https://github.com/trpc.group/trpc-cmdline/actions/workflows/prc.yaml) |
| 11 | +[](https://app.codecov.io/gh/trpc.group/trpc-cmdline/tree/main) |
| 12 | + |
| 13 | +trpc-cmdline 是 [trpc-cpp](https://github.com/trpc-group/trpc-cpp) 和 [trpc-go](https://github.com/trpc-group/trpc-go) 的命令行工具。 |
| 14 | + |
| 15 | +本项目支持 [Go](https://go.dev/doc/devel/release) 最新发布的三个版本。 |
| 16 | + |
| 17 | +## 安装 |
| 18 | + |
| 19 | +### 安装 trpc-cmdline |
| 20 | + |
| 21 | +#### 使用 go 命令进行安装 |
| 22 | + |
| 23 | +首先将以下内容添加到你的 `~/.gitconfig` 中: |
| 24 | + |
| 25 | +```bash |
| 26 | +[url "ssh://git@github.com/"] |
| 27 | + insteadOf = https://github.com/ |
| 28 | +``` |
| 29 | + |
| 30 | +然后执行以下命令以安装 `trpc-cmdline`: |
| 31 | + |
| 32 | +```bash |
| 33 | +go install trpc.group/trpc-go/trpc-cmdline/trpc@latest |
| 34 | +``` |
| 35 | + |
| 36 | +<!-- #### Install from release |
| 37 | +
|
| 38 | +<details><summary>Click to show the bash script</summary><br><pre> |
| 39 | +$ TAG="v0.0.1" # Choose tag. |
| 40 | +$ OS=linux # Choose from "linux", "darwin" or "windows". |
| 41 | +$ wget -O trpc https://github.com/trpc-group/trpc-cmdline/releases/download/${TAG}/trpc_${OS} |
| 42 | +$ mkdir -p ~/go/bin && chmod +x trpc && mv trpc ~/go/bin |
| 43 | +$ export PATH=~/go/bin:$PATH # Add this to your `~/.bashrc`. |
| 44 | +</pre></details> --> |
| 45 | + |
| 46 | +### 安装依赖 |
| 47 | + |
| 48 | + <!-- by using one of the following methods. |
| 49 | +
|
| 50 | +#### Using trpc setup |
| 51 | +
|
| 52 | +After installation of trpc-cmdline, simply running `trpc setup` will automatically install all the dependencies. |
| 53 | +
|
| 54 | +#### Install separately --> |
| 55 | + |
| 56 | +<details><summary>Install protoc </summary><br><pre> |
| 57 | +$ # Reference: https://grpc.io/docs/protoc-installation/ |
| 58 | +$ PB_REL="https://github.com/protocolbuffers/protobuf/releases" |
| 59 | +$ curl -LO $PB_REL/download/v3.15.8/protoc-3.15.8-linux-x86_64.zip |
| 60 | +$ unzip -o protoc-3.15.8-linux-x86_64.zip -d $HOME/.local |
| 61 | +$ export PATH=~/.local/bin:$PATH # Add this to your `~/.bashrc`. |
| 62 | +$ protoc --version |
| 63 | +libprotoc 3.15.8 |
| 64 | +</pre></details> |
| 65 | + |
| 66 | +<details><summary>Install flatc </summary><br><pre> |
| 67 | +$ # Reference: https://github.com/google/flatbuffers/releases |
| 68 | +$ wget https://github.com/google/flatbuffers/releases/download/v23.5.26/Linux.flatc.binary.g++-10.zip |
| 69 | +$ unzip -o Linux.flatc.binary.g++-10.zip -d $HOME/.bin |
| 70 | +$ export PATH=~/.bin:$PATH # Add this to your `~/.bashrc`. |
| 71 | +$ flatc --version |
| 72 | +flatc version 23.5.26 |
| 73 | +</pre></details> |
| 74 | + |
| 75 | +<details><summary>Install protoc-gen-go</summary><br><pre> |
| 76 | +$ # Reference: https://grpc.io/docs/languages/go/quickstart/ |
| 77 | +$ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest |
| 78 | +</pre></details> |
| 79 | + |
| 80 | +<details><summary>Install goimports</summary><br><pre> |
| 81 | +$ go install golang.org/x/tools/cmd/goimports@latest |
| 82 | +</pre></details> |
| 83 | + |
| 84 | +<details><summary>Install mockgen</summary><br><pre> |
| 85 | +$ # Reference: https://github.com/uber-go/mock |
| 86 | +$ go install go.uber.org/mock/mockgen@latest |
| 87 | +</pre></details> |
| 88 | + |
| 89 | + |
| 90 | +## 快速上手 |
| 91 | + |
| 92 | +### 生成完整项目 |
| 93 | + |
| 94 | +* 将以下内容复制到 `helloworld.proto`, 原始文件为 [./docs/helloworld/helloworld.proto](./docs/helloworld/helloworld.proto): |
| 95 | + |
| 96 | +```protobuf |
| 97 | +syntax = "proto3"; |
| 98 | +package helloworld; |
| 99 | +
|
| 100 | +option go_package = "github.com/some-repo/examples/helloworld"; |
| 101 | +
|
| 102 | +// HelloRequest is hello request. |
| 103 | +message HelloRequest { |
| 104 | + string msg = 1; |
| 105 | +} |
| 106 | +
|
| 107 | +// HelloResponse is hello response. |
| 108 | +message HelloResponse { |
| 109 | + string msg = 1; |
| 110 | +} |
| 111 | +
|
| 112 | +// HelloWorldService handles hello request and echo message. |
| 113 | +service HelloWorldService { |
| 114 | + // Hello says hello. |
| 115 | + rpc Hello(HelloRequest) returns(HelloResponse); |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +* 使用 trpc-cmdline 来生成完整项目: |
| 120 | +```go |
| 121 | +$ trpc create -p helloworld.proto -o out |
| 122 | +``` |
| 123 | + |
| 124 | +注意: `-p` 用于指定 proto 文件, `-o` 用于指定输出目录, |
| 125 | +更多 flag 信息可以运行 `trpc -h` 以及 `trpc create -h` 来进行查看。 |
| 126 | + |
| 127 | +* 进入输出目录,运行服务端: |
| 128 | +```bash |
| 129 | +$ cd out |
| 130 | +$ go run . |
| 131 | +... |
| 132 | +... trpc service:helloworld.HelloWorldService launch success, tcp:127.0.0.1:8000, serving ... |
| 133 | +... |
| 134 | +``` |
| 135 | + |
| 136 | +* 在另一个终端中进入输出目录,运行客户端: |
| 137 | +```bash |
| 138 | +$ go run cmd/client/main.go |
| 139 | +... simple rpc receive: |
| 140 | +``` |
| 141 | + |
| 142 | +注意: 由于生成的代码默认都是空操作,因此日志中显示的收到的数据内容也为空。 |
| 143 | + |
| 144 | +* 现在你可以尝试修改 `hello_world_service.go` 中的服务端代码以及 `cmd/client/main.go` 中的客户端代码来创建一个 echo 服务器。你可以参考 [https://github.com/trpc-group/trpc-go/tree/main/examples/helloworld](https://github.com/trpc-group/trpc-go/tree/main/examples/helloworld) 以获取灵感 |
| 145 | + |
| 146 | +* 生成文件的详细解释如下: |
| 147 | + |
| 148 | +```bash |
| 149 | +$ tree |
| 150 | +. |
| 151 | +|-- cmd |
| 152 | +| `-- client |
| 153 | +| `-- main.go # Generated client code. |
| 154 | +|-- go.mod |
| 155 | +|-- go.sum |
| 156 | +|-- hello_world_service.go # Generated server service implementation. |
| 157 | +|-- hello_world_service_test.go |
| 158 | +|-- main.go # Server entrypoint. |
| 159 | +|-- stub # Stub code. |
| 160 | +| `-- github.com |
| 161 | +| `-- some-repo |
| 162 | +| `-- examples |
| 163 | +| `-- helloworld |
| 164 | +| |-- go.mod |
| 165 | +| |-- helloworld.pb.go |
| 166 | +| |-- helloworld.proto |
| 167 | +| |-- helloworld.trpc.go |
| 168 | +| `-- helloworld_mock.go |
| 169 | +`-- trpc_go.yaml # Configuration file for trpc-go. |
| 170 | +``` |
| 171 | + |
| 172 | +### 仅生成桩代码 |
| 173 | + |
| 174 | +* 只需要添加 `--rpconly` 选项就可以只生成桩代码: |
| 175 | +```go |
| 176 | +$ trpc create -p helloworld.proto -o out --rpconly |
| 177 | +$ tree out |
| 178 | +out |
| 179 | +|-- go.mod |
| 180 | +|-- go.sum |
| 181 | +|-- helloworld.pb.go |
| 182 | +|-- helloworld.trpc.go |
| 183 | +`-- helloworld_mock.go |
| 184 | +``` |
| 185 | + |
| 186 | +### 常用的指令 |
| 187 | + |
| 188 | +下面列举了一些常用的命令行选项: |
| 189 | + |
| 190 | +* `-f`: 用于强制覆盖输出目录中的内容 |
| 191 | +* `-d some-dir`: 添加 proto 文件的查找路径(包括依赖的 proto 文件),可以指定多次 |
| 192 | +* `--mock=false`: 禁止生成 mock 代码 |
| 193 | +* `--nogomod=true`: 在生成桩代码时不生成 `go.mod` 文件,只在 `--rpconly=true` 的时候生效, 默认为 `false` |
| 194 | + |
| 195 | +更多命令行选项可以执行 `trpc -h` 以及 `trpc [subcmd] -h` 来进行查看。 |
| 196 | + |
| 197 | +### 更多功能 |
| 198 | + |
| 199 | +请查看 [文档](./docs/) |
| 200 | + |
| 201 | +## 贡献 |
| 202 | + |
| 203 | +本开源项目欢迎任何贡献,请阅读 [贡献指南](CONTRIBUTING.md) 以获取更多信息。 |
0 commit comments