余瑜的博客 余瑜的博客
首页
  • 并发
  • 线程池
  • spring
  • maven
  • 其他
  • redis
  • mysql
  • linux
  • zookeeper
  • docker
  • terminal
  • kong插件开发
  • 资料
  • leetCode-简单
  • blog
  • 其他
关于
GitHub (opens new window)
首页
  • 并发
  • 线程池
  • spring
  • maven
  • 其他
  • redis
  • mysql
  • linux
  • zookeeper
  • docker
  • terminal
  • kong插件开发
  • 资料
  • leetCode-简单
  • blog
  • 其他
关于
GitHub (opens new window)
  • linux

  • zookeeper

  • docker

  • terminal

  • kong插件开发

    • 开发环境搭建
    • go注册consul
    • konga配置
    • 插件开发
      • 路径信息及结构
        • 路径信息
        • 插件结构
      • 第一个kong插件
        • 配置信息-schema.lua
        • 程序入口handler
        • 启动
      • 验证
      • 补充
    • 调用c文件
    • 踩坑
  • 运维
  • kong插件开发
余瑜
2021-05-13
目录

插件开发

详细操作本文不错阐述, 有疑问可查看kong插件官方文档 (opens new window)

# 路径信息及结构

# 路径信息

使用brew install kong安装后 kong的路径为 /usr/local/Cellar/kong/2.3.3/share/lua/5.1/kong/plugins , 使用idea打开/usr/local/Cellar/kong/2.3.3/share/lua/5.1/kong 文件, 在 plugins 下新建文件夹并命名为demo-plugin .插件代码卸载该文件 idea插件推荐推荐使用EmmyLua

# 插件结构

demo-plugin
├── api.lua            # 用于扩展Admin API 
├── daos.lua           # 数据访问层
├── handler.lua        # (必需)包含请求的生命周期, 提供接口来实现插件逻辑
├── migrations         # 插件的表结构定义语句
│   ├── cassandra.lua  
│   └── postgres.lua
└── schema.lua         # (必需)插件配置参数定义, 可加入自定义校验函数
1
2
3
4
5
6
7
8

# 第一个kong插件

依据配置信息中配置的checkStr 的值是否等于test来判断是否放行该请求

# 配置信息-schema.lua

文档链接 (opens new window)

return {
  name = "demo",
  fields = {
    {
      config = {
        type = "record",
        fields = {
          {
            checkStr = { type = "string", required = true, default = "test" },
          },
        },
      },
    },
  },
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 程序入口handler

local BasePlugin = require "kong.plugins.base_plugin"
local DemoPlugin = BasePlugin:extend()

DemoPlugin.PRIORITY = 10

function DemoPlugin:new()
  DemoPlugin.super.new(self, "demo-plugin")
end

-- 退出程序
local function exit(msg, data)
  kong.response.add_header("Content-Type", "charset=UTF-8")
  kong.response.add_header("Content-Type", "application/json")
  return kong.response.exit(200, {
    code = -1,
    msg = msg,
    data = data,
  })
end
-- 程序入口
function DemoPlugin:access(conf)
  if conf.checkStr ~= 'test' then
    return exit("插件拦截")
  end
end

return DemoPlugin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 启动

修改 /etc/kong/kong.conf

plugins = bundled,demo-plugin
1

启动kong网关

kong start /etc/kong/kong.conf
1

# 验证

打开http://localhost:1337

路径: ROUTES => 选择上一节创建的路由 => plugins => ADD PLUGIN => orther => demo-plugin => ADD PLUGIN

访问服务进行验证

  1. 设置checkStr为test
curl http://localhost:8000/root8201/query
{
  "code": 200,
  "data": true,
  "msg": "ok"
}
1
2
3
4
5
6
  1. 设置checkStr为test1
 curl http://localhost:8000/root8201/query
{"msg":"插件拦截","code":-1}
1
2

# 补充

日志文件: /usr/local/Cellar/kong/2.3.3/logs

上次更新: 2021/06/09, 11:15:04

← konga配置 调用c文件→

Theme by Vdoing | Copyright © 2018-2022 逆光世间 | 备案号: 京ICP备19016086号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式