Skip to content

Commit

Permalink
Ftr: add nacos registry example
Browse files Browse the repository at this point in the history
  • Loading branch information
onewe committed Feb 24, 2023
1 parent 4aabde8 commit 2ae2547
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions examples/greeter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tracing-subscriber = "0.2.0"
dubbo = {path = "../../dubbo", version = "0.3.0" }
dubbo-config = {path = "../../config", version = "0.3.0" }
dubbo-registry-zookeeper = {path = "../../registry-zookeeper", version = "0.3.0" }
dubbo-registry-nacos = {path = "../../registry-nacos", version = "0.3.0" }

[build-dependencies]
dubbo-build = {path = "../../dubbo-build", version = "0.3.0" }
34 changes: 20 additions & 14 deletions examples/greeter/src/greeter/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ pub mod protos {
include!(concat!(env!("OUT_DIR"), "/org.apache.dubbo.sample.tri.rs"));
}

use dubbo::codegen::*;
use std::env;

use dubbo::{codegen::*, common::url::Url};
use dubbo_registry_nacos::nacos_registry::NacosRegistry;
use dubbo_registry_zookeeper::zookeeper_registry::ZookeeperRegistry;
use futures_util::StreamExt;
use protos::{greeter_client::GreeterClient, GreeterRequest};
Expand All @@ -39,21 +42,24 @@ async fn main() {

tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");

// let mut cli = GreeterClient::new(ClientBuilder::from_static(&"http://127.0.0.1:8888"));
let mut builder = ClientBuilder::new();

if let Ok(zk_servers) = env::var("ZOOKEEPER_SERVERS") {
let zkr = ZookeeperRegistry::new(&zk_servers);
let directory = RegistryDirectory::new(Box::new(zkr));
builder = builder.with_directory(Box::new(directory));
} else if let Ok(nacos_url_str) = env::var("NACOS_URL") {
// NACOS_URL=nacos://mse-96efa264-p.nacos-ans.mse.aliyuncs.com
let nacos_url = Url::from_url(&nacos_url_str).unwrap();
let registry = NacosRegistry::new(nacos_url);
let directory = RegistryDirectory::new(Box::new(registry));
builder = builder.with_directory(Box::new(directory));
} else {
builder = builder.with_host("http://127.0.0.1:8888");
}

// Here is example for zk
// let zk_connect_string = match env::var("ZOOKEEPER_SERVERS") {
// Ok(val) => val,
// Err(_) => "localhost:2181".to_string(),
// };
// let zkr = ZookeeperRegistry::new(&zk_connect_string);
// let directory = RegistryDirectory::new(Box::new(zkr));
// cli = cli.with_directory(Box::new(directory));
let mut cli = GreeterClient::new(builder);

let zkr = ZookeeperRegistry::default();
let directory = RegistryDirectory::new(Box::new(zkr));
let mut cli = GreeterClient::new(ClientBuilder::new().with_registry_directory(directory));
// using loop for loadbalance test
println!("# unary call");
let resp = cli
.greet(Request::new(GreeterRequest {
Expand Down
2 changes: 1 addition & 1 deletion registry-nacos/src/nacos_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const INNERCLASS_SYMBOL: &str = "$";
const INNERCLASS_COMPATIBLE_SYMBOL: &str = "___";

pub struct NacosRegistry {
nacos_naming_service: Arc<dyn NamingService>,
nacos_naming_service: Arc<dyn NamingService + Sync + Send + 'static>,
listeners: Mutex<HashMap<String, HashSet<Arc<NotifyListenerWrapper>>>>,
}

Expand Down

0 comments on commit 2ae2547

Please sign in to comment.