使用rust编写APISIX网关的wasm插件

机器:M1 mbp

首先需要安装rust的环境,rust提供有非常完善友好的工具链。可直接参考官网(https://www.rust-lang.org/zh-CN/tools/install)。

1.创建 Rust 项目:

使用 cargo new命令创建一个新的 Rust 库项目。例如:

cargo new --lib my_proxy_wasm_project

在Cargo.toml文件中添加 proxy-wasm作为依赖项。

2.编写你的 Wasm 插件:

在 src/lib.rs中编写你的插件代码。实现 proxy-wasm 提供的接口,如 HttpContext或 RootContext。如下是一个 对/hello 接口返回“hello,World”并添加response header。

3.编译为wasm插件:

执行如下命令:

rustup target add wasm32-unknown-unknown

cargo build --target wasm32-wasi --release 

生成如下图:

4.测试插件:

因为wasm插件基本都是通用的,所以在本地直接采用envoy进行功能测试验证。(需安装docker,本地测试使用envoy,使用docker compose up)

docoer-cmpose.yaml内容如下:

envoy.yml内容如下:

5.在APISIX中使用

将编译好的 .wasm 文件打入APISIX的镜像中, 选择固定目录,然后在APISIX 中的config.yml 中把.wasm文件全路径及插件相关信息进行配置即可。

wasm:
  plugins:
    - name: wasm_log # the name of the plugin
      priority: 7999 # priority
      file: t/wasm/log/xxx.wasm # the path of `.wasm` file
      http_request_phase: access # default to "access", can be one of ["access", "rewrite"]

然后添加对应插件的dashboard 的 schema.json 文件,便可在dashboard 进行页面化配置。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值