在 Spring 中使用 Azure Redis 缓存

Azure Cache for Redis 提供基于 Redis 软件的内存中数据存储。 Redis 可提高使用后端数据存储的应用程序的性能和可伸缩性。

本教程演示如何使用 Redis 缓存在 Spring Boot 应用程序中存储和检索数据。

在本教程中,我们包括两种身份验证方法:Microsoft Entra 身份验证和 Redis 身份验证。 “无密码”选项卡显示“Microsoft Entra 身份验证”,“密码”选项卡显示 Redis 身份验证。

Microsoft Entra 身份验证是一种使用 Microsoft Entra ID 中定义的标识连接到 Azure Redis 缓存的机制。 通过 Microsoft Entra 身份验证,可以在一个中心位置集中管理数据库用户标识和其他 Microsoft 服务,从而简化权限管理。

Redis 身份验证使用 Redis 中的密码。 如果选择使用密码作为凭据,则需要自行管理密码。

先决条件

将数据缓存到 Azure Redis 缓存

使用 Azure Redis 缓存实例,可以使用 Spring Cloud Azure 缓存数据。

若要使用 Lettuce 模块安装 Spring Cloud Azure Starter Data Redis,请将以下依赖项添加到 pom.xml 文件:

<dependencies>
 <dependency>
   <groupId>com.azure.spring</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
   <groupId>com.azure.spring</groupId>
   <artifactId>spring-cloud-azure-starter-data-redis-lettuce</artifactId>
 </dependency>
 <dependency>
   <groupId>com.azure.spring</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
</dependencies>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.azure.spring</groupId>
      <artifactId>spring-cloud-azure-dependencies</artifactId>
      <version>5.16.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

注意

此材料清单(BOM)应在pom.xml文件的部分中进行配置<dependencyManagement>。 此配置可确保所有 Spring Cloud Azure 依赖项都使用相同的版本。 有关用于此 BOM 的版本的详细信息,请参阅 我应使用哪个版本的 Spring Cloud Azure。

编写应用程序代码

若要使用 Redis 缓存来存储和检索数据,请使用以下步骤配置应用程序:

  1. application.properties 配置文件中配置 Redis 缓存凭据,如以下示例所示。

    spring.data.redis.host=<your-redis-name>.redis.cache.windows.net
    spring.data.redis.port=6380
    spring.data.redis.username=<your-redis-username>
    spring.data.redis.ssl.enabled=true
    spring.data.redis.azure.passwordless-enabled=true
    

    注意

    若要获取其值username,请按照缓存部分中的“启用Microsoft条目 ID 身份验证”中的说明操作,在缓存身份验证中使用 Microsoft Entra ID,并复制用户名值。

  2. 编辑启动类文件以显示以下内容。 此代码存储并检索数据。

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.core.ValueOperations;
    
    @SpringBootApplication
    public class DemoCacheApplication implements CommandLineRunner {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(DemoCacheApplication.class);
    
        @Autowired
        private StringRedisTemplate template;
    
        public static void main(String[] args) {
            SpringApplication.run(DemoCacheApplication.class, args);
        }
    
        @Override
        public void run(String... args) {
            ValueOperations<String, String> ops = this.template.opsForValue();
            String key = "testkey";
            if(!this.template.hasKey(key)){
                ops.set(key, "Hello World");
                LOGGER.info("Add a key is done");
            }
            LOGGER.info("Return the value from the cache: {}", ops.get(key));
        }
    
    }
    

然后,启动应用程序。 应用程序从 Redis 缓存中检索数据。 应会看到类似于以下示例的日志:

Add a key is done
Return the value from the cache: Hello World

部署到 Azure Spring Apps

现在,你已在本地运行 Spring Boot 应用程序,现在可以将其移动到生产环境。 使用 Azure Spring Apps 可以轻松地将 Spring Boot 应用程序部署到 Azure,而无需进行任何代码更改。 该服务管理 Spring 应用程序的基础结构,让开发人员可以专注于代码。 Azure Spring Apps 可以通过以下方法提供生命周期管理:综合性监视和诊断、配置管理、服务发现、CI/CD 集成、蓝绿部署等。 若要将应用程序部署到 Azure Spring Apps,请参阅将 第一个应用程序部署到 Azure Spring Apps

后续步骤

若要了解有关 Spring 和 Azure 的详细信息,请继续访问“Azure 上的 Spring”文档中心。