tin-nacos-wrapper 0.1.0

A Rust library for Nacos service discovery and configuration management
Documentation
# tin-nacos-wrapper

一个用于 Nacos 服务发现和配置管理的 Rust 库。该库提供了对 `nacos-sdk` 的高级封装,以简化 Nacos 集成到 Rust 应用程序中的过程。

## 功能特性

- **服务注册**:初始化时自动将服务实例注册到 Nacos。
- **服务发现**:订阅服务变更并发现健康实例。
- **配置管理**:访问 Nacos 配置服务。
- **远程服务调用**:用于向已发现的服务发送 HTTP 请求的工具。
- **简化的 API**:易于使用的 `NacosClient` 和配置。

## 安装

在你的 `Cargo.toml` 中添加以下内容:

```toml
[dependencies]
tin-nacos-wrapper = "0.1.0"
```

## 使用方法

### 1. 配置

使用 `NacosConfig` 定义你的 Nacos 配置:

```rust
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()), // 可选
};
```

### 2. 初始化

初始化 `NacosClient`。这将自动将当前服务实例注册到 Nacos。

```rust
use tin_nacos_wrapper::NacosClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 初始化客户端
    let client = NacosClient::init(config).await?;
    
    // ... 应用程序逻辑 ...

    Ok(())
}
```

### 3. 服务发现与远程调用

使用 `RemoteServiceClient` 发现服务并进行 HTTP 调用。

```rust
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(())
}
```

### 4. 手动注册与订阅

你也可以手动注册实例或订阅服务。

```rust
// 注册另一个实例
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