This is an automated email from the ASF dual-hosted git repository.

yangyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/dubbo-rust.git


The following commit(s) were added to refs/heads/main by this push:
     new 78148be  Ftr: add nacos registry example (#118)
78148be is described below

commit 78148be5114c232d30b47a87eed16d66e051b37e
Author: 毛文超 <[email protected]>
AuthorDate: Wed Mar 1 22:02:05 2023 +0800

    Ftr: add nacos registry example (#118)
---
 examples/echo/src/generated/grpc.examples.echo.rs |  4 +--
 examples/greeter/Cargo.toml                       |  1 +
 examples/greeter/src/greeter/client.rs            | 35 ++++++++++++++---------
 registry/nacos/Cargo.toml                         |  3 +-
 registry/nacos/src/nacos_registry.rs              |  2 +-
 5 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/examples/echo/src/generated/grpc.examples.echo.rs 
b/examples/echo/src/generated/grpc.examples.echo.rs
index ccb385c..16fb163 100644
--- a/examples/echo/src/generated/grpc.examples.echo.rs
+++ b/examples/echo/src/generated/grpc.examples.echo.rs
@@ -1,12 +1,12 @@
+// @generated by apache/dubbo-rust.
+
 /// EchoRequest is the request for echo.
-#[allow(clippy::derive_partial_eq_without_eq)]
 #[derive(Clone, PartialEq, ::prost::Message)]
 pub struct EchoRequest {
     #[prost(string, tag = "1")]
     pub message: ::prost::alloc::string::String,
 }
 /// EchoResponse is the response for echo.
-#[allow(clippy::derive_partial_eq_without_eq)]
 #[derive(Clone, PartialEq, ::prost::Message)]
 pub struct EchoResponse {
     #[prost(string, tag = "1")]
diff --git a/examples/greeter/Cargo.toml b/examples/greeter/Cargo.toml
index 0b14d1a..37bbbde 100644
--- a/examples/greeter/Cargo.toml
+++ b/examples/greeter/Cargo.toml
@@ -32,6 +32,7 @@ logger = {path="../../common/logger"}
 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 e01bf92..71d8f24 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};
@@ -28,21 +31,25 @@ use protos::{greeter_client::GreeterClient, GreeterRequest};
 #[tokio::main]
 async fn main() {
     logger::init();
-    // let mut cli = 
GreeterClient::new(ClientBuilder::from_static(&"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 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";);
+    }
+
+    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/Cargo.toml b/registry/nacos/Cargo.toml
index 36a8fd1..1e4518d 100644
--- a/registry/nacos/Cargo.toml
+++ b/registry/nacos/Cargo.toml
@@ -9,7 +9,7 @@ repository = "https://github.com/apache/dubbo-rust.git";
 # See more keys and their definitions at 
https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
-nacos-sdk = { version = "0.2", features = ["naming", "auth-by-http"] }
+nacos-sdk = { version = "0.2.3", features = ["naming", "auth-by-http"] }
 dubbo.workspace = true
 serde_json.workspace = true
 serde = { workspace = true, features = ["derive"] }
@@ -17,3 +17,4 @@ anyhow.workspace = true
 logger.workspace = true
 [dev-dependencies]
 tracing-subscriber = "0.3.16"
+tracing = "0.1"
diff --git a/registry/nacos/src/nacos_registry.rs 
b/registry/nacos/src/nacos_registry.rs
index 0ec8b40..8fd90fc 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<dyn NamingService>,
+    nacos_naming_service: Arc<dyn NamingService + Sync + Send + 'static>,
     listeners: Mutex<HashMap<String, HashSet<Arc<NotifyListenerWrapper>>>>,
 }
 

Reply via email to