2021-02-26 21:40:08 +08:00
|
|
|
|
---
|
|
|
|
|
title: serverless
|
|
|
|
|
---
|
|
|
|
|
|
2019-10-31 09:27:28 +08:00
|
|
|
|
<!--
|
|
|
|
|
#
|
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
#
|
|
|
|
|
-->
|
|
|
|
|
|
2019-08-26 13:10:15 +08:00
|
|
|
|
serverless 的插件有两个,分别是 `serverless-pre-function` 和 `serverless-post-function`,
|
|
|
|
|
前者会在指定阶段的最开始运行,后者是在指定阶段的最后运行。
|
|
|
|
|
|
|
|
|
|
这两个插件接收的参数都是一样的。
|
|
|
|
|
|
2020-04-28 15:53:29 +08:00
|
|
|
|
## Parameters
|
|
|
|
|
|
2020-09-23 08:11:27 +08:00
|
|
|
|
| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
|
|
|
|
|
| --------- | ------------- | -------- | ---------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ |
|
|
|
|
|
| phase | string | 可选 | ["access"] | ["rewrite", "access", "header_filter", "body_filter", "log", "balancer"] | |
|
|
|
|
|
| functions | array[string] | 必须 | | | 指定运行的函数列表,是数组类型,里面可以包含一个函数,也可以是多个函数,按照先后顺序执行。 |
|
2020-04-28 15:53:29 +08:00
|
|
|
|
|
2019-08-26 13:10:15 +08:00
|
|
|
|
需要注意的是,这里只接受函数,而不接受其他类型的 Lua 代码。比如匿名函数是合法的:
|
2020-04-28 15:53:29 +08:00
|
|
|
|
|
|
|
|
|
```lua
|
2019-08-26 13:10:15 +08:00
|
|
|
|
return function()
|
|
|
|
|
ngx.log(ngx.ERR, 'one')
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
闭包也是合法的:
|
2020-04-28 15:53:29 +08:00
|
|
|
|
|
|
|
|
|
```lua
|
2019-08-26 13:10:15 +08:00
|
|
|
|
local count = 1
|
|
|
|
|
return function()
|
|
|
|
|
count = count + 1
|
|
|
|
|
ngx.say(count)
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
但不是函数类型的代码就是非法的:
|
2020-04-28 15:53:29 +08:00
|
|
|
|
|
|
|
|
|
```lua
|
2019-08-26 13:10:15 +08:00
|
|
|
|
local count = 1
|
|
|
|
|
ngx.say(count)
|
|
|
|
|
```
|
|
|
|
|
|
2021-04-08 18:29:32 +08:00
|
|
|
|
从 `v2.6` 版本开始,我们会把 `conf` 和 `ctx` 作为头两个参数传递给 serverless 函数,就跟一般的插件一样。
|
|
|
|
|
|
2020-04-28 15:53:29 +08:00
|
|
|
|
## 示例
|
|
|
|
|
|
|
|
|
|
### 启动插件
|
2019-08-26 13:10:15 +08:00
|
|
|
|
|
|
|
|
|
下面是一个示例,在指定的 route 上开启了 serverless 插件:
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +08:00
|
|
|
|
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2019-08-26 13:10:15 +08:00
|
|
|
|
{
|
|
|
|
|
"uri": "/index.html",
|
|
|
|
|
"plugins": {
|
2019-12-30 20:28:36 +08:00
|
|
|
|
"serverless-pre-function": {
|
|
|
|
|
"phase": "rewrite",
|
|
|
|
|
"functions" : ["return function() ngx.log(ngx.ERR, \"serverless pre function\"); end"]
|
2021-04-08 18:29:32 +08:00
|
|
|
|
},
|
|
|
|
|
"serverless-post-function": {
|
|
|
|
|
"phase": "rewrite",
|
|
|
|
|
"functions" : ["return function(conf, ctx) ngx.log(ngx.ERR, \"match uri \", ctx.curr_req_matched and ctx.curr_req_matched._path); end"]
|
|
|
|
|
},
|
2019-08-26 13:10:15 +08:00
|
|
|
|
},
|
|
|
|
|
"upstream": {
|
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"39.97.63.215:80": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-28 15:53:29 +08:00
|
|
|
|
### 测试插件
|
|
|
|
|
|
2019-08-26 13:10:15 +08:00
|
|
|
|
使用 curl 访问:
|
2020-04-28 15:53:29 +08:00
|
|
|
|
|
2019-08-26 13:10:15 +08:00
|
|
|
|
```shell
|
|
|
|
|
curl -i http://127.0.0.1:9080/index.html
|
|
|
|
|
```
|
|
|
|
|
|
2021-04-08 18:29:32 +08:00
|
|
|
|
然后你在 error.log 日志中就会发现 `serverless pre function` 和 `match uri /index.html` 两个 error 级别的日志,
|
2019-08-26 13:10:15 +08:00
|
|
|
|
表示指定的函数已经生效。
|
|
|
|
|
|
2020-04-28 15:53:29 +08:00
|
|
|
|
### 移除插件
|
|
|
|
|
|
2019-08-26 13:10:15 +08:00
|
|
|
|
当你想去掉插件的时候,很简单,在插件的配置中把对应的 json 配置删除即可,无须重启服务,即刻生效:
|
|
|
|
|
|
|
|
|
|
```shell
|
2020-11-28 19:05:14 +08:00
|
|
|
|
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
|
2019-08-26 13:10:15 +08:00
|
|
|
|
{
|
|
|
|
|
"methods": ["GET"],
|
|
|
|
|
"uri": "/index.html",
|
|
|
|
|
"upstream": {
|
|
|
|
|
"type": "roundrobin",
|
|
|
|
|
"nodes": {
|
|
|
|
|
"39.97.63.215:80": 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}'
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
现在就已经移除了 serverless 插件了。其他插件的开启和移除也是同样的方法。
|