This makes it so you can check if the initial connection is established. Before this we used reconnect which would lazily attempt to connect. So if you were trying to connect to a non existant Server you wouldn't find out until after you attempted your first RPC. This simplifies everything by allowing you connect before creating the RPC client. BREAKING CHANGE: `Endpoint::channel` was removed in favor of an async `Endpoint::connect`.
40 lines
978 B
Rust
40 lines
978 B
Rust
pub mod hello_world {
|
|
tonic::include_proto!("helloworld");
|
|
}
|
|
|
|
pub mod echo {
|
|
tonic::include_proto!("grpc.examples.echo");
|
|
}
|
|
|
|
use echo::{client::EchoClient, EchoRequest};
|
|
use hello_world::{client::GreeterClient, HelloRequest};
|
|
use tonic::transport::Endpoint;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let channel = Endpoint::from_static("http://[::1]:50051")
|
|
.connect()
|
|
.await?;
|
|
|
|
let mut greeter_client = GreeterClient::new(channel.clone());
|
|
let mut echo_client = EchoClient::new(channel);
|
|
|
|
let request = tonic::Request::new(HelloRequest {
|
|
name: "Tonic".into(),
|
|
});
|
|
|
|
let response = greeter_client.say_hello(request).await?;
|
|
|
|
println!("GREETER RESPONSE={:?}", response);
|
|
|
|
let request = tonic::Request::new(EchoRequest {
|
|
message: "hello".into(),
|
|
});
|
|
|
|
let response = echo_client.unary_echo(request).await?;
|
|
|
|
println!("ECHO RESPONSE={:?}", response);
|
|
|
|
Ok(())
|
|
}
|