34个版本
0.8.1 | 2023年8月29日 |
---|---|
0.8.0 | 2023年6月19日 |
0.7.1 | 2022年9月14日 |
0.6.1 | 2022年5月11日 |
0.1.0 | 2019年9月23日 |
#1 in #low-memory
每月下载量36,052
在 5 个crate中使用 (3个直接使用)
175KB
4.5K SLoC
ttrpc-rust
ttrpc-rust是containerd的一个 非核心 子项目
ttrpc-rust
是 ttrpc 的Rust版本。 ttrpc 是专为低内存环境设计的GRPC。
ttrpc-rust
的 ttrpc_rust_plugin
编译器是基于 gRPC-rs 的gRPC编译器 grpcio-compiler 进行修改的。
使用方法
1. 使用 protoc
命令生成
从proto文件生成源代码
-
从github.com/protocolbuffers/protobuf安装protoc
-
安装protobuf-codegen
cargo install --force protobuf-codegen
- 从ttrpc-rust/compiler安装ttrpc_rust_plugin
cd ttrpc-rust/compiler
cargo install --force --path .
- 生成源代码
$ protoc --rust_out=. --ttrpc_out=. --plugin=protoc-gen-ttrpc=`which ttrpc_rust_plugin` example.proto
2. 编程生成
API用于生成例如在build.rs中使用的.rs文件
示例代码
fn main() {
protoc_rust_ttrpc::Codegen::new()
.out_dir("protocols")
.inputs(&[
"protocols/protos/agent.proto",
])
.include("protocols/protos")
.rust_protobuf() // also generate protobuf messages, not just services
.run()
.expect("Codegen failed.");
}
async/.await
ttrpc-rust支持async/.await。通过使用async/.await,您可以减少由线程引起的开销和资源消耗。
使用方法
1. 生成异步版本的代码
目前我们只支持使用ttrpc-codegen生成异步代码
ttrpc_codegen::Codegen::new()
.out_dir("protocols/asynchronous")
.inputs(&protos)
.include("protocols/protos")
.rust_protobuf()
.customize(Customize {
async_all: true, // It's the key option.
..Default::default()
})
.run()
.expect("Gen async codes failed.");
提供自定义选项
async_all
: 为服务器和客户端生成异步代码async_server
: 为服务器生成异步代码async_client
: 为客户端生成异步代码
更多信息请参考
example/build.rs
2. 使用async/.await的方式编写您的实现
请遵循 example/async-server.rs
和 example/async-client.rs
中的指南
运行示例
-
进入目录
$ cd ttrpc-rust/example
-
启动服务器
$ cargo run --example server
或
$ cargo run --example async-server
-
启动客户端
$ cargo run --example client
或
$ cargo run --example async-client
注意:protobuf的版本
protobuf-codegen、ttrpc_rust_plugin 和您的代码应使用相同的 protobuf 版本。如果您使用不同版本的 protobuf,将会遇到以下错误。
27 | const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_8_0;
| ^^^^^^^^^^^^^ help: a constant with a similar name exists: `VERSION_2_10_1`
原因是 由 protobuf-codegen 生成的文件仅与同一版本的运行时兼容
为了解决这个问题
- 使用新的 protobuf 重新构建 protobuf-codegen
cd grpc-rs
cargo clean
cargo update
cargo install --force protobuf-codegen
- 使用新的 protobuf 重新构建 ttrpc_rust_plugin
cd ttrpc-rust/compiler
cargo clean
cargo update
cargo install --force --path .
- 构建您的项目。
依赖项
~3–17MB
~193K SLoC