有没有办法在同一个主程序中同时启动 tonic gRPC 和 actix web 应用程序?

问题描述

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

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

我找到了这个答案,但对我不起作用:Using Actix from a Tokio App: mixing actix_web::main and 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)
}

但我收到一个编译错误提示“预期结构 tokio::task::local::LocalSet,找到结构 LocalSet,但我不知道如何继续。

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)