-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: 增加nacos_rust_client sdk 注册中心使用样例 #48
- Loading branch information
Showing
4 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# nacos_rust_client 说明 | ||
|
||
## 介绍 | ||
|
||
rust实现的nacos客户端sdk。 | ||
|
||
nacos_rust_client是在写[r-nacos](https://github.com/r-nacos/r-nacos) 过程中实现的客户端,方便服务端与客户端都相互验证,目前已功能已比较稳定,推荐使用。 | ||
|
||
0.3.x版本开始同时支持nacos `1.x`版http协议和`2.x`版本协议,支持在创建client时指定使用协议类型。 | ||
|
||
0.2.x版本只支持nacos `1.x`版http协议. | ||
|
||
0.3.x版本兼容0.2版本api,建议使用nacos_rust_client都升级到0.3.x版本。 | ||
|
||
特点: | ||
|
||
1. 使用 actix + tokio 实现。 | ||
2. 支持配置中心的推送、获取、监听。 | ||
3. 支持注册中心的服务实例注册(自动维护心跳)、服务实例获取(自动监听缓存实例列表)。 | ||
4. 创建的客户端后台处理,都放在同一个actix环境线程; 高性能,不会有线程膨胀,稳定可控。 | ||
|
||
|
||
## 样例运行说明 | ||
|
||
1. 运行sdk样例前,需要先在本地运行rnacos服务。默认使用`127.0.0.1:8848`服务地址,如果不一致,需要调整sdk中的nacos服务地址。 | ||
2. cd到样例目录,通过`cargo run`即可运行对应样例应用。 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
target/ |
19 changes: 19 additions & 0 deletions
19
sdk-examples/rust/nacos_rust_client/naming-register/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "naming-register" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[workspace] | ||
|
||
[dependencies] | ||
nacos_rust_client = "0.3" | ||
tokio = {version="1",features=["full"]} | ||
anyhow="1" | ||
serde = {version="1" ,features=["derive"]} | ||
serde_urlencoded = "0.6.1" | ||
serde_json="1" | ||
log="0" | ||
env_logger = "0.7" | ||
local_ipaddress = "0.1.3" |
95 changes: 95 additions & 0 deletions
95
sdk-examples/rust/nacos_rust_client/naming-register/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#![allow(unused_imports, unreachable_code)] | ||
/// nacos_rust_client 注册心样例 | ||
/// 包含注册、查询、监听服务实例功能样例 | ||
/// | ||
use nacos_rust_client::client::naming_client::{InstanceDefaultListener, ServiceInstanceKey}; | ||
use std::sync::Arc; | ||
|
||
use std::time::Duration; | ||
|
||
use nacos_rust_client::client::naming_client::{Instance, NamingClient, QueryInstanceListParams}; | ||
use nacos_rust_client::client::{AuthInfo, ClientBuilder, HostInfo}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
//std::env::set_var("RUST_LOG","INFO"); | ||
std::env::set_var("RUST_LOG", "INFO"); | ||
env_logger::init(); | ||
//let host = HostInfo::parse("127.0.0.1:8848"); | ||
//let client = NamingClient::new(host,"".to_owned()); | ||
let namespace_id = "public".to_owned(); //default teant | ||
//let auth_info = Some(AuthInfo::new("nacos","nacos")); | ||
let auth_info = None; | ||
//let client = NamingClient::new_with_addrs("127.0.0.1:8848,127.0.0.1:8848", namespace_id, auth_info); | ||
let client = ClientBuilder::new() | ||
.set_endpoint_addrs("127.0.0.1:8848,127.0.0.1:8848") | ||
.set_auth_info(auth_info) | ||
.set_tenant(namespace_id) | ||
.set_use_grpc(true) | ||
.set_app_name("foo".to_owned()) | ||
.build_naming_client(); | ||
let servcie_key = ServiceInstanceKey::new("foo", "DEFAULT_GROUP"); | ||
//可以通过监听器获取指定服务的最新实现列表,并支持触发变更回调函数,可用于适配微服务地址选择器。 | ||
let default_listener = InstanceDefaultListener::new( | ||
servcie_key, | ||
Some(Arc::new(|instances, add_list, remove_list| { | ||
println!( | ||
"service instances change,count:{},add count:{},remove count:{}", | ||
instances.len(), | ||
add_list.len(), | ||
remove_list.len() | ||
); | ||
})), | ||
); | ||
//tokio::time::sleep(Duration::from_millis(3000)).await; | ||
client | ||
.subscribe(Box::new(default_listener.clone())) | ||
.await | ||
.unwrap(); | ||
let ip = local_ipaddress::get().unwrap(); | ||
let service_name = "foo"; | ||
let group_name = "DEFAULT_GROUP"; | ||
for i in 0..10 { | ||
let port = 10000 + i; | ||
let instance = Instance::new_simple(&ip, port, service_name, group_name); | ||
//注册 | ||
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().await.unwrap(); | ||
}); | ||
|
||
//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 query_params() -> anyhow::Result<()> { | ||
//get client from global | ||
let client = nacos_rust_client::get_last_naming_client().unwrap(); | ||
let service_name = "foo"; | ||
let group_name = "DEFAULT_GROUP"; | ||
let params = QueryInstanceListParams::new_simple(service_name, group_name); | ||
// 模拟每秒钟获取一次实例 | ||
loop { | ||
//查询并按权重随机选择其中一个实例 | ||
match client.select_instance(params.clone()).await { | ||
Ok(instances) => { | ||
println!("select instance {}:{}", &instances.ip, &instances.port); | ||
} | ||
Err(e) => { | ||
println!("select_instance error {:?}", &e) | ||
} | ||
} | ||
tokio::time::sleep(Duration::from_millis(1000)).await; | ||
} | ||
Ok(()) | ||
} | ||
|