拓冰建站拓冰建站
首页 / 资讯中心 / 正文

Apache APISIX openfunction 插件详解:将 OpenFunction 无服务器函数接入 API 网关

Apache APISIX openfunction 插件详解将 OpenFunction 无服务器函数接入 API 网关【免费下载链接】apisixThe Cloud-Native API Gateway项目地址: https://gitcode.com/GitHub_Trending/ap/apisixopenfunction插件是 Apache APISIX 中用于对接开源分布式无服务器平台 CNCF OpenFunction 的动态上游集成插件启用后APISIX 会终止对已配置 URI 的请求并代表客户端向 OpenFunction 的 function 发起新请求再将响应返回给客户端。读完本文你将掌握该插件的全部属性配置、启用与测试流程、基于 service_token 的 Basic 认证方式以及 URL 路径转发的高级用法并能结合源码理解其底层请求代理机制。插件定位与工作原理openfunction插件的作用是把 OpenFunction 平台上的函数function作为 APISIX 路由的动态上游。传统上无服务器函数通常部署在 Kubernetes 集群中通过 NodePort 或 Ingress 对外暴露而接入 APISIX 后你可以利用 APISIX 的路由、鉴权、限流、日志等全套网关能力统一管理函数入口。从源码结构看该插件并非从零实现而是基于 APISIX 的通用无服务器上游工厂函数generic-upstream构建。其完整实现只有短短几十行见 apisix/plugins/openfunction.lualocal ngx_encode_base64 ngx.encode_base64 local plugin_name, plugin_version, priority openfunction, 0.1, -1902 local openfunction_authz_schema { service_token {type string} } local function request_processor(conf, ctx, params) local headers params.headers or {} -- setting authorization headers if authorization.service_token exists if conf.authorization and conf.authorization.service_token then headers[authorization] Basic .. ngx_encode_base64(conf.authorization.service_token) end params.headers headers end return require(apisix.plugins.serverless.generic-upstream)(plugin_name, plugin_version, priority, request_processor, openfunction_authz_schema)这个实现有两个关键信息插件优先级为-1902属于较低优先级在请求处理链路的较后阶段执行避免干扰其他高优先级插件的鉴权、改写等逻辑。request_processor回调当配置了authorization.service_token时会把xxx:xxx格式的 token 做 Base64 编码拼成Basic xxx:xxx写入转发请求的authorization头从而支持 OpenFunction 函数入口的 Basic Auth 认证。采用同样工厂模式实现的还有 aws-lambda.lua 和 azure-functions.lua它们只是各自实现了不同的认证头注入逻辑印证了 APISIX 通过工厂函数 请求处理器回调复用无服务器集成能力的架构设计。属性说明插件属性定义在通用工厂generic-upstream的 schema 中见 apisix/plugins/serverless/generic-upstream.luaopenfunction通过openfunction_authz_schema额外注入了authorization.service_token。完整属性如下名称类型必选项默认值有效值描述function_uristring是OpenFunction function 的 URI例如https://localhost:30858/default/function-samplessl_verifyboolean否true设置为true时执行 SSL 验证authorizationobject否访问 OpenFunction 函数的授权凭证authorization.service_tokenstring否OpenFunction service token格式为xxx:xxx支持函数入口的 Basic Auth 认证方式timeoutinteger否3000 ms[100,...] msOpenFunction action 与 HTTP 调用超时时间以毫秒为单位keepaliveboolean否true设置为true时保持连接活动状态以便复用keepalive_timeoutinteger否60000 ms[1000,...] ms连接空闲时保持活动状态的时间以毫秒为单位keepalive_poolinteger否5[1,...]连接断开之前可接收的最大请求数这些取值约束timeout 100、keepalive_timeout 1000、keepalive_pool 1直接来自 generic-upstream.lua 中的 schema 定义配置不合法时 APISIX 会直接拒绝。关于 timeout 的重要提示timeout字段同时规定了 OpenFunction function 的最大执行时间以及 APISIX 中 HTTP 客户端的请求超时时间。由于 OpenFunction 函数调用可能需要较长时间来拉取容器镜像和启动容器如果timeout设置过小会导致大量请求失败。生产环境建议根据函数冷启动耗时适当调大该值。前提条件使用openfunction插件前需要先部署 OpenFunction 平台并通过以下命令生成推送函数容器镜像所需的仓库密钥Docker Hub 或 Quay.io 等REGISTRY_SERVERhttps://index.docker.io/v1/ REGISTRY_USERyour_registry_user REGISTRY_PASSWORDyour_registry_password kubectl create secret docker-registry push-secret \ --docker-server$REGISTRY_SERVER \ --docker-username$REGISTRY_USER \ --docker-password$REGISTRY_PASSWORD同时请确保当前环境中已安装对应版本的 Kubernetes 集群。函数本身的创建与构建可参考 OpenFunction 官方示例仓库。启用插件通过 Admin API 在指定路由上启用openfunction插件。首先从 conf/config.yaml 中获取admin_key并存入环境变量admin_key$(yq .deployment.admin.admin_key[0].key conf/config.yaml | sed s///g)注意conf/config.yaml中deployment.admin.admin_key若未显式配置 keyAPISIX 启动时会自动生成并回写因此请确保该文件中的 key 已实际生效。然后创建路由curl http://127.0.0.1:9180/apisix/admin/routes/1 -H X-API-KEY: $admin_key -X PUT -d { uri: /hello, plugins: { openfunction: { function_uri: http://localhost:3233/default/function-sample/test, authorization: { service_token: test:test } } } }配置说明function_uri指向 OpenFunction 暴露的函数端点NodePort 或网关地址authorization.service_token传入test:test后插件会在转发请求时自动注入Authorization: Basic dGVzdDp0ZXN0头。测试插件使用 curl 发起请求curl -i http://127.0.0.1:9080/hello -X POST -dtest正常返回结果hello, test!源码层的请求转发机制从 generic-upstream.lua 的 access 阶段实现可以看到请求转发的完整链路读取 URI 参数与请求头并读取请求体读取失败时返回400解析function_uri将请求方法、请求体、查询参数、请求头以及host头组装为转发参数应用request_processor回调注入认证头即上述 Basic Auth 逻辑通过resty.http发起子请求使用timeout设置超时转发失败时记录错误日志并返回503将上游响应头与状态码、响应体透传回客户端对于 HTTP/2 请求会按 RFC 7540 清理Connection、Keep-Alive等连接相关头。仓库中的测试用例 t/plugin/openfunction.t 覆盖了完整的行为验证包括schema 校验TEST 1-3function_uri缺失时报property function_uri is required类型错误时报wrong type: expected string, got numberGET/POST 转发TEST 5、7GET /hello返回Hello, function-sample!POST /hello携带test请求体返回Hello, test!service_token 认证TEST 8-9配置test:test后上游收到的认证头为[Basic dGVzdDp0ZXN0]即test:test的 Base64 编码用户自定义 Authorization 头不被覆盖TEST 11当客户端自行携带authorization: user-token-xxx时插件保留原值上游收到[user-token-xxx]上游 404 透传TEST 13函数不存在时返回404 not found。配置路径转发openfunction插件还支持 URL 路径转发请求路径中通配符*匹配的部分会被追加到function_uri之后从而将请求代理到上游的 OpenFunction API 端点。重要路由上配置的uri必须以*结尾此功能才能正常工作。APISIX 路由是严格匹配的*表示此 URI 的任何子路径都将匹配到同一路由。示例配置curl http://127.0.0.1:9180/apisix/admin/routes/1 -H X-API-KEY: $admin_key -X PUT -d { uri: /hello/*, plugins: { openfunction: { function_uri: http://localhost:3233/default/function-sample, authorization: { service_token: test:test } } } }此时对路径/hello/123的请求将调用对应的函数并转发添加的路径curl http://127.0.0.1:9080/hello/123返回结果Hello, 123!其底层实现在 generic-upstream.lua插件通过ctx.curr_req_matched[:ext]取得通配符匹配到的子路径并处理首尾斜杠后拼接到function_uri的 path 上。对应测试用例见 t/plugin/openfunction.t 中的 TEST 14-15路由/hello/*下请求GET /hello/openfunction返回Hello, openfunction!。删除插件需要禁用openfunction插件时删除路由配置中对应的 JSON 配置即可APISIX 会自动重新加载相关配置无需重启服务curl http://127.0.0.1:9180/apisix/admin/routes/1 -H X-API-KEY: $admin_key -X PUT -d { methods: [GET], uri: /hello, upstream: { type: roundrobin, nodes: { 127.0.0.1:1980: 1 } } }恢复后的路由将不再触发 OpenFunction 转发而是按普通 upstream 直接代理到127.0.0.1:1980。使用建议合理设置 timeout函数冷启动拉镜像、起容器耗时较长建议按函数实际启动时间设置超时避免请求集中失败优先启用 keepalive默认keepalive: true可复用与函数服务之间的连接显著降低频繁建连开销并可通过keepalive_pool控制连接池大小善用路径转发配合uri: /xxx/*可以将同一函数的多个子路径统一收敛到一个路由减少路由数量安全加固若函数入口启用了认证务必配置authorization.service_tokenHTTPS 场景下默认开启的ssl_verify可防止中间人攻击仅在函数端证书不受信时才考虑关闭。【免费下载链接】apisixThe Cloud-Native API Gateway项目地址: https://gitcode.com/GitHub_Trending/ap/apisix创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门