65.9K
CodeProject 正在变化。 阅读更多。
Home

Dapr 的轻量级、安全、可移植且高性能运行时

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3投票s)

2023 年 1 月 5 日

CPOL

6分钟阅读

viewsIcon

8798

如何使用高性能 Wasm 运行时 WasmEdge 作为 Dapr 的 sidecar 应用程序运行时

自 2019 年发布以来,Dapr(分布式应用程序运行时)已迅速成为构建微服务的非常受欢迎的开源框架。它提供了分布式应用程序中常用的构建块和预打包服务,例如服务调用、状态管理、消息队列、资源绑定和触发器、mTLS 安全连接以及服务监控。分布式应用程序开发人员可以在运行时利用并使用这些构建块公开的基于 Web 的 API。这些应用程序通常被称为微服务,并作为 sidecar 运行。Dapr 是多运行时微服务架构的一个例子。

Dapr 的 sidecar 模式非常类似于服务网格。然而,与旨在无需任何代码更改即可管理应用程序的传统服务网格不同,Dapr 应用程序需要集成并积极利用外部 Dapr 构建块服务。

Dapr sidecar 中的微服务应用程序可以是本机客户端 (NaCl) 应用程序(从 Go 和 Rust 等语言编译而来),也可以是 Python 或 JavaScript 等语言编写的托管语言应用程序。换句话说,sidecar 应用程序可以有自己的语言运行时。sidecar 模型允许 Dapr 为其应用程序支持“任何语言、任何框架、任何地方”。

WebAssembly 和 WasmEdge

Dapr 可以直接在操作系统上运行 sidecar 应用程序,也可以通过 Docker 等应用程序容器运行。容器提供了可移植性、易于部署和安全性等优势,但也带来了显著的开销。

在本文中,我们提出了一种运行 Dapr sidecar 应用程序的新方法。我们使用用 Rust 或 Go 编写的简单 NaCl 来监听微服务的 API 请求。它将请求数据传递给 WebAssembly 运行时进行处理。微服务的业务逻辑是应用程序开发人员创建和部署的 WebAssembly 函数。

图 1. 带有 WebAssembly 函数的 Dapr 微服务。

WebAssembly 运行时非常适合执行业务逻辑函数

  • WebAssembly 程序运行速度可以媲美编译后的本机二进制文件,并且比容器消耗的资源少得多。
  • WebAssembly 支持 C/C++、Rust、Swift 和 Kotlin 等高性能语言。它还可以支持 JavaScript 和 DSL(领域特定语言)等高级语言。
  • WebAssembly 程序是可移植的,可以轻松地跨不同的操作系统和硬件平台进行部署。
  • WebAssembly 提供了一个安全的沙箱,可以在运行时隔离应用程序。开发人员可以通过声明安全策略来限制程序对操作系统或其他资源的访问。

下表总结了 sidecar 应用程序不同方法的优缺点。

WasmEdge 是一个领先的云原生 WebAssembly 运行时,由 CNCF(云原生计算基金会)/ Linux Foundation 托管。它是当今市场上最快的 WebAssembly 运行时。WasmEdge 支持所有标准的 WebAssembly 扩展以及专有的 TensorFlow 推理、KV 存储和图像处理等扩展。其编译器工具链不仅支持 C/C++、Rust、Swift、Kotlin 和 AssemblyScript 等 WebAssembly 语言,还支持常规 JavaScript

WasmEdge 应用程序可以嵌入到 C 程序、Go 程序、Rust 程序、JavaScript 程序或操作系统的命令行中。运行时可以由 Docker 工具(例如 CRI-O)、编排工具(例如 K8s)、无服务器平台(例如 VercelNetlifyAWS LambdaTencent SCF)以及数据流框架(例如 YoMo 和 Zenoh)进行管理。

在本文中,我将演示如何使用 WasmEdge 作为 Dapr 的 sidecar 应用程序运行时。

快速入门

首先,您需要安装 GoRustDaprWasmEdgerustwasmc 编译器工具。

接下来,从 Github fork 或克隆演示应用程序。您可以将此仓库用作您自己的应用程序模板。

$ git clone https://github.com/second-state/dapr-wasm

演示包含三个 Dapr sidecar 应用程序。

  • web-port 项目提供了一个公共 Web 服务,用于显示静态 HTML 页面。这是应用程序的 UI。
  • image-api-rs 项目提供了一个 WasmEdge 微服务,使用 grayscale 函数将输入图像转换为灰度图像。它演示了 Rust SDK 在 Dapr 和 WasmEdge 中的使用。
  • image-api-go 项目提供了一个 WasmEdge 微服务,使用 classify 函数识别和分类输入图像上的对象。它演示了 Go SDK 在 Dapr 和 WasmEdge 中的使用。

图 2. 演示应用程序中的 Dapr sidecar 微服务。

您可以按照README中的说明启动 sidecar 服务。以下是构建 WebAssembly 函数和启动三个 sidecar 服务的命令。

# Build the classify and grayscale WebAssembly functions, 
# and deploy them to the sidecar projects
$ cd functions/grayscale
$ ./build.sh
$ cd ../../
$ cd functions/classify
$ ./build.sh
$ cd ../../

# Build and start the web service for the application UI
$ cd web-port
$ go build
$ ./run_web.sh
$ cd ../

# Build and start the microservice for image processing (grayscale)
$ cd image-api-rs
$ cargo build
$ ./run_api_rs.sh
$ cd ../

# Build and start the microservice for tensorflow-based image classification
$ cd image-api-go
$ go build --tags "tensorflow image"
$ ./run_api_go.sh
$ cd ../

最后,您应该可以在浏览器中看到 Web UI。

图 3. 演示应用程序的运行情况。

两个 WebAssembly 函数

我们有两个用 Rust 编写并编译成 WebAssembly 的函数。它们部署在 sidecar 微服务中,以执行实际的图像处理和分类工作。

虽然我们的示例 WebAssembly 函数是用 Rust 编写的,但您可以将用 C/C++、Swift、Kotlin 和 AssemblyScript 编写的函数编译成 WebAssembly。WasmEdge 还支持用 JavaScript 和 DSL 编写的函数。

grayscale 函数是一个 Rust 程序,它从 STDIN 读取图像数据,并将灰度图像写入 STDOUT

use image::{ImageFormat, ImageOutputFormat};
use std::io::{self, Read, Write};

fn main() {
   let mut buf = Vec::new();
   io::stdin().read_to_end(&mut buf).unwrap();

   let image_format_detected: ImageFormat = image::guess_format(&buf).unwrap();
   let img = image::load_from_memory(&buf).unwrap();
   let filtered = img.grayscale();
   let mut buf = vec![];
   match image_format_detected {
       ImageFormat::Gif => {
           filtered.write_to(&mut buf, ImageOutputFormat::Gif).unwrap();
       }
       _ => {
           filtered.write_to(&mut buf, ImageOutputFormat::Png).unwrap();
       }
   };
   io::stdout().write_all(&buf).unwrap();
   io::stdout().flush().unwrap();
}

我们使用 rustwasmc 进行构建,然后将其复制到 image-api-rs sidecar。

$ cd functions/grayscale
$ rustup override set 1.50.0
$ rustwasmc  build --enable-ext
$ cp ./pkg/grayscale.wasm ../../image-api-rs/lib 

classify 函数是一个 Rust 函数,它接受一个图像数据的字节数组作为输入,并返回一个表示分类的字符串。它使用WasmEdge TensorFlow API

use wasmedge_tensorflow_interface;

pub fn infer_internal(image_data: &[u8]) -> String {
   let model_data: &[u8] = include_bytes!
   ("models/mobilenet_v1_1.0_224/mobilenet_v1_1.0_224_quant.tflite");
   let labels = include_str!
   ("models/mobilenet_v1_1.0_224/labels_mobilenet_quant_v1_224.txt");

   let flat_img = wasmedge_tensorflow_interface::load_jpg_image_to_rgb8
                  (image_data, 224, 224);

   let mut session = wasmedge_tensorflow_interface::Session::new(
       &model_data,
       wasmedge_tensorflow_interface::ModelType::TensorFlowLite,
   );
   session
       .add_input("input", &flat_img, &[1, 224, 224, 3])
       .run();
   let res_vec: Vec<u8> = session.get_output("MobilenetV1/Predictions/Reshape_1");

   // ... Map the probabilities in res_vec to text labels in the labels file ...
  
   if max_value > 50 {
       format!(
           "It {} a <a href='https://www.google.com/search?q={}'>{}</a> in the picture",
           confidence.to_string(),
           class_name,
           class_name
       )
   } else {
       format!("It does not appears to be any food item in the picture.")
   }
}

我们使用 rustwasmc 进行构建,然后将其复制到 image-api-go sidecar。

$ cd functions/classify
$ rustup override set 1.50.0
$ rustwasmc  build --enable-ext
$ cp ./pkg/classify_bg.wasm ../../image-api-go/lib/classify_bg.wasm 

在接下来的三个部分中,我们将分别介绍这三个 sidecar 服务。

图像处理 Sidecar

image-api-rs sidecar 应用程序是用 Rust 编写的。它应该已经从上一步安装了 WebAssembly 函数 lib/grayscale.wasm。请参考 functions/bin/install.sh 脚本来安装 WasmEdge Runtime 二进制文件 lib/wasmedge-tensorflow-lite 及其依赖项。

sidecar 微服务运行一个基于 Tokio 的事件循环,在路径 /api/image 上监听传入的 HTTP 请求。

#[tokio::main]
pub async fn run_server(port: u16) {
   pretty_env_logger::init();

   let home = warp::get().map(warp::reply);

   let image = warp::post()
       .and(warp::path("api"))
       .and(warp::path("image"))
       .and(warp::body::bytes())
       .map(|bytes: bytes::Bytes| {
           let v: Vec<u8> = bytes.iter().map(|&x| x).collect();
           let res = image_process(&v);
           Ok(Box::new(res))
       });

   let routes = home.or(image);
   let routes = routes.with(warp::cors().allow_any_origin());

   let log = warp::log("dapr_wasm");
   let routes = routes.with(log);
   warp::serve(routes).run((Ipv4Addr::UNSPECIFIED, port)).await
}

一旦在 HTTP POST 请求中接收到图像文件,它就会调用 WasmEdge 中的 WebAssembly 函数来执行图像处理任务。它创建一个 WasmEdge 实例来与 WebAssembly 程序进行交互。

pub fn image_process(buf: &Vec<u8>) -> Vec<u8> {
   let mut child = Command::new("./lib/wasmedge-tensorflow-lite")
       .arg("./lib/grayscale.wasm")
       .stdin(Stdio::piped())
       .stdout(Stdio::piped())
       .spawn()
       .expect("failed to execute child");
   {
       // limited borrow of stdin
       let stdin = child.stdin.as_mut().expect("failed to get stdin");
       stdin.write_all(buf).expect("failed to write to stdin");
   }
   let output = child.wait_with_output().expect("failed to wait on child");
   output.stdout
}

以下 Dapr CLI 命令将在 Dapr 运行时环境中启动该微服务。

$ cd image-api-rs
$ sudo dapr run --app-id image-api-rs \
        --app-protocol http \
        --app-port 9004 \
        --dapr-http-port 3502 \
        --components-path ../config \
        --log-level debug \
        ./target/debug/image-api-rs
$ cd ../

Tensorflow Sidecar

image-api-go sidecar 应用程序是用 Go 编写的。它应该已经从上一步安装了 WebAssembly 函数 lib/classify_bg.wasm。请参考 functions/bin/install.sh 脚本来安装 WasmEdge Runtime Go SDK。

sidecar 微服务运行一个事件循环,在路径 /api/image 上监听传入的 HTTP 请求。

func main() {
   s := daprd.NewService(":9003")

   if err := s.AddServiceInvocationHandler("/api/image", imageHandlerWASI); err != nil {
       log.Fatalf("error adding invocation handler: %v", err)
   }

   if err := s.Start(); err != nil && err != http.ErrServerClosed {
       log.Fatalf("error listenning: %v", err)
   }
}

一旦在 HTTP POST 请求中接收到图像文件,它就会调用 WasmEdge 中的 WebAssembly 函数来执行基于 Tensorflow 的图像分类任务。它利用 WasmEdge 的 Go API 与 WebAssembly 程序进行交互。

func imageHandlerWASI
(_ context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
   image := in.Data

   var conf = wasmedge.NewConfigure(wasmedge.REFERENCE_TYPES)
   conf.AddConfig(wasmedge.WASI)
   var vm = wasmedge.NewVMWithConfig(conf)

   var wasi = vm.GetImportObject(wasmedge.WASI)
   wasi.InitWasi(
       os.Args[1:],     /// The args
       os.Environ(),    /// The envs
       []string{".:."}, /// The mapping directories
       []string{},      /// The preopens will be empty
   )

   /// Register WasmEdge-tensorflow and WasmEdge-image
   var tfobj = wasmedge.NewTensorflowImportObject()
   var tfliteobj = wasmedge.NewTensorflowLiteImportObject()
   vm.RegisterImport(tfobj)
   vm.RegisterImport(tfliteobj)
   var imgobj = wasmedge.NewImageImportObject()
   vm.RegisterImport(imgobj)

   vm.LoadWasmFile("./lib/classify_bg.wasm")
   vm.Validate()
   vm.Instantiate()

   res, err := vm.ExecuteBindgen("infer", wasmedge.Bindgen_return_array, image)
   ans := string(res.([]byte))
  
   vm.Delete()
   conf.Delete()

   out = &common.Content{
       Data:        []byte(ans),
       ContentType: in.ContentType,
       DataTypeURL: in.DataTypeURL,
   }
   return out, nil
}

以下 Dapr CLI 命令将在 Dapr 运行时环境中启动该微服务。

$ cd image-api-go
$ sudo dapr run --app-id image-api-go \
        --app-protocol http \
        --app-port 9003 \
        --dapr-http-port 3501 \
        --log-level debug \
        --components-path ../config \
        ./image-api-go
$ cd ../ 

Web UI Sidecar

Web UI 服务 web-port 是一个用 Go 编写的简单 Web 服务器。它从 static 文件夹提供静态 HTML 和 JavaScript 文件,并将上传到 /api/hello 的图像发送到 grayscaleclassify sidecar 的 /api/image 端点。

func main() {
   http.HandleFunc("/static/", staticHandler)
   http.HandleFunc("/api/hello", imageHandler)
   println("listen to 8080 ...")
   log.Fatal(http.ListenAndServe(":8080", nil))
}

func staticHandler(w http.ResponseWriter, r *http.Request) {
   // ... read and return the contents of HTML CSS and JS files ...
}

func imageHandler(w http.ResponseWriter, r *http.Request) {
   // ... ...
   api := r.Header.Get("api")
   if api == "go" {
       daprClientSend(body, w)
   } else {
       httpClientSend(body, w)
   }
}

// Send to the image-api-go sidecar (classify) via the Dapr API
func daprClientSend(image []byte, w http.ResponseWriter) {
   // ... ...
   resp, err := client.InvokeMethodWithContent
   (ctx, "image-api-go", "/api/image", "post", content)
   // ... ...
}

// Send to the image-api-rs sidecar (grayscale) via the HTTP API
func httpClientSend(image []byte, w http.ResponseWriter) {
   // ... ...
   req, err := http.NewRequest
   ("POST", "https://:3502/v1.0/invoke/image-api-rs/method/api/image", 
     bytes.NewBuffer(image))
   // ... ...
}

page.js 中的 JavaScript 只是将图像上传到 web-port sidecar 的 /api/hello 端点,而 web-port 将根据请求头 api 请求 classify 或 grayscale 微服务。

function runWasm(e) {
   const reader = new FileReader();
   reader.onload = function (e) {
       setLoading(true);
       var req = new XMLHttpRequest();
       req.open("POST", '/api/hello', true);
       req.setRequestHeader('api', getApi());
       req.onload = function () {
           // ...  display results ...
       };
       const blob = new Blob([e.target.result], {
           type: 'application/octet-stream'
       });
       req.send(blob);
   };
   console.log(image.file)
   reader.readAsArrayBuffer(image.file);
}

以下 Dapr CLI 命令将为静态 UI 文件启动 Web 服务。

$ cd web-port
$ sudo dapr run --app-id go-web-port \
        --app-protocol http \
        --app-port 8080 \
        --dapr-http-port 3500 \
        --components-path ../config \
        --log-level debug \
        ./web-port
$ cd ../ 

就是这样!您现在拥有一个由两种语言编写的三部分分布式应用程序!

下一步

正如我们所展示的,Dapr 的分布式网络运行时与 WasmEdge 的通用语言运行时之间存在着很多协同作用。这种方法可以推广并应用于其他服务网格或分布式应用程序框架。与 Dapr 不同,许多服务网格只能在 Kubernetes 中作为其控制平面运行,因此依赖于 Kubernetes API。WasmEdge 是一个与 Kubernetes 兼容的运行时,可以作为运行微服务的轻量级容器替代方案发挥重要作用。敬请关注!

历史

  • 2023 年 1 月 5 日:初始版本
© . All rights reserved.