nacos-sdk-rust

Nacos client in Rust

Proposal

https://github.com/alibaba/nacos/issues/8443#issuecomment-1248227587

Quickstart

Add Dependency

Add the dependency in Cargo.toml:

toml [dependencies] nacos-sdk = { version = "0.1", features = ["default"] }

Usage of Config

```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),
}

```

开发说明

主要依赖包

在 nacos-sdk-rust 工程里,为主要功能的实现,将会引入以下依赖包。

Tip:Rust 入门推荐 Rust语言圣经(Rust Course)

简要描述 client & server 的交互

请关注 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 的实现。

Config 配置管理模块

Naming 服务注册模块

Common 通用能力

License

Apache License Version 2.0