【问题标题】:Run multiple actix app on different ports在不同的端口上运行多个 actix 应用程序
【发布时间】:2020-01-08 20:19:58
【问题描述】:

我正在尝试运行两个应用程序(一个在端口 3006 上用于管理,另一个在端口 8080 上提供数据)。
他们共享数据库池、缓存...

对于 actix 1.0,我有这个工作(我不知道这是否是最好的方法):

let server = Server::build()
  // FIRST APP
  .bind("", "0.0.0.0:3006", move || {
      HttpService::build().finish({
          App::new()
              .wrap(actix_Logger::new(
                  "WMTS %a %r %s %b %{Referer}i %{User-Agent}i %T",
              ))
              .data(pool.clone())
              .data(cache.clone())
              .service(
                  web::scope("/utils")
                      .route(
                          "one",
                          web::get().to(api::one),
                      )
                      .route("two", web::get().to(api::two))
              )
              .service(
                  web::scope("/data")
                      .route("get_data", web::get().to(api::get_data)),
              )
      })
  })
  .unwrap()
  // SECOND APP
  .bind("", "0.0.0.0:8080", move || {
      HttpService::build().finish(
          App::new()
              .wrap(actix_Logger::new(
                  "API %a %r %s %b %{Referer}i %{User-Agent}i %T",
              ))
              .data(pool.clone())
              .data(cache.clone())
              .service(web::resource("/graphql").route(web::post().to(api::graphql)))
              .service(web::resource("/health").route(web::get().to(api::health)))
              .service(web::resource("/metrics").route(web::get().to(api::metrics))),
      )
  })
  .unwrap();

  server.run()?;

但是如何让它与 actix 2.0 一起工作呢?

【问题讨论】:

    标签: rust rust-actix actix-web


    【解决方案1】:

    actix-web 自己的API 而言,1.02.0 之间确实没有太大变化。这是一件好事,因为您仍然可以使用熟悉的 API 来配置路由、应用程序数据、记录器等。

    确实发生了变化的是actix-web 已移至异步/等待。您的应用程序也需要适应它:

    //# actix-rt = "1.0"
    //# actix-web = "2.0"
    //# futures = "0.3"
    use actix_web::{web, App, HttpServer, Responder};
    use futures::future;
    
    async fn utils_one() -> impl Responder {
        "Utils one reached\n"
    }
    
    async fn health() -> impl Responder {
        "All good\n"
    }
    
    #[actix_rt::main]
    async fn main() -> std::io::Result<()> {
        let s1 = HttpServer::new(move || {
                App::new().service(web::scope("/utils").route("/one", web::get().to(utils_one)))
            })
            .bind("0.0.0.0:3006")?
            .run();
        let s2 = HttpServer::new(move || {
                App::new().service(web::resource("/health").route(web::get().to(health)))
            })
            .bind("0.0.0.0:8080")?
            .run();
        future::try_join(s1, s2).await?;
    
        Ok(())
    }
    

    我想您仍然可以使用Server::build API 来构建多个绑定,但是这里显示的方法更加模块化。 HttpServer::run 现在只返回一个 Future。然后你加入这两个并等待他们两个。

    它按预期工作:

    $ curl http://127.0.0.1:3006/utils/one
    Utils one reached
    $ curl http://127.0.0.1:8080/health
    All good
    

    【讨论】:

    • 太棒了!谢谢 !我还不熟悉新的 async await 版本。
    • 您知道如何更改HttpServer 未来的错误类型吗?我想将其映射到anyhow::Error,因为我还需要加入另一个不返回std::io::Error的未来@
    猜你喜欢
    • 2020-01-21
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2013-04-04
    • 2015-11-07
    • 2021-03-14
    相关资源
    最近更新 更多