11个不稳定版本
0.6.0 | 2021年11月8日 |
---|---|
0.5.0 | 2020年9月10日 |
0.4.4 | 2020年8月14日 |
0.4.3 | 2020年4月13日 |
0.3.1 | 2019年10月7日 |
#943 in 异步
404每月下载次数
150KB
3K SLoC
Rants
一个为Rust编程语言设计的异步NATS客户端库。
客户端旨在成为NATS客户端协议的一个易于使用且轻薄的包装器。学习使用客户端的最简单方法是阅读NATS客户端协议文档。库API的主要入口点是Client
结构体。
TLS支持可以通过启用native-tls
功能的native-tls
crate或通过启用rustls-tls
功能的rustls
crate来实现。
示例
use futures::stream::StreamExt;
use rants::Client;
#[tokio::main]
async fn main() {
// A NATS server must be running on `127.0.0.1:4222`
let address = "127.0.0.1:4222".parse().unwrap();
let client = Client::new(vec![address]);
// Configure the client to receive messages even if it sent the message
client.connect_mut().await.echo(true);
// Connect to the server
client.connect().await;
// Create a new subject called "test"
let subject = "test".parse().unwrap();
// Subscribe to the "test" subject
let (_, mut subscription) = client.subscribe(&subject, 1024).await.unwrap();
// Publish a message to the "test" subject
client
.publish(&subject, b"This is a message!")
.await
.unwrap();
// Read a message from the subscription
let message = subscription.next().await.unwrap();
let message = String::from_utf8(message.into_payload()).unwrap();
println!("Received '{}'", message);
// Disconnect from the server
client.disconnect().await;
}
开发
集成测试套件需要NATS_PATH
环境变量指向NATS服务器可执行文件
> cargo test
在集成测试中使用env_logger
crate。要启用它并运行单个测试,请
> RUST_LOG=rants=trace cargo test ping_pong
替代方案
依赖项
~6–20MB
~281K SLoC