Spring Cloud Azure Redis 支持

本文适用于: ✔️版本 4.14.0 ✔️ 版本 5.8.0

使用 Spring Redis 库连接 Azure Redis 缓存。 通过添加spring-cloud-azure-starterspring-cloud-azure-resourcemanager添加到应用程序,可以通过 Azure 资源管理器和自动配置 Redis 属性来读取 Azure Redis 缓存连接信息。

依赖项设置

如果要使用 Spring Cloud Azure Redis 支持,请使用 Redis 将以下依赖项添加到 Spring Boot 应用程序。

<dependencies>
    <dependency>
      <groupId>com.azure.spring</groupId>
      <artifactId>spring-cloud-azure-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>com.azure.spring</groupId>
      <artifactId>spring-cloud-azure-resourcemanager</artifactId>
    </dependency>
</dependencies>

配置

注意

如果选择使用安全主体通过 Microsoft Entra ID 进行身份验证和授权来访问 Azure 资源,请参阅 “使用 Microsoft Entra ID 授权访问”,以确保安全主体已获得访问 Azure 资源的足够权限。

使用 Redis 支持时可配置的属性:

properties Description 默认值 必需
spring.cloud.azure.redis.enabled 是否启用了 Azure Redis 缓存。 true
spring.cloud.azure.redis.name Azure Redis 缓存实例名称。
spring.cloud.azure.redis.resource.resource-group Azure Redis 缓存的资源组。
spring.cloud.azure.profile.subscription-id 订阅的 ID。

注意

还需要身份验证信息才能对 Azure 资源管理器进行身份验证。 资源管理器的凭据相关配置应在前缀spring.cloud.azure下进行配置。 有关详细信息,请参阅 Spring Cloud Azure 身份验证

基本用法

添加以下属性,可以继续操作。

spring.cloud.azure.redis.name=${AZURE_CACHE_REDIS_NAME}
spring.cloud.azure.redis.resource.resource-group=${AZURE_CACHE_REDIS_RESOURCE_GROUP}

示例

有关详细信息,请参阅 GitHub 上的 azure-spring-boot-samples 存储库。