1

我正在尝试实现一个服务于 gRPC 和 REST 调用并且由单个二进制文件启动的服务器。

在这个主要功能中,我试图启动一个 tonic gRPC 服务器和一个 actix-web REST API。

我找到了这个答案,但它对我不起作用:使用 Tokio 应用程序中的 Actix:混合 actix_web::main 和 tokio::main?

Tonic 使用#[tokio::main]而 actix 使用#[actix_web::main]

我运行cargo expand了两个主要功能并得到以下信息:

  • #[tokio::main]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .expect("Failed building the Runtime")
        .block_on(async {
            # my code goes here
        })
}
  • #[actix_web::main]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    actix_web::rt::System::new("main").block_on(async move {
        {
            # my code goes here
        }
    })
}

我尝试遵循此讨论中的代码:https://github.com/actix/actix-web/issues/1283他们在其中使用此代码:

#[tokio::main]
async fn main() -> std::io::Result<()>{
    let local = tokio::task::LocalSet::new();
    let sys = actix_web::rt::System::run_in_tokio("server", &local);
    let server_res = HttpServer::new(|| App::new().route("/", web::get().to(hello_world)))
        .bind("0.0.0.0:8000")?
        .run()
        .await?;
    sys.await?;
    Ok(server_res)
}

但我收到一个编译错误,说 `expected struct tokio::task::local::LocalSet, found struct LocalSet,我不知道如何继续。

我对 Rust 很陌生,所以任何建议都值得赞赏。

4

0 回答 0