1. toml依赖
nacos_rust_client = "0.3"
local_ipaddress = "0.1"
2. 代码
//todo 维护实时服务列表,用来在请求到来时选择转发至具体的服务
use std::sync::Arc;
use log::debug;
use nacos_rust_client::client::{
naming_client::{
Instance, InstanceDefaultListener, QueryInstanceListParams, ServiceInstanceKey,
},
ClientBuilder,
};
use super::config::Config;
pub async fn init_nacos() {
let server_name = Config::global().server_name();
let server_port = Config::global().server_port();
let nacos_ip = Config::global().nacos_ip();
let nacos_port = Config::global().nacos_port();
let nacos_group_name = Config::global().nacos_group();
//这里小小的吐槽一下rnacos的大神,希望能把这个u32的端口号类型更新成跟其他工具一样,我看作者说用的actix-web,我试过actix,它的端口号也是u16的哈
connect(&server_name, server_port as u32, &nacos_ip, nacos_port as u32, &nacos_group_name);
add_service_listinner(&server_name, &nacos_group_name).await;
}
//注册
pub fn connect(service_name: &str, service_port: u32, nacos_ip: &str, nacos_port: u32 ,nacos_group_name: &str) {
let nacos_addr = &format!("{}:{}", nacos_ip, nacos_port);
ClientBuilder::new()
.set_endpoint_addrs(nacos_addr)
.set_use_grpc(true)//使用grpc,不用可以设置为false
.build_naming_client();
let ip = local_ipaddress::get().unwrap();
let instance = Instance::new_simple(&ip, service_port, service_name, nacos_group_name);
let naming_client = nacos_rust_client::get_last_naming_client().unwrap();
//向nacos注册
naming_client.register(instance);
}
//获取服务列表
pub async fn get_service_list(service_name: &str, group_name: &str) {
let naming_client = nacos_rust_client::get_last_naming_client().unwrap();
let query = QueryInstanceListParams::new_simple(service_name, group_name);
let instance_list = naming_client.query_instances(query).await;
debug!("instance list is : {:?}", instance_list);
}
//监听服务上下线的变化
pub async fn add_service_listinner(service_name: &str, group_name: &str) {
let naming_client = nacos_rust_client::get_last_naming_client().unwrap();
let servcie_key = ServiceInstanceKey::new(service_name, group_name);
let default_listener = InstanceDefaultListener::new(
*Box::new(servcie_key),
Some(Arc::new(|instances, add_list, remove_list| {
let service_name = if instances.len() > 0 {
&instances[0].service_name
} else if add_list.len() > 0 {
&add_list[0].service_name
} else {
&remove_list[0].service_name
};
let service_name: Vec<&str> = service_name.split("@@").collect();
debug!(
"{} instances changed, list:{:?},add count:{},remove count:{}",
&service_name[1],
instances,
add_list.len(),
remove_list.len()
);
})),
);
naming_client
.subscribe(Box::new(default_listener))
.await
.unwrap();
}
//向注册中心请求一个已注册服务
pub async fn select_service(service_name: &str, group_name: &str) -> String {
let query = QueryInstanceListParams::new_simple(service_name, group_name);
let naming_client = nacos_rust_client::get_last_naming_client().unwrap();
match naming_client.select_instance(query).await {
Ok(instances) => {
let target_address = format!("{}:{}", &instances.ip, &instances.port);
debug!("select instance {}", &target_address);
target_address
}
Err(e) => {
debug!("select_instance error {:?}", &e);
"".to_string()
}
}
}
标签:name,service,nacos,client,let,naming,rust
From: https://www.cnblogs.com/jiajie6591/p/18644851