kong网关开发自己的插件

Kong官方文档:

https://docs.konghq.com/0.14.x/plugin-development/

 

lua中文开发手册:

https://www.runoob.com/manual/lua53doc/contents.html#contents

一.代码结构部分介绍

代码结构:

.
├── kong
│   └── plugins
│       └── middleendauth
│           ├── handler.lua           #请求生命周期
│           ├── middleend_client.lua  #逻辑实现部分
│           ├── schema.lua            #插件配置参数定义,或自定义校验函数
│           ├── migrations
│           │   ├── 000_base_qingke-auth.lua    # 数据库结构信息
│           │   └── init.lua                   # 初始化数据结构信息
├── kong-plugin-qingke_auth-0.1.0-1.rockspec
├── README.md
├── ucenter-lua #三方包
│   ├── bin
│   │   └── ucenter_demo.sh
│   ├── conf
│   │   └── nginx.conf
│   ├── lib
│   │   ├── demo
│   │   │   └── demo.lua
│   │   ├── help
│   │   │   ├── aes.lua
│   │   │   ├── base64.lua
│   │   │   ├── common_util.lua
│   │   │   ├── http_headers.lua
│   │   │   ├── http.lua
│   │   │   └── rsa.lua
│   │   └── ucenter
│   │       └── ucenter_client.lua
│   └── readme.md

生命周期函数介绍:

函数名

运行上下文函数

描述

:init_worker

init_worker_by_lua_block

在nginx worker启动时执行

:certificate

ssl_certificate_by_lua_block

在SSL握手时证书阶段执行该句柄

:access

access_by_lua_block

访问上游服务器前执行

:rewrite

rewrite_by_lua_block

请求的rewrite阶段执行

:header_filter

header_filter_by_lua_block

接收上游服务的所有Response header信息

:body_filter

body_filter_by_lua_block

从上游服务接收的响应主体的每个块时执行。 由于响应被流回客户端,因此它可以超过缓冲区大小并按块进行流式传输。 因此,如果响应很大,则会多次调用此方法

:log

log_by_lua_block

响应字节结束后执行

自定义逻辑编写可以如下:

-- Grab pluginname from module name
local plugin_name = ({...})[1]:match("^kong%.plugins%.([^%.]+)")

-- load the base plugin object and create a subclass
local TALMiddleAuth = require("kong.plugins.base_plugin"):extend()

-- constructor
function TALMiddleAuth:new()
  TALMiddleAuth.super.new(self, plugin_name)
  -- do initialization here, runs in the 'init_by_lua_block', before worker processes are forked
end

-- handles more initialization, but AFTER the worker process has been forked/created.
-- It runs in the 'init_worker_by_lua_block'
function TALMiddleAuth:init_worker()
  TALMiddleAuth.super.init_worker(self)
  -- 自定义代码编写
end 

-- runs in the ssl_certificate_by_lua_block handler
function TALMiddleAuth:certificate(plugin_conf)
  TALMiddleAuth.super.certificate(self)
  -- 自定义代码编写
end 

-- runs in the 'rewrite_by_lua_block' (from version 0.10.2+)
-- IMPORTANT: during the `rewrite` phase neither the `api` nor the `consumer` will have
-- been identified, hence this handler will only be executed if the plugin is
-- configured as a global plugin!
function TALMiddleAuth:rewrite(plugin_conf)
  TALMiddleAuth.super.rewrite(self)
  -- 自定义代码编写
end 

-- runs in the 'access_by_lua_block'
function TALMiddleAuth:access(plugin_conf)
    TALMiddleAuth.super.access(self)
    -- 自定义代码编写
end

-- runs in the 'header_filter_by_lua_block'
function TALMiddleAuth:header_filter(plugin_conf)
  TALMiddleAuth.super.header_filter(self)
  -- 自定义代码编写
end

-- runs in the 'body_filter_by_lua_block'
function TALMiddleAuth:body_filter(plugin_conf)
  TALMiddleAuth.super.body_filter(self)
  -- 自定义代码编写
end 

-- runs in the 'log_by_lua_block'
function TALMiddleAuth:log(plugin_conf)
  TALMiddleAuth.super.log(self)
  -- 自定义代码编写
end 

-- 运行级别
TALMiddleAuth.PRIORITY = 1000

-- return our plugin object
return TALMiddleAuth

 

二.添加kong自定义插件配置

kong自定义插件目录位置:

配置kong自定义插件

#vim /etc/kong/kong.conf

plugins = bundled,middleendauth
lua_package_path = ./?.lua;./?/init.lua;/usr/local/qingke_tal_auth/?.lua;/usr/local/qingke_tal_auth/ucenter-lua/lib/ucenter/?.lua;/usr/local/qingke_tal_auth/ucenter-lua/lib/demo/?.lua;/usr/local/qingke_tal_auth/ucenter-lua/lib/help/?.lua;

重启或启动kong服务

重启动

/usr/local/bin/kong restart

启动

/usr/local/bin/kong start

三.添加kong路由服务

添加一个转发服务名称叫middleendauth-service,域名为www.zhaoyutest.cn

#curl -i -X POST --url http://127.0.0.1:8001/services/ --data 'name=middleendauth-service' --data 'url=http://www.zhaoyutest.cn/'

www.zhaoyutest.cn 可以换成自己的代理域名

 

在middleendauth-service服务下添加一个路由 /apptest

curl -i -X POST --url http://127.0.0.1:8001/services/middleendauth-service/routes  --data 'paths[]=/apptest'

 

在middleendauth-service服务下添加自定义插件

curl -i -X POST --url http://127.0.0.1:8001/services/middleendauth-service/plugins/ --data 'name=middleendauth'

测试自定义插件

curl -i -X GET  --url http://127.0.0.1:8000/middleendauth-service

以上部分也可以通过konga去管理;

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值