kong网关插件开发初探

本文涉及的产品
Serverless 应用引擎 SAE,800核*时 1600GiB*时
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
EMR Serverless StarRocks,5000CU*H 48000GB*H
简介: kong插件开发初探
使用平台:Ubuntu 20.04

使用软件:kong-enterprise-edition-3.3.1.0.amd64.deb

plugin模板:git clone https://github.com/Kong/kong-plugin.git

参考文档:https://docs.konghq.com/gateway/3.3.x/plugin-development/

参考文章:https: //zhuanlan.zhihu.com/p/52402537、https://segmentfault.com/a/1190000039683513

以下步骤假设你已经了解到如何安装kong网关和数据库,如何使用kong网关进行代理转发

1.plugin文件结构

  • 基本结构

    simple-plugin
    ├── handler.lua
    └── schema.lua
    • handler.lua 插件的核心,可以定义一些在请求的各个声明周期运行的函数。
    • schema.lua 插件的一些配置信息。比如可以配置哪些字段,默认值,校验之类的
  • 高级结构

如果需要更加深度的和 kong 进行集成,比如在 kong 的数据库中有自己的表,在 Admin API 中有自己的管理断点等等。还有一些其他可以定义的文件。

complete-plugin
├── api.lua
├── daos.lua
├── handler.lua
├── migrations
│   ├── cassandra.lua
│   └── postgres.lua
└── schema.lua
api.lua No Defines a list of endpoints to be available in the Admin API to interact with the custom entities handled by your plugin.
daos.lua No Defines a list of DAOs (Database Access Objects) that are abstractions of custom entities needed by your plugin and stored in the data store.
handler.lua Yes An interface to implement. Each function is to be run by Kong at the desired moment in the lifecycle of a request / connection.
migrations/*.lua No The database migrations (e.g. creation of tables). Migrations are only necessary when your plugin has to store custom entities in the database and interact with them through one of the DAOs defined by daos.lua.
schema.lua Yes Holds the schema of your plugin’s configuration, so that the user can only enter valid configuration values.
  • 模块名称

"kong.plugins.<plugin_name>.<module_name>"

2.插件开发

我们开发一个简单的插件 Demo,对每个请求添加一个 uuid 叫 my-uuid,添加的 header 我们可以自己在插件的配置中定义(Kong 实际已经自带了一个类似的插件

  • 下载kong的插件模板

    git clone https://github.com/Kong/kong-plugin.git
    #以上步骤是一个参考,这里我们徒手开发
    #创建插件目录my-uuid
    mkdir my-uuid
    #cd my-uuid,创建并编辑handler.lua,内容如下
    

local uuid = require "kong.tools.utils".uuid

local MyUUIDHandler = {}

MyUUIDHandler.PRIORITY = 1
MyUUIDHandler.VERSION = "0.1.0"


function MyUUIDHandler:access(conf)
  -- Set header for upstream
  local trace_id = kong.request.get_header(conf.header_name)
  if not trace_id or trace_id == "" then
    -- Generate the header value
    trace_id = uuid()
    if trace_id then
      kong.service.request.set_header(conf.header_name, trace_id)
    end
  end

  kong.ctx.plugin.trace_id = trace_id
end


function MyUUIDHandler:header_filter(conf)
  local trace_id = kong.ctx.plugin.trace_id or
                         kong.request.get_header(conf.header_name)

  if not trace_id or trace_id == "" then
    trace_id = uuid()
  end

  kong.response.set_header(conf.header_name, trace_id)
end


return MyUUIDHandler
#创建并编辑schema.lua
local typedefs = require "kong.db.schema.typedefs"

return {
    name = "my-uuid",
    fields = {
        { 
            consumer = typedefs.no_consumer 
        },
        {
            config = {
                type = "record",
                fields = {
                    { header_name = { type = "string", required = true }, },
                },
            },
        },
    },
}

## 3.安装插件

  • 新建rockspec文件,Lua模块的包管理器 [LuaRocks]

    #cd ../ 创建kong-plugin-my-uuid-0.1.0-1.rockspec
    package = "kong-plugin-my-uuid"
    
    version = "0.1.0-1" 
    local pluginName = package:match("^kong%-plugin%-(.+)$")
    
    supported_platforms = {"linux", "macosx"}
    source = {
      url = "https://gitxxxxxx.com/xxxx/kong-plugins",
      tag = "0.1.0"
    }
    
    description = {
      summary = "Add uuid in request/response header"
    }
    
    dependencies = {}
    
    build = {
      type = "builtin",
      modules = {
        ["kong.plugins."..pluginName..".handler"] = pluginName.."/handler.lua",
        ["kong.plugins."..pluginName..".schema"] = pluginName.."/schema.lua",
      }
    }

    使用命令sudo luarocks make编译安装插件,编译完显示"kong-plugin-my-uuid 0.1.0-1 is now installed in /usr/local "

4.加载插件

  • 编辑kong.conf 文件,找到plugins参数,修改为plugins = bundled,my-uuid
  • 重启kong网关sudo kong restart
  • 检查my-uuid插件是否加载curl http://localhost:8001|grep my-uuid
  • 验证插件

    启用插件

    首先我们新建一个API 服务叫 mockbin,上游地址是 http://mockbin.org/bin/xxxxx-79b5-4eb9-9558-c3bc57b7bf48。网关上绑定其路由为 /mockbin/v1
    这样我们访问 http://${网关 IP}:${端口}/mockbin/v1 就可以直接被代理到上游的服务 http://mockbin.org//bin/xxxxx-79b5-4eb9-9558-c3bc57b7bf48 上。
    my-uuid 插件绑定到该服务中,并且设置 uuid 的 header 名称为 my-trace-id

image.png

请求网关地址 http://${网关 IP}:${端口}/mockbin/v1/instances
image.png

5. 参考

这里kong使用了lua的框架Lapis,参考 https://leafo.net/lapis/

6. konga的显示设置

/usr/local/share/lua/5.1/kong/constants.lua 修改此文件的主要目的是为了让自定义插件在konga 界面上显示,如果不用ui管理界面可以忽略。

local plugins = {
  "jwt",
  "acl",
  "correlation-id",
  "cors",
  "oauth2",
  "tcp-log",
  "udp-log",
  "file-log",
  "http-log",
  "key-auth",
  "hmac-auth",
  "basic-auth",
  "ip-restriction",
  "request-transformer",
  "response-transformer",
  "request-size-limiting",
  "rate-limiting",
  "response-ratelimiting",
  "syslog",
  "loggly",
  "datadog",
  "ldap-auth",
  "statsd",
  "bot-detection",
  "aws-lambda",
  "request-termination",
  "prometheus",
  "proxy-cache",
  "session",
  "acme",
  "grpc-gateway",
  "grpc-web",
  "pre-function",
  "post-function",
  "azure-functions",
  "zipkin",
  "demo", ## 此项为我的自定义插件
}
相关文章
|
3月前
|
负载均衡 应用服务中间件 API
Nginx、Kong、Apisix、Gateway网关比较
Nginx、Kong、Apisix、Gateway网关比较
519 1
Nginx、Kong、Apisix、Gateway网关比较
|
3月前
|
Prometheus 网络协议 JavaScript
api 网关 kong 数据库记录请求响应报文
Kong的tcp-log-with-body插件是一个高效的工具,它能够转发Kong处理的请求和响应。这个插件非常适用于需要详细记录API请求和响应信息的情景,尤其是在调试和排查问题时。
125 0
api 网关 kong 数据库记录请求响应报文
|
10月前
|
JSON 应用服务中间件 nginx
如何修改kong网关access.log的日志格式
有需要需要调整kong网关的日志格式,调整日志输出内容,由于原来使用docker部署kong网关,并且使用了环境变量指定了网关运行的参数,这里在以下介绍的方式还需要修改容器的环境变量,但是也提供了一条思路,就是部署网关的时候,统一使用kong.conf进行配置
500 0
|
10月前
|
存储 安全 PHP
【100天精通Python】Day48:Python Web开发_WSGI网络服务器网关接口与使用
【100天精通Python】Day48:Python Web开发_WSGI网络服务器网关接口与使用
80 0
|
1月前
|
应用服务中间件 API 数据库
Docker 安装 KONG 带你玩转 API 网关
**摘要:** 在微服务架构中,API网关Kong作为流行开源选择,提供身份验证、安全和流量控制等功能。通过Docker部署Kong简单高效。步骤包括:创建Docker网络,部署PostgreSQL数据库,初始化Kong数据库,启动Kong容器,并检查运行状态。此外,安装Konga管理界面便于直观管理Kong。使用Docker命令行,逐步设置环境变量和网络连接,即可完成安装。当不再需要时,可清理相关容器和网络。Kong结合Konga,为API管理提供强大且用户友好的解决方案。
82 1
|
11月前
|
NoSQL API Redis
高性能分布式API网关Kong2
高性能分布式API网关Kong2
541 2
|
11月前
|
Java 应用服务中间件 API
高性能分布式API网关Kong1
高性能分布式API网关Kong1
905 2
|
11月前
|
SQL 监控 Cloud Native
基于云原生网关插件实现WAF防护能力
一起体验云原生网关开箱即用,支持热插拔的WAF防护能力,同时您将掌握云原生网关上的插件使用方式
427 0
|
12月前
|
监控 Java API
基于Spring-Cloud-Gateway开发API网关的思路
基于Spring-Cloud-Gateway开发API网关的思路
|
1月前
|
监控 负载均衡 Java
深入理解Spring Cloud中的服务网关
深入理解Spring Cloud中的服务网关