Skip to main content

etcd_discovery/
etcd_discovery.rs

1use rust_zero_core::{EtcdClient, EtcdConfig};
2use std::{env, time::Duration};
3
4#[tokio::main(flavor = "current_thread")]
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let endpoint = env::var("ETCD_ENDPOINT").unwrap_or_else(|_| "http://127.0.0.1:2379".to_owned());
7    let advertised =
8        env::var("SERVICE_ENDPOINT").unwrap_or_else(|_| "http://127.0.0.1:8080".to_owned());
9    let client =
10        EtcdClient::connect(EtcdConfig::new([endpoint]).with_namespace("/rust-zero/examples"))
11            .await?;
12    let subscription = client.subscribe("users").await?;
13    let _lease = client
14        .publish("users", "users-local", advertised, Duration::from_secs(10))
15        .await?;
16
17    println!(
18        "published users-local; discovered {:?}",
19        subscription.endpoints()
20    );
21    tokio::signal::ctrl_c().await?;
22    Ok(())
23}