目前,每当上游服务关闭时,孔令辉都会抛出“{”message“:”环均衡器获取对端失败“}”
我正在尝试创建一个自定义插件来检测连接超时,并向客户端返回自定义消息,我现在面临的拦截器是在我的自定义插件中编写lua代码来检测超时。我试过使用
if(kong.response.get_source() == "error")但这似乎也不能检测到超时。
有没有人知道当我写一个自定义的kong插件时,我应该做些什么来检测连接超时?
发布于 2021-05-10 21:59:39
对不起,我没听懂你的话。因为我不知道你的函数会返回什么
如果kong.response.get_source()返回字符串{"message":"failure to get a peer from the ring-balancer"}
您需要使用JSON库来解析它。
local json = require("json")
local res = json.deceode(kong.response.get_source())
if res.message == "xxx" then
end但是你的message有点长。您可以向JSON字符串添加一个新的key-value来表示状态。
例如:"{"status":"error","message":"failure to get a peer from the ring-balancer"}"
然后你可以像这样写代码。
local json = require("json")
local res = json.deceode(kong.response.get_source())
if res.status == "error" then
end我刚看了Kong API Docs。
我发现kong.response.get_source()的返回值是HTTP状态码,比如200/500。
您可以尝试print(kong.response.get_source())并查看结果。
https://stackoverflow.com/questions/67467402
复制相似问题