Nacos client in Rust
https://github.com/alibaba/nacos/issues/8443#issuecomment-1248227587
Add the dependency in Cargo.toml
:
toml
[dependencies]
nacos-sdk = { version = "0.1.0", features = ["default"] }
```rust let mut configservice = ConfigServiceBuilder::new( ClientProps::new() .serveraddr("0.0.0.0:9848") // Attention! "public" is "", it is recommended to customize the namespace with clear meaning. .namespace("") .appname("simpleapp"), ) .build() .await;
// example get a config
let config_resp = config_service.get_config("todo-data-id".to_string(), "todo-group".to_string());
match config_resp {
Ok(config_resp) => tracing::info!("get the config {}", config_resp),
Err(err) => tracing::error!("get the config {:?}", err),
}
// example add a listener
let _listen = config_service.add_listener(
"todo-data-id".to_string(),
"todo-group".to_string(),
Box::new(|config_resp| {
tracing::info!("listen the config={:?}", config_resp);
}),
);
match _listen {
Ok(_) => tracing::info!("listening the config success"),
Err(err) => tracing::error!("listen config error {:?}", err),
}
```
Build with cargo build
Note: The proto buf client generation is built into cargo build process so updating the proto files under proto/ is enough to update the proto buf client.
请 cargo fmt --all
格式化代码再提交
Rust 入门,还有太多东西不规范,仍需斟酌各种实现逻辑
-Dnacos.standalone=true
在 nacos-sdk-rust 工程里,为主要功能的实现,将会引入以下依赖包。
Tip:Rust 入门推荐 Rust语言圣经(Rust Course)
请关注 proto/nacos_grpc_service.proto
并知晓构建出客户端侧的 stub,实现同步调用 service Request.request()
,流式交互 service BiRequestStream.requestBiStream()
。
tikv/grpc-rs
创建与 Nacos-server 的 gRPC 双工长链接,serde/json
适配与 server 的交互序列化;
gRPC 交互的 Payload 和 Metadata 由 Protocol Buffers
序列化,具体的 Request/Response 实体 json 格式二进制数据维护于 Payload.body,类型名字符串维护于 Metadata.type 。
有了 gRPC 双工长链接,也有了数据序列化方式,那么就是对 Request/Response 的处理逻辑啦; 而 client 会接受 server 的主动调用,故可以实现一个通用的 RequestHandler 接受 server 的请求,根据 Request 类型分发到具体的处理实现并返回对应的 Response。
而 client 请求 server 的部分,则 do it ...
以上交互务必参考 java nacos-client 和 nacos-server 的实现。
NACOS_CLIENT_CONFIG_*
于配置管理, NACOS_CLIENT_NAMING_*
于服务注册tracing::info!()
opentelemetry