在 Azure API 管理中使用外部 Redis 兼容缓存Use an external Redis-compatible cache in Azure API Management

除了利用内置缓存外,Azure API 管理还允许将响应缓存在外部 Redis 兼容缓存中,例如 Azure Cache for Redis。In addition to utilizing the built-in cache, Azure API Management allows for caching responses in an external Redis-compatible cache, e.g. Azure Cache for Redis.

使用外部缓存可以克服内置缓存的一些限制:Using an external cache allows to overcome a few limitations of the built-in cache:

  • 避免在执行 API 管理更新期间定期清除缓存Avoid having your cache periodically cleared during API Management updates
  • 更好地控制你的缓存配置Have more control over your cache configuration
  • 缓存比 API 管理层允许的数据更多的数据Cache more data than your API Management tier allows to
  • 将缓存与 API 管理的消耗层配合使用Use caching with the Consumption tier of API Management
  • API 管理自承载网关中启用缓存Enable caching in the API Management self-hosted gateways

若要更详细地了解缓存,请参阅 API 管理缓存策略Azure API 管理中的自定义缓存For more detailed information about caching, see API Management caching policies and Custom caching in Azure API Management.

将自己的缓存带到 APIM

学习内容:What you'll learn:

  • 在 API 管理中添加外部缓存Add an external cache in API Management

先决条件Prerequisites

要完成本教程,需要:To complete this tutorial, you need to:

创建 Azure Cache for Redis Create Azure Cache for Redis

本部分介绍了如何在 Azure 中创建用于 Redis 的 Azure 缓存。This section explains how to create an Azure Cache for Redis in Azure. 如果你已有用于 Redis 的 Azure 缓存(无论是在 Azure 内部还是外部),则可以跳到下一部分。If you already have an Azure Cache for Redis, within or outside of Azure, you can skip to the next section.

  1. 若要创建缓存,请登录到 Azure 门户并选择“创建资源” 。To create a cache, sign in to the Azure portal and select Create a resource.

    选择“创建资源”

  2. 在“新建”页上选择“数据库”,然后选择“Azure Cache for Redis”。 On the New page, select Databases and then select Azure Cache for Redis.

    选择“Azure Cache for Redis”

  3. 在“新建 Redis 缓存”页上配置新缓存的设置。 On the New Redis Cache page, configure the settings for your new cache.

    设置Setting 建议的值Suggested value 说明Description
    DNS 名称DNS name 输入任何全局唯一的名称。Enter a globally unique name. 缓存名称必须是包含 1 到 63 个字符的字符串,只能包含数字、字母或连字符。The cache name must be a string between 1 and 63 characters that contains only numbers, letters, or hyphens. 该名称必须以数字或字母开头和结尾,且不能包含连续的连字符。The name must start and end with a number or letter, and can't contain consecutive hyphens. 缓存实例的主机名是 <DNS 名称>.redis.cache.chinacloudapi.cn 。 Your cache instance's host name will be <DNS name>.redis.cache.chinacloudapi.cn.
    订阅Subscription 单击下拉箭头并选择你的订阅。Drop down and select your subscription. 要在其下创建此新的 Azure Cache for Redis 实例的订阅。The subscription under which to create this new Azure Cache for Redis instance.
    资源组Resource group 单击下拉箭头并选择一个资源组,或者选择“新建”并输入新的资源组名称。 Drop down and select a resource group, or select Create new and enter a new resource group name. 要在其中创建缓存和其他资源的资源组的名称。Name for the resource group in which to create your cache and other resources. 将所有应用资源放入一个资源组可以轻松地统一管理或删除这些资源。By putting all your app resources in one resource group, you can easily manage or delete them together.
    LocationLocation 单击下拉箭头并选择一个位置。Drop down and select a location. 选择与要使用该缓存的其他服务靠近的区域Select a region near other services that will use your cache.
    定价层Pricing tier 单击下拉箭头并选择一个定价层Drop down and select a Pricing tier. 定价层决定可用于缓存的大小、性能和功能。The pricing tier determines the size, performance, and features that are available for the cache. 有关详细信息,请参阅用于 Redis 的 Azure 缓存概述For more information, see Azure Cache for Redis Overview.
  4. 选择“创建” 。Select Create.

    创建 Azure Redis 缓存

    创建缓存需要花费片刻时间。It takes a while for the cache to create. 可以在 Azure Cache for Redis 的“概述”页上监视进度。 You can monitor progress on the Azure Cache for Redis Overview page. 如果“状态”显示为“正在运行”,则表示该缓存可供使用。 When Status shows as Running, the cache is ready to use.

    创建的 Azure Redis 缓存

将 Redis 缓存部署到 Kubernetes Deploy Redis cache to Kubernetes

对于缓存,自承载网关仅依赖于外部缓存。For caching, self-hosted gateways rely exclusively on external caches. 为了使缓存有效,自承载网关和它们所依赖的缓存必须彼此接近,以最大程度地降低查找和存储延迟。For caching to be effective self-hosted gateways and the cache they rely on must be located close to each other to minimize lookup and store latencies. 将 Redis 缓存部署到同一个 Kubernetes 群集或附近的单独群集是最佳做法。Deploying a Redis cache into the same Kubernetes cluster or in a separate cluster nearby are the best options. 请单击此链接,了解如何将 Redis 缓存部署到 Kubernetes 群集。Follow this link to learn how to deploy Redis cache to a Kubernetes cluster.

添加外部缓存 Add an external cache

按照以下步骤在 Azure API 管理中添加用于 Redis 的外部 Azure 缓存。Follow the steps below to add an external Azure Cache for Redis in Azure API Management.

将自己的缓存带到 APIM

备注

“使用位置”设置指定将使用配置的缓存的 Azure 区域或自承载网关位置。The Use from setting specifies an Azure region or a self-hosted gateway location that will use the configured cache. 配置为“默认值”的缓存将由具有特定匹配区域或位置值的缓存进行重写。The caches configured as Default will be overridden by caches with a specific matching region or location value.

例如,如果 API 管理承载在“美国东部”、“东南亚”和“西欧”,并且配置了两个缓存,一个用于默认,另一个用于东南亚,则东南亚中的 API 管理将使用其自己的缓存,而其他两个区域将使用默认缓存项。For example, if API Management is hosted in the East US, Southeast Asia and West Europe regions and there are two caches configured, one for Default and one for Southeast Asia, API Management in Southeast Asia will use its own cache, while the other two regions will use the Default cache entry.

从同一订阅中添加用于 Redis 的 Azure 缓存Add an Azure Cache for Redis from the same subscription

  1. 在 Azure 门户中浏览到你的 API 管理实例。Browse to your API Management instance in the Azure portal.
  2. 从左侧的菜单中选择“外部缓存”选项卡。Select the External cache tab from the menu on the left.
  3. 单击“+ 添加”按钮。Click the + Add button.
  4. 从“缓存实例”下拉字段中选择你的缓存。Select your cache in the Cache instance dropdown field.
  5. 在“使用以下位置中的”下拉字段中选择“默认”或指定所需的区域。Select Default or specify the desired region in the Use from dropdown field.
  6. 单击“保存” 。Click Save.

添加位于当前 Azure 订阅外部或 Azure 外部(后者为一般情况)的用于 Redis 的 Azure 缓存Add an Azure Cache for Redis hosted outside of the current Azure subscription or Azure in general

  1. 在 Azure 门户中浏览到你的 API 管理实例。Browse to your API Management instance in the Azure portal.
  2. 从左侧的菜单中选择“外部缓存”选项卡。Select the External cache tab from the menu on the left.
  3. 单击“+ 添加”按钮。Click the + Add button.
  4. 从“缓存实例”下拉字段中选择“自定义”。 Select Custom in the Cache instance dropdown field.
  5. 在“使用以下位置中的”下拉字段中选择“默认”或指定所需的区域。Select Default or specify the desired region in the Use from dropdown field.
  6. 在“连接字符串”字段中提供用于 Redis 的 Azure 缓存连接字符串。Provide your Azure Cache for Redis connection string in the Connection string field.
  7. 单击“保存” 。Click Save.

将 Redis 缓存添加到自承载网关Add a Redis cache to a self-hosted gateway

  1. 在 Azure 门户中浏览到你的 API 管理实例。Browse to your API Management instance in the Azure portal.
  2. 从左侧的菜单中选择“外部缓存”选项卡。Select the External cache tab from the menu on the left.
  3. 单击“+ 添加”按钮。Click the + Add button.
  4. 从“缓存实例”下拉字段中选择“自定义”。 Select Custom in the Cache instance dropdown field.
  5. 指定所需的自承载网关位置,或“使用位置”下拉字段中的“默认值” 。Specify the desired self-hosted gateway location or Default in the Use from dropdown field.
  6. 在“连接字符串”字段中提供你的 Redis 缓存连接字符串。Provide your Redis cache connection string in the Connection string field.
  7. 单击“保存” 。Click Save.

使用外部缓存Use the external cache

在 Azure API 管理中配置外部缓存后,可以通过缓存策略使用该缓存。Once the external cache is configured in Azure API Management, it can be used through caching policies. 有关详细步骤,请参阅添加缓存以提高 Azure API 管理中的性能See Add caching to improve performance in Azure API Management for detailed steps.

后续步骤 Next steps