nacos_naming_client/
lib.rs

1mod net;
2mod data;
3pub mod error;
4mod util;
5mod client;
6mod config;
7pub mod constants;
8pub use data::model;
9pub use config::*;
10pub use client::*;
11pub use data::ServiceChangeListener;
12pub use net::{NamingRemote, HttpNamingRemote};
13
14#[cfg(test)]
15mod test {
16
17    use std::time::Duration;
18
19    use async_trait::async_trait;
20
21    use crate::{
22        NamingClient, config::{NamingConfig, ServerConfig}, constants, 
23        error::Result, model::Instance, ServiceChangeListener
24    };
25
26    fn init_logger() {
27        let _ = env_logger::builder()
28            // Include all events in tests
29            .filter_level(log::LevelFilter::max())
30            // Ensure events are captured by `cargo test`
31            .is_test(true)
32            // Ignore errors initializing the logger if tests race to configure it
33            .try_init();
34    }
35
36    #[tokio::test]
37    
38    async fn test_beat() -> Result<()> {
39        init_logger();
40        let config = NamingConfig {
41            namespace_id: "public".to_string(),
42            cluster: constants::DEFAULT_CLUSTER.to_owned(),
43            group: constants::DEFAULT_GROUP.to_owned(),
44            server_list: vec![ServerConfig::new(
45                "http".to_string(), "192.168.1.221:8848".to_string(), "nacos".to_string()
46            )],
47            cache_dir: "/workspaces/nacos-sdk-rust/output/failover".to_owned(),
48            load_at_start: false,
49            update_when_empty: false,
50            user_name: Some("nacos".to_string()),
51            password: Some("nacos".to_string()),
52        };
53        let client = NamingClient::new_http(config).await;
54        
55        // client.register_instance(Instance::new_with_defaults("test", "192.168.1.221", 8888)).await?;
56        client.subscribe("service-im", "DEFAULT_GROUP", vec!["DEFAULT"], Listener).await?;
57        //let instances = client.select_instances("c4", "DEFAULT_GROUP", vec!["DEFAULT"], false).await?;
58        //println!("server data => \n{:?}", instances);
59        tokio::time::sleep(Duration::from_secs(60 * 10)).await;
60
61        Ok(())
62    }
63
64    struct Listener;
65
66    #[async_trait]
67    impl ServiceChangeListener for Listener {
68        async fn changed(&self, service_name: &str, hosts: Vec<Instance>) {
69            println!("changed => {}:{:?}", service_name, hosts);
70        }
71    }
72
73}
74