如何将 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 supported Azure regions. 多区域功能有助于减少地理上分散的 API 使用者所感知的请求延迟,并且还可改善其中一个区域处于离线状态时的服务可用性。Multi-region feature helps reduce request latency perceived by geographically distributed API consumers and 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. 可向主要区域或次要区域添加更多单元。Additional units can be added to the Primary or Secondary regions. API 管理网关组件将部署到每个选定的主要区域和次要区域。An API Management gateway component is deployed to every selected Primary and Secondary region. 传入的 API 请求将自动定向到最近的区域。Incoming API requests are automatically directed to the closest region. 如果某个区域脱机,API 请求将自动路由到与发生故障的区域最靠近的下一个网关。If a region goes offline, the API requests will be automatically routed around the failed region to the next closest gateway.

备注

只会将 API 管理的网关组件部署到所有区域。Only the gateway component of API Management is deployed to all regions. 服务管理组件和开发人员门户只会托管在主要区域。The service management component and developer portal are hosted in the Primary region only. 因此,如果主要区域发生服务中断,在主要区域恢复联机之前,访问开发人员门户和更改配置(例如添加 API、应用策略)的功能将受到影响。Therefore, in case of the Primary region outage, access to the developer portal and ability to change configuration (e.g. adding APIs, applying policies) will be impaired until the Primary region comes back online. 当主要区域脱机时,可用的辅助区域将继续使用可用的最新配置为 API 流量提供服务。While the Primary region is offline available Secondary regions will continue to serve the API traffic using the latest configuration available to them.

可用性Availability

重要

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

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

备注

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

  1. 在 Azure 门户中,导航到 API 管理服务并单击菜单中的“位置”条目。In the Azure portal, navigate to your API Management service and click on the Locations entry in the menu.
  2. 单击顶部栏中的“+ 添加”。Click + Add in the top bar.
  3. 从下拉列表中选择位置,并通过滑块为其设置单位数。Select the location from the drop-down list and set the number of units with the slider.
  4. 单击“添加”按钮进行确认。Click the Add button to confirm.
  5. 重复此过程,直到配置所有位置。Repeat this process until you configure all locations.
  6. 单击顶部栏中的“保存”以开始部署过程。Click Save in the top bar to start the deployment process.

删除 API 管理服务位置 Delete an API Management service location

  1. 在 Azure 门户中,导航到 API 管理服务并单击菜单中的“位置”条目。In the Azure portal, navigate to your API Management service and click on the Locations entry in the menu.
  2. 若要删除位置,请使用表右端的“...”按钮打开上下文菜单。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.
  3. 确认删除,并单击“保存”应用所做的更改。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 North and China East regions:

    <policies>
        <inbound>
            <base />
            <choose>
                <when condition="@("China North".Equals(context.Deployment.Region, StringComparison.OrdinalIgnoreCase))">
                    <set-backend-service base-url="http://contoso-us.com/" />
                </when>
                <when condition="@("China East".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>
    

提示

还可以使用 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.net URL 模式,例如 https://contoso-chinanorth2-01.regional.azure-api.cnThe regional endpoints follow the URL pattern of https://<service-name>-<region>-01.regional.azure-api.net, for example https://contoso-chinanorth2-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.