在Spring Boot微服务使用ListOperations操作Redis集群List列表

这篇具有很好参考价值的文章主要介绍了在Spring Boot微服务使用ListOperations操作Redis集群List列表。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

记录:444

场景:在Spring Boot微服务使用RedisTemplate的ListOperations操作Redis集群的List列表数据类型。

版本:JDK 1.8,Spring Boot 2.6.3,redis-6.2.5。

1.微服务中配置Redis信息

1.1在pom.xml添加依赖

pom.xml文件:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
  <version>2.6.3</version>
</dependency>

解析:spring-boot-starter-data-redis和spring-boot版本保持一致。

1.2在application.yml中配置Redis集群信息

(1)application.yml配置内容

spring:
  redis:
    cluster:
      nodes:
        - 192.168.19.161:27001
        - 192.168.19.161:27002
        - 192.168.19.162:27001
        - 192.168.19.162:27002
        - 192.168.19.163:27001
        - 192.168.19.163:27002
    password: demo12345678
    timeout: 60000

(2)解析

配置内容来源。

jar包:spring-boot-autoconfigure-2.6.3.jar。

类:org.springframework.boot.autoconfigure.data.redis.RedisProperties。

当引入spring-boot-starter时,此包已经引入。

当需要配置集群其它信息时,在RedisProperties类中查找并配置到application.yml就能生效。

1.3加载简要逻辑

Spring Boot微服务在启动时,自动注解机制会读取application.yml的配置信息注入到RedisProperties对象的对应属性。因此,在Spring环境中就能取到Redis集群的配置信息。

Spring从RedisProperties对象中取配置注入到RedisTemplate客户端中。因此,RedisTemplate客户端就能对Redis集群做增、删、改、查等操作。

2.配置RedisTemplate

RedisTemplate是springframework框架中封装的操作Redis的客户端。

类全称:org.springframework.data.redis.core.RedisTemplate

2.1配置RedisTemplate

@Configuration
public class RedisConfig {
  @Bean("redisTemplate")
  public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
      // 1.创建RedisTemplate对象
      RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
      // 2.加载Redis配置
      redisTemplate.setConnectionFactory(lettuceConnectionFactory);
      // 3.配置key序列化
      RedisSerializer<?> stringRedisSerializer = new StringRedisSerializer();
      redisTemplate.setKeySerializer(stringRedisSerializer);
      redisTemplate.setHashKeySerializer(stringRedisSerializer);
      // 4.配置Value序列化
      Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
      ObjectMapper objMapper = new ObjectMapper();
      objMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
      objMapper.activateDefaultTyping(objMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
      jackson2JsonRedisSerializer.setObjectMapper(objMapper);
      redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
      redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
      // 5.初始化RedisTemplate
      redisTemplate.afterPropertiesSet();
      return redisTemplate;
  }
  @Bean
  public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
      return redisTemplate.opsForList();
  }
}

2.2解析

使用@Configuration和@Bean配置RedisTemplate后,使用@Autowired注解注入RedisTemplate和ListOperations实例操作Redis集群。

3.使用ListOperations操作Redis集群List列表

3.1简要说明

使用ValueOperations操作字符串,常用操作:增、查、改、删、设置超时等。

3.2操作示例

@RestController
@RequestMapping("/hub/example/operateCluster")
@Slf4j
public class OperateClusterController {
  @Autowired
  private RedisTemplate redisTemplate;
  @Autowired
  private ListOperations listOperations;
  /**
   * 使用ListOperations,操作List类型数据
   */
  @GetMapping("/f03")
  public Object f03() {
      log.info("ListOperations操作Redis集群开始...");
      // 1.增
      listOperations.leftPush("D:2023060803:01", "杭州");
      listOperations.rightPush("D:2023060803:01", "苏州");
      // 2.查,查出队列指定范围元素,不会删除队列里面数据,(0,-1)查出全部元素
      listOperations.leftPush("D:2023060803:01", "南京");
      listOperations.leftPush("D:2023060803:01", "无锡");
      List cityList = redisTemplate.boundListOps("D:2023060803:01").range(0, -1);
      cityList.forEach((value) -> {
          System.out.println("从队列D:2023060803:01取值: " + value);
      });
      // 3.取,逐个取出队列元素(取出一个元素后,队列就没有这个元素了)
      Object city01 = listOperations.leftPop("D:2023060803:01");
      Object city02 = listOperations.rightPop("D:2023060803:01");
      log.info("从队列D:2023060803:01取出: city01=" + city01 + ",city02=" + city02);
      // 4.删
      listOperations.leftPush("D:2023060803:01", "常州");
      listOperations.leftPush("D:2023060803:01", "绍兴");
      long time = 5000;
      log.info("{}秒后,删除D:2023060803:01队列", time / 1000);
      ThreadUtil.sleep(time);
      redisTemplate.delete("D:2023060803:01");
      // 5.设置超时
      listOperations.leftPush("D:2023060803:02", "上海");
      redisTemplate.boundValueOps("D:2023060803:02").expire(5, TimeUnit.MINUTES);
      redisTemplate.expire("D:2023060803:02", 10, TimeUnit.MINUTES);
      // 6.查询List的元素个数
      Long size = listOperations.size("D:2023060803:02");
      System.out.println("查询List的元素个数,size=" + size);
      log.info("ListOperations操作Redis集群结束...");
      return "执行成功";
  }
}

3.3测试验证

使用Postman测试。

请求RUL:http://127.0.0.1:18205/hub-205-redis/hub/example/operateCluster/f03

以上,感谢。

2023年6月8日文章来源地址https://www.toymoban.com/news/detail-477652.html

到了这里,关于在Spring Boot微服务使用ListOperations操作Redis集群List列表的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring Boot 中的 Redis 数据操作配置和使用

    Redis(Remote Dictionary Server)是一种高性能的开源内存数据库,用于缓存、消息队列、会话管理和数据存储。在Spring Boot应用程序中,Redis被广泛用于各种用例,包括缓存、持久性存储和分布式锁。本文将探讨如何在Spring Boot中配置和使用Redis,包括数据操作和常见用例。 要在S

    2024年02月07日
    浏览(30)
  • Spring Boot 中的 Redis 的数据操作配置和使用

    Redis 是一种高性能的 NoSQL 数据库,它支持多种数据结构,包括字符串、哈希、列表、集合和有序集合。Redis 还提供了丰富的命令,可以对数据进行快速的 CRUD 操作。Spring Boot 是一个基于 Spring 的快速开发框架,它提供了对 Redis 的集成支持。在本文中,我们将介绍如何在 Spri

    2024年02月11日
    浏览(38)
  • 使用Spring Boot操作Redis、ES、MongoDB举例

    在Spring Boot应用程序中操作Redis通常涉及到使用Spring Data Redis,这是一个提供简便方法来操作Redis的库。以下是一个基本示例,演示如何在Spring Boot应用程序中集成和使用Redis: 步骤 1: 添加依赖项 首先,在你的 pom.xml 文件中添加Spring Data Redis的依赖项。 步骤 2: 配置Redis 在你的

    2024年01月25日
    浏览(32)
  • 【微服务部署】十、使用Docker Compose搭建高可用Redis集群

      现如今,业务系统对于缓存Redis的依赖似乎是必不可少的,我们可以在各种各样的系统中看到Redis的身影。考虑到系统运行的稳定性,Redis的应用和MySQL数据库一样需要做到高可用部署。 一、Redis 的多种高可用方案 常见的Redis的高可用方案有以下几种: Redis Replication(主从

    2024年02月07日
    浏览(32)
  • 在Spring Boot微服务使用jasypt-spring-boot加密和解密yml配置文件

    记录 :424 场景 :在Spring Boot微服务,使用jasypt-spring-boot加密和解密yml配置文件中的配置信息。 版本 :JDK 1.8,Spring Boot 2.6.3,jasypt-1.9.3,jasypt-spring-boot-2.1.2, jasypt-spring-boot-3.0.5。 开源地址 :https://github.com/ulisesbocchio/jasypt-spring-boot 1.在Spring Boot微服务使用jasypt-spring-boot-3.0.5版本

    2024年02月09日
    浏览(58)
  • Spring boot 操作 Redis

    🌹作者主页:青花锁 🌹简介:Java领域优质创作者🏆、Java微服务架构公号作者😄 🌹简历模板、学习资料、面试题库、技术互助 🌹文末获取联系方式 📝 专栏 描述 Java项目实战 介绍Java组件安装、使用;手写框架等 Aws服务器实战 Aws Linux服务器上操作nginx、git、JDK、Vue Jav

    2024年03月20日
    浏览(39)
  • 【Spring Boot】操作Redis数据结构

    2024年02月07日
    浏览(23)
  • 【Spring Boot 3】【Redis】基本数据类型操作

    软件开发是一门实践性科学,对大多数人来说,学习一种新技术不是一开始就去深究其原理,而是先从做出一个可工作的DEMO入手。但在我个人学习和工作经历中,每次学习新技术总是要花费或多或少的时间、检索不止一篇资料才能得出一个可工作的DEMO,这占用了我大量的时

    2024年01月20日
    浏览(30)
  • 在Spring Boot微服务集成spring-kafka操作Kafka集群

    记录 :461 场景 :在Spring Boot微服务集成spring-kafka-2.8.2操作Kafka集群。使用KafkaTemplate操作Kafka集群的生产者Producer。使用@KafkaListener操作Kafka集群的消费者Consumer。 版本 :JDK 1.8,Spring Boot 2.6.3,kafka_2.12-2.8.0,spring-kafka-2.8.2。 Kafka集群安装 :https://blog.csdn.net/zhangbeizhen18/article/details

    2024年02月10日
    浏览(33)
  • 在Spring Boot微服务集成Jedis操作Redis

    记录 :406 场景 :在Spring Boot微服务集成Jedis操作Redis的缓存和队列。 版本 :JDK 1.8,Spring Boot 2.6.3,redis-6.2.5,jedis-3.7.1。 1.微服务中配置 Redis信息 1.1在application.yml中Jedis配置信息 1.2使用 ConfigurationProperties加载Jedis配置 Spring Boot微服务在启动时,自动注解机制会读取application.yml的

    2023年04月15日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包