rust实现的nacos客户端。
目前暂时只支持1.x
版http协议,2.x
服务兼容1.x
协议。
使用 actix + tokio 实现
toml
[dependencies]
nacos_rust_client = "0.1"
运行下面的例子,需要先启动nacos服务,把例子中的host改成实际的地址。
例子完整依赖与代码可以参考 examples/下的代码。
```rust use std::time::Duration; use std::sync::Arc;
use nacosrustclient::client::{ HostInfo, AuthInfo }; use nacosrustclient::client::configclient::{ ConfigClient,ConfigKey,ConfigDefaultListener }; use serde::{Serialize,Deserialize}; use serdejson;
pub struct Foo { pub name: String, pub number: u64, }
async fn main() { std::env::setvar("RUSTLOG","INFO"); envlogger::init(); //let host = HostInfo::parse("127.0.0.1:8848"); //let configclient = ConfigClient::new(host,String::new()); let tenant = "public".toowned(); //default teant //let authinfo = Some(AuthInfo::new("nacos","nacos")); let authinfo = None; let configclient = ConfigClient::newwithaddrs("127.0.0.1:8848,127.0.0.1:8848",tenant,authinfo); tokio::time::sleep(Duration::frommillis(1000)).await;
let key = ConfigKey::new("001","foo","");
//设置
config_client.set_config(&key, "1234").await.unwrap();
//获取
let v=config_client.get_config(&key).await.unwrap();
println!("{:?},{}",&key,v);
let mut foo_obj= Foo {
name:"foo name".to_owned(),
number:0u64,
};
let key = ConfigKey::new("foo_config","foo","");
let foo_config_obj_listener = Box::new(ConfigDefaultListener::new(key.clone(),Arc::new(|s|{
//字符串反序列化为对象,如:serde_json::from_str::<T>(s)
Some(serde_json::from_str::<Foo>(s).unwrap())
})));
let foo_config_string_listener = Box::new(ConfigDefaultListener::new(key.clone(),Arc::new(|s|{
//字符串反序列化为对象,如:serde_json::from_str::<T>(s)
Some(s.to_owned())
})));
config_client.set_config(&key,&serde_json::to_string(&foo_obj).unwrap()).await.unwrap();
//监听
config_client.subscribe(foo_config_obj_listener.clone()).await;
config_client.subscribe(foo_config_string_listener.clone()).await;
//从监听对象中获取
println!("key:{:?} ,value:{:?}",&key.data_id,foo_config_string_listener.get_value());
for i in 1..10 {
foo_obj.number=i;
let foo_json_string = serde_json::to_string(&foo_obj).unwrap();
config_client.set_config(&key,&foo_json_string).await.unwrap();
// 配置推送到服务端后, 监听更新需要一点时间
tokio::time::sleep(Duration::from_millis(1000)).await;
let foo_obj_from_listener = foo_config_obj_listener.get_value().unwrap();
let foo_obj_string_from_listener = foo_config_string_listener.get_value().unwrap();
// 监听项的内容有变更后会被服务端推送,监听项会自动更新为最新的配置
println!("foo_obj_from_listener :{}",&foo_obj_string_from_listener);
assert_eq!(foo_obj_string_from_listener.to_string(),foo_json_string);
assert_eq!(foo_obj_from_listener.number,foo_obj.number);
assert_eq!(foo_obj_from_listener.number,i);
}
} ```
```rust use nacosrustclient::client::naming_client::{ServiceInstanceKey, InstanceDefaultListener}; use std::sync::Arc;
use std::time::Duration;
use nacosrustclient::client::{HostInfo, AuthInfo, naming_client::{NamingClient, Instance,QueryInstanceListParams}};
async fn main(){ //std::env::setvar("RUSTLOG","INFO"); std::env::setvar("RUSTLOG","INFO"); envlogger::init(); //let host = HostInfo::parse("127.0.0.1:8848"); //let client = NamingClient::new(host,"".toowned()); let namespaceid = "public".toowned(); //default teant //let authinfo = Some(AuthInfo::new("nacos","nacos")); let authinfo = None; let client = NamingClient::newwithaddrs("127.0.0.1:8848,127.0.0.1:8848", namespaceid, authinfo); let servciekey = ServiceInstanceKey::new("foo","DEFAULTGROUP"); //可以通过监听器获取指定服务的最新实现列表,并支持触发变更回调函数,可用于适配微服务地址选择器。 let defaultlistener = InstanceDefaultListener::new(servciekey,Some(Arc::new( |instances,addlist,removelist| { println!("service instances change,count:{},add count:{},remove count:{}",instances.len(),addlist.len(),removelist.len()); }))); client.subscribe(Box::new(defaultlistener.clone())).await.unwrap(); let ip = localipaddress::get().unwrap(); let servicename = "foo"; let groupname="DEFAULTGROUP"; for i in 0..10{ let port=10000+i; let instance = Instance::newsimple(&ip,port,servicename,groupname); //注册 client.register(instance); tokio::time::sleep(Duration::from_millis(1000)).await; }
//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
```