diff --git a/examples/greeter/Cargo.toml b/examples/greeter/Cargo.toml index 4e56f4e4..39077b8c 100644 --- a/examples/greeter/Cargo.toml +++ b/examples/greeter/Cargo.toml @@ -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" } diff --git a/examples/greeter/src/greeter/client.rs b/examples/greeter/src/greeter/client.rs index d6208434..855f9e6f 100644 --- a/examples/greeter/src/greeter/client.rs +++ b/examples/greeter/src/greeter/client.rs @@ -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}; @@ -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 { diff --git a/registry-nacos/src/nacos_registry.rs b/registry-nacos/src/nacos_registry.rs index 888382f0..360febad 100644 --- a/registry-nacos/src/nacos_registry.rs +++ b/registry-nacos/src/nacos_registry.rs @@ -60,7 +60,7 @@ const INNERCLASS_SYMBOL: &str = "$"; const INNERCLASS_COMPATIBLE_SYMBOL: &str = "___"; pub struct NacosRegistry { - nacos_naming_service: Arc, + nacos_naming_service: Arc, listeners: Mutex>>>, }