#grpc #compiler #protobuf #grpc-rs

protoc-grpcio

API 用于程序化调用 grpcio (grpc-rs) gRPC 编译器

13 个版本 (7 个稳定)

使用旧的 Rust 2015

3.0.0 2020年12月9日
2.0.0 2020年6月22日
1.3.0 2020年6月17日
1.1.0 2019年9月26日
0.1.0 2018年1月25日

1674网络编程

Download history 1447/week @ 2024-03-14 1520/week @ 2024-03-21 1876/week @ 2024-03-28 1348/week @ 2024-04-04 1376/week @ 2024-04-11 1395/week @ 2024-04-18 953/week @ 2024-04-25 1023/week @ 2024-05-02 1323/week @ 2024-05-09 1268/week @ 2024-05-16 1072/week @ 2024-05-23 1262/week @ 2024-05-30 1107/week @ 2024-06-06 1088/week @ 2024-06-13 935/week @ 2024-06-20 565/week @ 2024-06-27

每月下载量 3,966
用于 12 个 Crates(直接使用 8 个)

Apache-2.0

16KB
250

protoc-grpcio

crates.io Build Status License

访问 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