具有多个后端的Azure API管理 [英] Azure API Management with multiple backends

查看:8
本文介绍了具有多个后端的Azure API管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已设置并正在运行Azure API管理。API调用是对在Azure虚拟机上运行FastAPI的Docker容器进行的。这个后端容器负责根据它收到的查询运行一些AI模型。对于单个用户来说,这一切都很好。

问题是:这些AI模型在容器内的配置文件中定义,并且是特定于用户的。

我希望使用Azure API Management根据用户订阅来路由请求。换句话说,给定订阅,我想知道调用哪个后端(每个后端都是一个容器,在Azure虚拟机上为该特定用户/公司运行特定的AI模型)。

解决此问题的最佳方法是什么?

推荐答案

我发现您可以使用入站策略来指定使用哪个后端,具体取决于用户所属的组。对我答案的引用来自from here

转到您的API管理,选择组并创建与您的用户相关的组。然后将您的用户添加到其各自的组中。

转到您的API,选择入站规则,然后使用如下内容指定您的后端:

<policies>
<inbound>
    <choose>
        <when condition="@(context.User.Groups.Select(g => g.Name).Contains("org1"))">
            <set-backend-service base-url="https://abc-apim.azure-api.net/org1app" />
        </when>
        <when condition="@(context.User.Groups.Select(g => g.Name).Contains("org2"))">
            <set-backend-service base-url="https://abc-apim.azure-api.net/org2app" />
        </when>
        <otherwise>
            <return-response>
                <set-status code="401" reason="Unauthorized" />
                <set-header name="WWW-Authenticate" exists-action="override">
                    <value>Bearer error="Invalid user group"</value>
                </set-header>
            </return-response>
        </otherwise>
    </choose>
    <base />
</inbound>
<backend>
    <base />
</backend>
<outbound>
    <base />
</outbound>
<on-error>
    <base />
</on-error>
</policies>

我认为这种方法不是很好,因为您必须在策略中对后端路由进行硬编码。但它起作用了。

这篇关于具有多个后端的Azure API管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆