Rust Web框架Actix-web 2.0 體驗

2019-12-26     鋒哥愛學習

簡介

Rust web框架Actix web 在昨天發布了2.0版,作為Rust的粉絲,當然要第一時間體驗了

代碼


新建一個基本的代碼目錄,cargo new 就可以, 參見我的目錄



src/main.rs 的內容


use actix_web::{get, web, App, HttpServer, Responder};#[get("/")]async fn index() -> impl Responder {    format!("Hello World")}#[actix_rt::main]async fn main() -> std::io::Result<()> {    println!("Listen on 127.0.0.1:8080");    HttpServer::new(|| App::new().service(index))        .backlog(65536)        .maxconn(65536)        .maxconnrate(65536)        .bind("127.0.0.1:8080")?        .run()        .await}


cargo.toml的內容


[package]name = "actix-hello"version = "0.1.0"authors = ["ZhiFeng Hu"]edition = "2019"# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]actix-web = "2.0"actix-rt = "1.0.0"


編譯運行


如果你只是跑一下試試,那麼執行


cargo run


然後打開瀏覽器,訪問 http://localhost:8080就可以看到效果了


如果需要編譯二進位可執行程序,就運行


cargo build


編譯完之後,可執行文件在 target/release



性能

拉一個性能跑分測試,結果能跑到3.6w qps,性能還可以吧


總結

actix-web是基於rust語言開發的,異步的web框架,可以用於構建web應用,或者提供api。 性能也還可以,比php,python等動態語言要強,略遜於 java,golang。值得體驗。

文章來源: https://twgreatdaily.com/zh-sg/TRqjT28BMH2_cNUgWGts.html