如何将 Azure API 管理服务实例部署到多个 Azure 区域How to deploy an Azure API Management service instance to multiple Azure regions

Azure API 管理多区域部署,该部署可使 API 发布者在任意数量的所需 Azure 区域中分配单个 Azure API 管理服务。Azure API Management supports multi-region deployment, which enables API publishers to distribute a single Azure API management service across any number of desired Azure regions. 这有助于减少地理上分散的 API 使用者所感知的请求延迟,并且还改善其中一个区域处于离线状态时的服务可用性。This helps reduce request latency perceived by geographically distributed API consumers and also improves service availability if one region goes offline.

新的 Azure API 管理服务最初只在一个 Azure 区域(主要区域)中包含一个单元A new Azure API Management service initially contains only one unit in a single Azure region, the Primary Region. 可通过 Azure 门户轻松添加其他区域。Additional regions can be easily added through the Azure portal. API 管理网关服务器部署到每个区域,调用流量将路由到最近的网关,以减小延迟。An API Management gateway server is deployed to each region and call traffic will be routed to the closest gateway in terms of latency. 如果一个区域处于离线状态,则传入流量自动重定向到下一个最近的网关。If a region goes offline, the traffic is automatically redirected to the next closest gateway.

Note

Azure API 管理仅复制跨区域的 API 网关组件。Azure API Management replicates only the API gateway component across regions. 服务管理组件仅托管在主要区域中。The service management component is hosted only in the Primary Region. 如果主要区域发生服务中断,则无法向 Azure API 管理服务实例应用配置更改 - 包括设置或策略更新。In case of an outage in the Primary Region, applying configuration changes to an Azure API Management service instance is not possible - including settings or policies updates.

可用性Availability

Important

此功能仅在 API 管理的“高级”层中可用。This feature is available only in the Premium tier of API Management.

将 API 管理服务实例部署到新区域 Deploy an API Management service instance to a new region

Note

如果尚未创建 API 管理服务实例,请参阅创建 API 管理服务实例If you have not yet created an API Management service instance, see Create an API Management service instance.

在 Azure 门户中,导航到 API 管理服务实例的“规模和定价”页。In the Azure portal, navigate to the Scale and pricing page for your API Management service instance.

“缩放”选项卡

若要部署到新的区域,请单击工具栏中的“+ 添加区域”。To deploy to a new region, click on + Add region from the toolbar.

添加区域

从下拉列表中选择位置,并通过滑块为其设置单位数。Select the location from the drop-down list and set the number of units for with the slider.

指定单位

单击“添加”将选择放置在“位置”表中。Click Add to place your selection in the Locations table.

重复此过程,直到配置所有位置,并单击工具栏中的“保存”,启动部署过程。Repeat this process until you have all locations configured and click Save from the toolbar to start the deployment process.

从位置中删除 API 管理服务实例 Delete an API Management service instance from a location

在 Azure 门户中,导航到 API 管理服务实例的“规模和定价”页。In the Azure portal, navigate to the Scale and pricing page for your API Management service instance.

“缩放”选项卡

若要删除位置,请使用表右端的“...”按钮打开上下文菜单。For the location you would like to remove, open the context menu using the ... button at the right end of the table. 选择“删除”选项。Select the Delete option.

确认删除,并单击“保存”应用所做的更改。Confirm the deletion and click Save to apply the changes.

将 API 调用路由到区域后端服务 Route API calls to regional backend services

Azure API 管理只有一个后端服务 URL。Azure API Management features only one backend service URL. 即使不同的区域中存在 Azure API 管理实例,API 网关也仍会将请求转发到只在一个区域中部署的同一后端服务。Even though there are Azure API Management instances in various regions, the API gateway will still forward requests to the same backend service, which is deployed in only one region. 在这种情况下,只有特定于该请求的区域中 Azure API 管理缓存的响应才能提升性能,但访问全球范围的后端时仍可能导致较高的延迟。In this case, the performance gain will come only from responses cached within Azure API Management in a region specific to the request, but contacting the backend across the globe may still cause high latency.

若要充分利用系统的地理分布性,应在 Azure API 管理实例所在的同一区域中部署后端服务。To fully leverage geographical distribution of your system, you should have backend services deployed in the same regions as Azure API Management instances. 然后,可以使用策略和 @(context.Deployment.Region) 属性将流量路由到后端的本地实例。Then, using policies and @(context.Deployment.Region) property, you can route the traffic to local instances of your backend.

  1. 导航到 Azure API 管理实例,然后在左侧菜单中单击“API”。Navigate to your Azure API Management instance and click on APIs from the left menu.

  2. 选择所需的 API。Select your desired API.

  3. 在“入站处理”中的箭头式下拉列表内单击“代码编辑器”。Click Code editor from the arrow dropdown in the Inbound processing.

    API 代码编辑器

  4. 结合使用 set-backendchoose 条件策略,在文件的 <inbound> </inbound> 节中构建适当的路由策略。Use the set-backend combined with conditional choose policies to construct a proper routing policy in the <inbound> </inbound> section of the file.

    例如,以下 XML 文件适用于中国东部和中国北部区域:For example, the below XML file would work for China East and China North regions:

    <policies>
        <inbound>
            <base />
            <choose>
                <when condition="@("China East".Equals(context.Deployment.Region, StringComparison.OrdinalIgnoreCase))">
                    <set-backend-service base-url="http://contoso-us.com/" />
                </when>
                <when condition="@("China North".Equals(context.Deployment.Region, StringComparison.OrdinalIgnoreCase))">
                    <set-backend-service base-url="http://contoso-asia.com/" />
                </when>
                <otherwise>
                    <set-backend-service base-url="http://contoso-other.com/" />
                </otherwise>
            </choose>
        </inbound>
        <backend>
            <base />
        </backend>
        <outbound>
            <base />
        </outbound>
        <on-error>
            <base />
        </on-error>
    </policies>
    

Tip

还可以使用 Azure 流量管理器来配置后端服务的前端,将 API 调用定向到流量管理器中,然后让流量管理器自动解析路由。You may also front your backend services with Azure Traffic Manager, direct the API calls to the Traffic Manager, and let it resolve the routing automatically.

使用 API 管理区域网关的自定义路由 Use custom routing to API Management regional gateways

API 管理根据最低延迟将请求路由到区域网关。API Management routes the requests to a regional gateway based on the lowest latency. 尽管无法在 API 管理中替代此设置,但可以结合自定义路由规则使用自己的流量管理器。Although it is not possible to override this setting in API Management, you can use your own Traffic Manager with custom routing rules.

  1. 创建自己的 Azure 流量管理器Create your own Azure Traffic Manager.
  2. 如果使用自定义域,请将它与流量管理器配合使用,而不要与 API 管理服务配合使用。If you are using a custom domain, use it with the Traffic Manager instead of the API Management service.
  3. 在流量管理器中配置 API 管理区域终结点Configure the API Management regional endpoints in Traffic Manager. 区域终结点遵循 https://<service-name>-<region>-01.regional.azure-api.cn URL 模式,例如 https://contoso-chinaeast-01.regional.azure-api.cnThe regional endpoints follow the URL pattern of https://<service-name>-<region>-01.regional.azure-api.cn, for example https://contoso-chinaeast-01.regional.azure-api.cn.
  4. 在流量管理器中配置 API 管理区域状态终结点Configure the API Management regional status endpoints in Traffic Manager. 区域状态终结点遵循 https://<service-name>-<region>-01.regional.azure-api.cn/status-0123456789abcdef URL 模式,例如 https://contoso-chinaeast-01.regional.azure-api.cn/status-0123456789abcdefThe regional status endpoints follow the URL pattern of https://<service-name>-<region>-01.regional.azure-api.cn/status-0123456789abcdef, for example https://contoso-chinaeast-01.regional.azure-api.cn/status-0123456789abcdef.
  5. 指定流量管理器的路由方法Specify the routing method of the Traffic Manager.