0

我正在尝试编写一个客户端来与使用 Rust 用 tonic 编写的 grpc 服务器进行通信,但是我无法理解在哪里定义和连接到客户端,因此我的 import 语句出现错误。我已经学习了几个教程,但在查找有关如何创建和导入客户端的信息时遇到了麻烦。我的错误是:

error[E0432]: unresolved import `user::client`
 --> user/src/client.rs:2:36
  |
2 | use user::client::{UserClient};
  |                                    ^^^^^^ could not find `client` in `user`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0432`.
error: could not compile `user`

在我的proto.rs文件中:

syntax = "proto3";

package user;

message CreateUser {
  string name = 1;
}



[package]
name = "user"
version = "0.1.0"
edition = "2018"

[lib]

[[bin]] 
name = "server"
path = "src/server.rs"

[[bin]]
name = "client"
path = "src/client.rs"

[dependencies]
tonic = "0.5"
prost = "0.8"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }


[build-dependencies]
tonic-build = "0.5"

我的lib.rs文件:

pub mod user {
    tonic::include_proto!("user");
}

pub mod server;
pub mod client{
    tonic::include_proto!("user");
}

main.rs

use user::server;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing::info!(message = "Started user server");
    server::start_server().await?;

    Ok(())
}

client.rs

use user::{UserRequest };
use user::client::{UserClient}; // what should this import be, where does the client et created?

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = UserClient::connect("http://[::1]:50051").await?; // how do I create UserClient

    let request = tonic::Request::new(UserRequest {
        id: "1".into(),
    });

    println!("Sending request to gRPC Server...");
    let response = client.create_user(request).await?;

    println!("RESPONSE={:?}", response);

    Ok(())
}

作为参考,我正在关注:

https://tjtelan.com/blog/lets-build-a-single-binary-grpc-server-client-with-rust-in-2020/

https://blog.logrocket.com/rust-and-grpc-a-complete-guide/

https://dev.to/anshulgoyal15/a-beginners-guide-to-grpc-with-rust-3c7o

4

0 回答 0