rust实现的nacos客户端。
使用 actix + tokio 实现
toml
[dependencies]
nacos_rust_client = "0.1"
运行下面的例子,需要先启动nacos服务,把例子中的host改成实际的地址。
例子完整依赖与代码可以参考 examples/下的代码。
```rust use nacosrustclient::client::{ HostInfo }; use nacosrustclient::client::config_client::{ ConfigClient,ConfigKey,ConfigListener,ConfigDefaultListener };
async fn main() { let host = HostInfo::parse("127.0.0.1:8848"); let mut configclient = ConfigClient::new(host,String::new()); let key = ConfigKey::new("001","foo",""); //设置 configclient.setconfig(&key, "1234").await.unwrap(); //获取 let v=configclient.get_config(&key).await.unwrap(); println!("{:?},{}",&key,v);
let key = ConfigKey::new("003","foo","");
let c = Box::new(ConfigDefaultListener::new(key.clone(),Arc::new(|s|{
//字符串反序列化为对象,如:serde_json::from_str::<T>(s)
Some(s.to_owned())
})));
config_client.set_config(&key,"1234").await.unwrap();
//监听
config_client.subscribe(c.clone()).await;
//从监听对象中获取
println!("003 value:{:?}",c.get_value());
tokio::signal::ctrl_c().await.expect("failed to listen for event");
} ```
```rust use std::sync::Arc; use std::io::{Read, stdin};
use std::time::Duration;
use nacosrustclient::client::{HostInfo, naming_client::{NamingClient, Instance,QueryInstanceListParams}};
async fn main(){ std::env::setvar("RUSTLOG","INFO"); envlogger::init(); let host = HostInfo::parse("127.0.0.1:8848"); let client = NamingClient::new(host,"".toowned());
let ip = local_ipaddress::get().unwrap();
for i in 0..10{
let port=10000+i;
let instance = Instance::new(&ip,port,"foo","","","",None);
//注册
client.register(instance);
}
//tokio::spawn(async{query_params2().await.unwrap();});
let client2 = client.clone();
tokio::spawn(
async move {
query_params(client2.clone()).await;
}
);
//let mut buf = vec![0u8;1];
//stdin().read(&mut buf).unwrap();
tokio::signal::ctrl_c().await.expect("failed to listen for event");
println!("n:{}",&client.namespace_id);
}
async fn queryparams(client:Arc
```