etcd-discovery

Crates.ioetcd-discovery
lib.rsetcd-discovery
version0.1.6
sourcesrc
created_at2023-11-10 10:01:44.988846
updated_at2024-04-29 15:04:19.28124
descriptionEtcd discovery and register for tonic
homepage
repositoryhttps://github.com/cody-why/etcd-discovery
max_upload_size
id1030874
size58,396
plucky (cody-why)

documentation

https://docs.rs/crate/etcd-discovery/latest

README

Etcd discovery and register for tonic

Crates.io version docs.rs docs Download

Use

[dependencies]
tokio = { version = "1", features = ["macros","rt-multi-thread"] }
tonic = "0.11"
etcd-discovery = "0.1"

server

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let addr = "127.0.0.1:50051";
    
    // 使用etcd注册服务
    let opt = ConnectOptions::new().with_user("tonic_user", "789789");
    let mut register = EtcdRegister::connect(["127.0.0.1:2379"], Some(opt)).await?;
    register.lease_grant(30, 10).await?;
    register.put("/hello/1", format!("http://{addr}")).await?;
    
    let greeter = MyGreeter::default();
    let svc = GreeterServer::new(greeter);

    tracing::info!("GreeterServer listening on: {}", addr);
    
    Server::builder()
        // 使用拦截器
        // .layer(tonic::service::interceptor(check_auth))
        .add_service(svc)
        .serve(addr.parse()?)
        .await?;

    Ok(())
}

client


#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt().init();
    // 使用etcd服务发现
    let opt = ConnectOptions::new().with_user("tonic_user", "789789");
    let mut discover = EtcdTonicDiscovery::connect(["127.0.0.1:2379"], Some(opt)).await?;
    discover.service_discover("/hello").await?;
    
    let channel = discover.get_service("/hello/1").unwrap();
    let mut client = GreeterClient::new(channel);

    let request = tonic::Request::new(HelloRequest {
        name: "Tonic".into(),
    });

    let response = client.say_hello(request).await?;
    println!("RESPONSE={:?}", response);

    tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
        
   

    Ok(())
}

Commit count: 15

cargo fmt