Kong 2.8.0至3.x自定义插件迁移报错求助
Kong 2.8.0 迁移至 3.0.0 自定义插件 Schema 问题解决
问题分析
你遇到的两个错误都是因为 Kong 3.0.0 对插件 Schema 的格式要求发生了变化,与 2.x 版本的 legacy Schema 不兼容导致的:
第一个错误原因
[error] 1#0: init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:560: error loading plugin schemas: on plugin 'file-log-extended': [postgres] 2 schema violations (fields: expected an array; name: field required for entity check)
- Kong 3.0 要求所有插件 Schema 必须显式声明
name字段,指定插件的唯一标识 - 2.x 版本中
fields是键值对形式的 table,而 3.0 对 legacy Schema 的fields要求是数组格式(每个配置项是一个独立的键值对 table)
第二个错误原因
[error] 1#0: init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:543: error loading plugin schemas: on plugin 'file-log-extended': failed converting legacy schema for file-log-extended: unknown legacy field attribute: "config"
你错误地在 legacy Schema 中使用了 config 包裹字段——config 是 Kong 3.0 新引入的 Schema 格式(用于声明式配置)的结构,legacy Schema 不支持这个属性,因此被判定为非法字段。
解决方案
根据你的需求,有两种修改方式:
方式一:兼容 Legacy 模式(无需修改插件业务代码)
保持插件沿用 2.x 的逻辑,只调整 Schema 格式以适配 3.0:
local typedefs = require "kong.db.schema.typedefs" local pl_utils = require "pl.utils" return { name = "file-log-extended", -- 必须添加插件名称 fields = { -- 改为数组格式,每个配置项单独作为一个元素 { path = { required = true, type = "string"} }, { log_bodies = { type = "boolean", default = true } } } }
方式二:升级为 Kong 3.0 新 Schema 格式(需同步修改插件代码)
如果要完全适配 3.0 的新特性,使用 config 结构,同时需要调整插件 handler 中获取配置的逻辑:
修改后的 Schema:
local typedefs = require "kong.db.schema.typedefs" local pl_utils = require "pl.utils" return { name = "file-log-extended", config = { type = "record", fields = { { path = { required = true, type = "string"} }, { log_bodies = { type = "boolean", default = true } } } } }
同步修改插件 handler(示例):
原来的代码中获取配置可能是:
local path = self.conf.path
需要改为:
local path = self.config.path
内容的提问来源于stack exchange,提问作者mgu
相关产品推荐
相关产品推荐

