apisix icon indicating copy to clipboard operation
apisix copied to clipboard

help request: What is the best way to dynamically route a cluster by token in customer plugin?

Open xBoo opened this issue 8 months ago • 3 comments

Description

i got tenantid in jwt token, and i want to use this id to route the cluster, how can i do it ?

local jwt_obj = jwt:verify(conf.jwt_secret, token)
    if jwt_obj.valid and jwt_obj.verified then
        local tid = jwt_obj.payload.tid
        if tid ~= nil then
            local red, err = redis.new(conf)
            if not red then
                return 502, err
            end

            local domain, redis_err = red:get("tid-" .. tid)
            if not domain or domain == nil or (type(domain) == "userdata" and tostring(domain) == "userdata: NULL") then
                return 502, redis_err
            else
               -- Here I do not know,how can i set the  cluster domain?use the upstream id is the best way?
               --like this?  ctx.upstream_id = "512895159801742021"
                core.request.set_header(ctx, "x-cluster", domain)
            end
        else
            -- todo: add no token handle
            core.log.warn("tid is nil, set default cluster")
        end
    else
        core.log.warn("jwt valid error:" .. core.json.encode(jwt_obj))
        return 502, jwt_obj.reason
    end

Environment

  • APISIX version (run 3.9.0):
  • Operating system (run uname -a):
  • OpenResty / Nginx version (run openresty -V or nginx -V):
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):

xBoo avatar Jun 07 '24 09:06 xBoo