| Crates.io | tin-nacos-wrapper |
| lib.rs | tin-nacos-wrapper |
| version | 0.1.0 |
| created_at | 2025-11-24 03:34:05.808621+00 |
| updated_at | 2025-11-24 03:34:05.808621+00 |
| description | A Rust library for Nacos service discovery and configuration management |
| homepage | https://github.com/JiabinTang/tin-nacos-wrapper |
| repository | https://github.com/JiabinTang/tin-nacos-wrapper |
| max_upload_size | |
| id | 1947330 |
| size | 74,680 |
一个用于 Nacos 服务发现和配置管理的 Rust 库。该库提供了对 nacos-sdk 的高级封装,以简化 Nacos 集成到 Rust 应用程序中的过程。
NacosClient 和配置。在你的 Cargo.toml 中添加以下内容:
[dependencies]
tin-nacos-wrapper = "0.1.0"
使用 NacosConfig 定义你的 Nacos 配置:
use tin_nacos_wrapper::NacosConfig;
let config = NacosConfig {
server_addr: "127.0.0.1:8848".to_string(),
namespace: "public".to_string(),
app_name: "my-service".to_string(),
group_name: "DEFAULT_GROUP".to_string(),
client_ip: "127.0.0.1".to_string(),
client_port: 8080,
auth_username: Some("nacos".to_string()), // 可选
auth_password: Some("nacos".to_string()), // 可选
};
初始化 NacosClient。这将自动将当前服务实例注册到 Nacos。
use tin_nacos_wrapper::NacosClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 初始化客户端
let client = NacosClient::init(config).await?;
// ... 应用程序逻辑 ...
Ok(())
}
使用 RemoteServiceClient 发现服务并进行 HTTP 调用。
use tin_nacos_wrapper::RemoteServiceClient;
use serde_json::Value;
async fn call_other_service(client: &NacosClient) -> Result<(), Box<dyn std::error::Error>> {
// 获取 "target-service" 的一个健康实例地址 (ip:port)
let address = RemoteServiceClient::get_service_address(client, "target-service").await?;
// 构建 URL
let url = format!("http://{}/api/data", address);
// 发送 GET 请求
let response: Value = RemoteServiceClient::get_with_response(&url, None).await?;
println!("Response: {:?}", response);
Ok(())
}
你也可以手动注册实例或订阅服务。
// 注册另一个实例
client.register_instance(
"another-service".to_string(),
Some("DEFAULT_GROUP".to_string()),
"192.168.1.100".to_string(),
9090
).await?;
// 订阅服务
client.subscribe_service(
Some("DEFAULT_GROUP".to_string()),
"target-service".to_string()
).await?;
MIT