相熟 Apache APISIX 的小伙伴都晓得,之前在社区中咱们曾经反对了 Java 和 Go 语言的 Runner,明天 Apache APISIX Python Runner 也来了,社区中的小伙伴们在开发 Apache APISIX 插件时又多了一种新抉择。
Python 语言作为一个解释型的高级编程语言,它语法简洁易上手、代码可读性好 ,在跨平台 、可移植性 、开发效率上都有很好的体现,同时作为一个高级编程语言它的封装形象水平比拟高屏蔽了很多底层细节(例如:GC )让咱们在开发的过程中能够更专一应用逻辑的开发。
同时作为一个有 30 年历史的老牌开发语言,它的生态以及各种模块曾经十分欠缺,咱们大部分的开发和利用场景都能够从社区中找到很成熟的模块或解决方案。
Python 其余的长处就不再一一赘述,当然它的毛病也比拟显著:Python 作为一门解释性语言,相较于 C++ 和 Go 这样的编译型语言,在性能上的差距还是比拟大的。
理解:我的项目架构
apache-apisix-python-runner 这个我的项目能够了解为 Apache APISIX 和 Python 之间的一道桥梁,通过 Python Runner
能够把 Python 间接利用到 Apache APISIX 的插件开发中,最重要的还是心愿让更多对 Apache APISIX 和 API 网关感兴趣的 Python 开发者通过这个我的项目,更多地理解和应用 Apache APISIX,以下为 Apache APISIX 多语言反对的架构图。
上图右边是 Apache APISIX 的工作流程,左边的 Plugin Runner
是各语言的插件运行器,本文介绍的 apisix-python-plugin-runner
就是反对 Python 语言的 Plugin Runner
。
在 Apache APISIX 中配置一个 Plugin Runner
时,Apache APISIX 会启动一个子过程运行 Plugin Runner
,该子过程与 Apache APISIX 过程属于同一个用户,当咱们重启或从新加载 Apache APISIX 时,Plugin Runner
也将被重启。
如果你为一个给定的路由配置了 ext-plugin-*
插件,申请命中该路由时将触发 Apache APISIX 通过 Unix Socket
向 Plugin Runner
发动 RPC
调用。调用分为两个阶段:
- ext-plugin-pre-req :在执行
Apache APISIX
内置插件(Lua 语言插件)之前 - ext-plugin-post-req :在执行
Apache APISIX
内置插件(Lua 语言插件)之后
大家能够依据须要抉择并配置 Plugin Runner
的执行机会。Plugin Runner
会解决 RPC
调用,在其外部创立一个模仿申请,而后运行多语言编写的插件,并将后果返回给 Apache APISIX。
多语言插件的执行程序是在 ext-plugin-*
插件配置项中定义的,像其余插件一样,它们能够被启用并在运行中从新定义。
装置:部署测试
根底运行环境:Apache APISIX 2.7、Python 3.6+
Apache APISIX 的装置部署可参考 Apache APISIX 官网文档:如何构建 Apache APISIX (https://github.com/apache/api...)进行部署。
1. 下载安装 Python Runner
$ git clone https://github.com/apache/apisix-python-plugin-runner.git$ cd apisix-python-plugin-runner$ make install
2. 配置 Python Runner
- 开发模式配置
- 运行 Python Runner
$ cd /path/to/apisix-python-plugin-runner$ APISIX_LISTEN_ADDRESS=unix:/tmp/runner.sock python3 apisix/main.py start
- 批改 Apache APISIX 配置文件
$ vim /path/to/apisix/conf/config.yamlapisix: admin_key: - name: "admin" key: edd1c9f034335f136f87ad84b625c8f1 role: adminext-plugin: path_for_test: /tmp/runner.sock
- 生产模式配置
- 批改 Apache APISIX 配置文件
$ vim /path/to/apisix/conf/config.yamlapisix: admin_key: - name: "admin" key: edd1c9f034335f136f87ad84b625c8f1 role: adminext-plugin: cmd: [ "python3", "/path/to/apisix-python-plugin-runner/apisix/main.py", "start" ]
- Python Runner 配置(可选)
如果须要对 Log Level
或 Unix Domain Socket
环境变量调整能够批改 Runner
的配置文件
$ vim /path/to/apisix-python-plugin-runner/apisix/config.yamlsocket: file: $env.APISIX_LISTEN_ADDRESS # Environment variable or absolute pathlogging: level: debug # error warn info debug
3. 启动 Python Runner
$ cd /path/to/apisix# Start or Restart$ ./bin/apisix [ start | restart ]
启动或重启 Apache APISIX 即可,此时 Apache APISIX 和 Python Runner
曾经实现配置并启动。
4. 测试 Python Runner
- 配置 Apache APISIX 路由及插件信息
# 应用默认demo插件进行测试$ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{ "uri": "/get", "plugins": { "ext-plugin-pre-req": { "conf": [ { "name": "stop", "value":"{\"body\":\"hello\"}"} ] } }, "upstream": { "type": "roundrobin", "nodes": { "127.0.0.1:1980": 1 } }}'
plugins.ext-plugin-pre-req.conf
为Runner
插件配置,conf
为数组格局能够同时设置多个插件。- 插件配置对象中
name
为插件名称,该名称须要与插件代码文件和对象名称统一。 - 插件配置对象中
value
为插件配置,能够为JSON
字符串。
2. 拜访验证
$ curl http://127.0.0.1:9080/get -iHTTP/1.1 200 OKDate: Fri, 13 Aug 2021 13:39:18 GMTContent-Type: text/plain; charset=utf-8Transfer-Encoding: chunkedConnection: keep-alivehost: 127.0.0.1:9080accept: */*user-agent: curl/7.64.1X-Resp-A6-Runner: PythonServer: APISIX/2.7Hello, Python Runner of APISIX
实际:插件开发
1. 插件目录
/path/to/apisix-python-plugin-runner/apisix/plugins
此目录中的 .py
文件将会被主动加载。
2. 插件示例
/path/to/apisix-python-plugin-runner/apisix/plugins/stop.py/path/to/apisix-python-plugin-runner/apisix/plugins/rewrite.py
3. 插件格局
from apisix.runner.plugin.base import Basefrom apisix.runner.http.request import Requestfrom apisix.runner.http.response import Responseclass Stop(Base): def __init__(self): """ Example of `stop` type plugin, features: This type of plugin can customize response `body`, `header`, `http_code` This type of plugin will interrupt the request """ super(Stop, self).__init__(self.__class__.__name__) def filter(self, request: Request, response: Response): """ The plugin executes the main function :param request: request parameters and information :param response: response parameters and information :return: """ # 在插件中能够通过 `self.config` 获取配置信息,如果插件配置为JSON将主动转换为字典构造 # print(self.config) # 设置响应头信息 headers = request.headers headers["X-Resp-A6-Runner"] = "Python" response.headers = headers # 设置响应体信息 response.body = "Hello, Python Runner of APISIX" # 设置响应状态码 response.status_code = 201 # 通过调用 `self.stop()` 中断请求流程,此时将立刻响应申请给客户端 # 如果未显示调用 `self.stop()` 或 显示调用 `self.rewrite()`将持续将申请 # 默认为 `self.rewrite()` self.stop()
4. 插件标准及注意事项
- 实现插件对象必须继承
Base
类 - 插件必须实现
filter
函数 filter
函数参数只能蕴含Request
和Response
类对象作为参数Request
对象参数能够获取申请信息Response
对象参数能够设置响应信息self.config
能够获取插件配置信息filter
函数中调用self.stop()
时将马上中断请求,响应数据。filter
函数中调用self.rewrite()
时,将会持续申请。
奉献:参加我的项目
目前 Apache APISIX 各语言的 Runner 还处于晚期开发阶段,咱们会陆续欠缺其性能。胜利的开源我的项目离不开大家的参加和奉献,欢送各位参加 Apache APISIX Runner
的开发,让咱们一起共建 Apache APISIX 与各语言的桥梁。具体我的项目可参考下方:
- apisix-python-plugin-runner (https://github.com/apache/api...)
- apisix-go-plugin-runner (https://github.com/apache/api...)
- apisix-java-plugin-runner (https://github.com/apache/api...)
举荐浏览
- Go 让 Apache APISIX 锦上添花
- 如何用 Java 编写 Apache APISIX 插件
作者介绍
帅进超,Apache APISIX PMC
对于 Apache APISIX
Apache APISIX 是一个动静、实时、高性能的开源 API 网关,提供负载平衡、动静上游、灰度公布、服务熔断、身份认证、可观测性等丰盛的流量治理性能。Apache APISIX 能够帮忙企业疾速、平安的解决 API 和微服务流量,包含网关、Kubernetes Ingress 和服务网格等。
寰球已有数百家企业应用 Apache APISIX 解决要害业务流量,涵盖金融、互联网、制作、批发、运营商等等,比方美国航空航天局(NASA)、欧盟的数字工厂、中国航信、中国移动、腾讯、华为、微博、网易、贝壳找房、360、泰康、奈雪的茶等。
200 余位贡献者,一起缔造了 Apache APISIX 这个世界上最沉闷的开源网关我的项目。聪慧的开发者们!快来退出这个沉闷而多样化的社区,一起来给这个世界带来更多美妙的货色吧!
- Apache APISIX GitHub 地址:https://github.com/apache/apisix
- Apache APISIX 官网:https://apisix.apache.org/
- Apache APISIX 文档:https://apisix.apache.org/zh/...