You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache APISIX能否基于URL参数/路径动态路由至不同后端服务?

Apache APISIX 多租户动态路由实现方案

完全可以实现你描述的需求,APISIX提供了多种灵活的机制来支持运行时动态获取租户-后端服务映射,并缓存结果以优化性能,以下是具体实现方式:

1. 基于内置Lua函数+缓存的轻量方案

使用serverless-pre-function插件编写Lua逻辑,在请求处理阶段完成:

  • 从请求参数(如tenantId)提取租户标识
  • 先检查APISIX内置缓存是否存在该租户的后端映射
  • 缓存未命中时,调用自研映射服务获取地址并写入缓存
  • 将获取到的后端地址设置为当前请求的上游

示例Lua代码片段(可直接配置到路由的serverless-pre-function中):

local core = require("apisix.core")
local http = require("resty.http")
local cache = require("apisix.cache")

-- 获取租户对应后端服务的函数
local function get_tenant_backend(tenant_id)
    local cache_key = "tenant_backend:" .. tenant_id
    -- 优先读取缓存
    local backend, err = cache.get(cache_key)
    if backend then return backend end

    -- 调用自研映射服务
    local httpc = http.new()
    local res, err = httpc:request_uri("http://your-mapping-service/tenant/" .. tenant_id, {
        method = "GET",
        keepalive_timeout = 60000,
        keepalive_pool = 10
    })

    if not res or res.status ~= 200 then
        core.log.error("获取租户", tenant_id, "后端失败:", err)
        return nil
    end

    -- 缓存结果,设置300秒过期时间
    backend = res.body
    cache.set(cache_key, backend, 300)
    return backend
end

-- 请求改写逻辑
local function rewrite(conf, ctx)
    local tenant_id = core.request.get_uri_args(ctx)["tenantId"]
    if not tenant_id then
        return 400, {message = "缺少tenantId参数"}
    end

    local backend = get_tenant_backend(tenant_id)
    if not backend then
        return 500, {message = "无法获取后端服务地址"}
    end

    -- 设置上游节点
    ctx.upstream_nodes = { {host = backend, port = 80} }
end

return {rewrite = rewrite}

2. 外部插件扩展方案

如果你的映射逻辑复杂,或偏好使用Go/Python等非Lua语言开发,可以使用ext-plugin-pre-req插件:

  • 编写外部插件,在插件中实现调用自研服务、缓存映射的逻辑
  • 插件运行时动态修改请求的上游配置
  • 这种方式更适合团队已有技术栈的复用,扩展性更强

3. 动态配置更新方案(适合低频变更场景)

如果租户-后端映射变更不频繁,也可以通过APISIX Admin API实时更新route和service配置,将不同租户的请求路由到对应后端。但这种方式不如前两种灵活,无法实现运行时动态获取映射。

内容的提问来源于stack exchange,提问作者Amit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 00:22:37