背景
kong的插件开发是基于lua开发的,原理是基于lua-nginx-module的机制。kong的机制其实同openresty一样,都是利用lua脚本动态控制路由规则。
所以增加kong自定义插件需要两个位置:
插件目录位置:/usr/local/share/lua/5.1/kong/plugins/
kong配置文件位置:/etc/kong/
开发HelloWorld
首先在插件目录里增加一个文件夹:hello-world
然后在文件夹里创建两个文件:handler.lua和schema.lua
handler.lua 是插件逻辑文件,里面放具体的过滤规则
local BasePlugin = require "kong.plugins.base_plugin"
local http = require("socket.http")
local ltn12 = require("ltn12")
local CustomHandler = BasePlugin:extend()
local resultAns = ">>插件开始运行了\n"
CustomHandler.VERSION = "1.0.0"
--设置执行的优先级,Kong 将按照插件的优先级来确定其执行顺序(越大越优先)
CustomHandler.PRIORITY = 10
-- 你插件handler的构造函数。
-- 如果要扩展Base Plugin handler,它的唯一作用就是用名称实例化自己。
-- 该名称是您的插件名称,同时它将打印在日志中
function CustomHandler:new()
CustomHandler.super.new(self, "hello-world")
end
function CustomHandler:init_worker() -- 最终执行父实现 (并将记录您的插件正在进入此上下文)
CustomHandler.super.init_worker(self) -- 实现任何自定义逻辑
end
function CustomHandler:preread(config)
CustomHandler.super.preread(self) -- Implement any custom logic here
end
function CustomHandler:certificate(config)
CustomHandler.super.certificate(self) -- Implement any custom logic here
end
function CustomHandler:rewrite(config)
CustomHandler.super.rewrite(self) -- Implement any custom logic here
end
function CustomHandler:access(config)
CustomHandler.super.access(self) -- Implement any custom logic here
resultAns = resultAns .. ">>>>>>>执行:access阶段开始\n输出嵌入的内容(请求在还未到达上游服务器):\n"
resultAns = resultAns .. "kong.version:\t" .. kong.version .. "\n"
resultAns = resultAns .. "kong.client.get_ip():\t" .. kong.client.get_ip() .. "\n"
resultAns = resultAns .. "kong.request.get_scheme():\t" .. kong.request.get_scheme() .. "\n"
resultAns = resultAns .. "kong.request.get_host():\t" .. kong.request.get_host() .. "\n"
resultAns = resultAns .. "kong.request.get_port()\t:" .. kong.request.get_port() .. "\n"
resultAns = resultAns .. "kong.request.get_http_version():\t" .. kong.request.get_http_version() .. "\n"
resultAns = resultAns .. "kong.request.get_method():\t" .. kong.request.get_method() .. "\n"
resultAns = resultAns .. "kong.request.get_path():\t" .. kong.request.get_path() .. "\n"
resultAns = resultAns .. "kong.request.get_header():\t" .. kong.request.get_header("token") .. "\n"
resultAns = resultAns .. "kong.request.get_raw_body():\t" .. kong.request.get_raw_body() .. "\n"
resultAns = resultAns .. "<<<<<<<执行httprequest \n" resultAns = resultAns .. "<<<<<<<执行access阶段结束 \n"
local u = "http://www.baidu.com"
local t = {}
local r, c, h = http.request{url = u, sink = ltn12.sink.table(t)}
kong.log("responsebody:", table.concat(t))
return kong.response.exit(200, resultAns, {["Content-Type"] = "application/json", ["WWW-Authenticate"] = "Basic"})
end
function CustomHandler:header_filter(config)
CustomHandler.super.header_filter(self) -- Implement any custom logic here
end
function CustomHandler:body_filter(config)
CustomHandler.super.body_filter(self) -- Implement any custom logic here
end
function CustomHandler:log(config)
CustomHandler.super.log(self) -- Implement any custom logic here
end
-- 该模块需要返回创建的表以便让Kong 可以执行这些功能。
return CustomHandler
schema.lua 是插件声明文件,里面放插件的参数等申明
local typedefs = require "kong.db.schema.typedefs"
return {
name = "hello-world",
fields = {
{consumer = typedefs.no_consumer},
{config = {type = "record",
fields = { -- 这里的username, 会显示在插件配置页 {
username = {
type = "array",
elements = {type = "string"},
default = {}
}}}}}}}
编辑kong.conf,增加plugins = bundled,hello-world。一般情况下/etc/kong里是没有kong.conf,只有kong.conf.default和kong.logrotate,可以把kong.conf.default拷贝成kong.conf即可
测试运行
配置服务重启服务:docker container restart 24d3ec1fa160
配置服务:
添加路由添加路由:
配置插件配置插件:
发送命令测试:curl -i 10.10.30.70:8000/index -H 'token:test' -H 'Content-Type: application/json' -d '{data:test}'
获得测试结果:HTTP/1.1 200 OK Date: Mon, 26 Jul 2021 10:11:51 GMT Content-Type: application/json Connection: keep-alive WWW-Authenticate: Basic Content-Length: 1032 X-Kong-Response-Latency: 47 Server: kong/2.3.3 >>插件开始运行了 >>>>>>>执行:access阶段开始 输出嵌入的内容(请求在还未到达上游服务器): kong.version: 2.3.3 kong.client.get_ip(): 10.10.30.69 kong.request.get_scheme(): http kong.request.get_host(): 10.10.30.70 kong.request.get_port() :8000 kong.request.get_http_version(): 1.1 kong.request.get_method(): POST kong.request.get_path(): /index kong.request.get_header(): test kong.request.get_raw_body(): {data:test} <<<<<<<执行httprequest <<<<<<<执行access阶段结束 >>>>>>>执行:access阶段开始 输出嵌入的内容(请求在还未到达上游服务器):
网友评论