13 个版本 (7 个稳定)
使用旧的 Rust 2015
3.0.0 | 2020年12月9日 |
---|---|
2.0.0 | 2020年6月22日 |
1.3.0 |
|
1.1.0 | 2019年9月26日 |
0.1.0 | 2018年1月25日 |
1674 在 网络编程
每月下载量 3,966
用于 12 个 Crates(直接使用 8 个)
16KB
250 行
protoc-grpcio
访问 grpc-rs 编译器 的程序化 API。
需求
- 您必须安装 Google 的 Protocol Buffer 编译器(
protoc
)并在PATH
中。
示例 build.rs
对于如下布局的项目
$ tree
.
├── build.rs
├── Cargo.toml
└── src
├── client.rs
├── protos
│ ├── example
│ │ └── diner.proto
│ └── mod.rs
└── server.rs
3 directories, 7 files
build.rs
可能看起来像这样
extern crate protoc_grpcio;
fn main() {
let proto_root = "src/protos";
println!("cargo:rerun-if-changed={}", proto_root);
protoc_grpcio::compile_grpc_protos(
&["example/diner.proto"],
&[proto_root],
&proto_root,
None
).expect("Failed to compile gRPC definitions!");
}
示例 Cargo.toml
而 Cargo.toml
可能看起来像这样
[package]
# ...
build = "build.rs"
[lib]
name = "protos"
path = "src/protos/mod.rs"
[[bin]]
name = "server"
path = "src/server.rs"
[[bin]]
name = "client"
path = "src/client.rs"
[dependencies]
futures = "0.1.16"
grpcio = "0.4.3"
protobuf = "~2"
[build-dependencies]
protoc-grpcio = "1.0.2"
您可以在 example/
下检查此示例,通过在一个会话中编译并运行示例服务器
cargo run --manifest-path example/Cargo.toml --bin server
...
Finished dev [unoptimized + debuginfo] target(s) in 27.97 secs
Running `example/target/debug/server`
listening on 127.0.0.1:34431
然后,在另一个会话中运行客户端
$ cargo run --manifest-path example/Cargo.toml --bin client 34431
...
Finished dev [unoptimized + debuginfo] target(s) in 1.28 secs
Running `example/target/debug/client 34431`
Ate items: SPAM items: EGGS and got charged $0.30
致谢
感谢 TiKV 项目开发者为 (grpc-rs) 和 Stepan Koltsov (@stepancheg, rust-protobuf) 做出的出色工作,他们为 Rust 带来了 Protocol Buffers 和 gRPC 支持。
依赖关系
~3–12MB
~150K SLoC