广告
返回顶部
首页 > 资讯 > 精选 >spring整合redis消息监听通知使用的方法是什么
  • 853
分享到

spring整合redis消息监听通知使用的方法是什么

2023-06-22 00:06:40 853人浏览 独家记忆
摘要

本篇内容介绍了“spring整合redis消息监听通知使用的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!问题引入在电商系统中,秒

本篇内容介绍了“spring整合redis消息监听通知使用的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

问题引入

在电商系统中,秒杀,抢购,红包优惠卷等操作,一般都会设置时间限制,比如订单15分钟不付款自动关闭,红包有效期24小时等等。那对于这种需求最简单的处理方式就是使用定时任务,定时扫描数据库的方式处理。但是为了更加精确的时间控制,定时任务的执行时间会设置的很短,所以会造成很大的数据库压力。

是否有更加稳妥的解决方式呢?我们可以利用Redis的key失效机制结合REDIS的消息通知机制结合完成类似问题的处理。

1.1 过期问题描述

在电商系统中,秒杀,抢购,红包优惠卷等操作,一般都会设置时间限制,比如订单15分钟不付款自动关闭,红包有效期24小时等等

1.2 常用解决方案分析

目前企业中最常见的解决方案大致分为两种:

  • 使用定时任务处理,定时扫描数据库中过期的数据,然后进行修改。但是为了更加精确的时间控制,定时任务的执行时间会设置的很短,所以会造成很大的数据库压力。

  • 使用消息通知,当数据失效时发送消息,程序接收到失效消息后对响应的数据进行状态修改。此种方式不会对数据库造成太大的压力

1.3.整合SpringData Redis开发

我们使用redis解决过期优惠券和红包等问题,并且在java环境中使用redis的消息通知。目前世面比较流行的java代码操作redis的aiP有:Jedis和RedisTemplate

Jedis是Redis官方推出的一款面向Java的客户端,提供了很多接口供Java语言调用。

SpringData Redis是Spring官方推出,可以算是Spring框架集成Redis操作的一个子框架,封装了Redis的很多命令,可以很方便的使用Spring操作Redis数据库。由于现代企业开发中都使用Spring整合项目,所以在api的选择上我们使用Spring提供的SpringData Redis

spring整合redis监听消息

1. 配置监听redis消息

如果要在java代码中监听redis的主题消息,我们还需要自定义处理消息的监听器,

MessageListener类的源码

package org.springframework.data.redis.connection;import org.springframework.lang.Nullable;public interface MessageListener {void onMessage(Message message, @Nullable byte[] pattern);}

拓展这个接口的代码如下

public class RedisMessageListener implements MessageListener {public void onMessage(Message message, byte[] pattern) { System.out.println("从channel为" + new String(message.getChannel())                + "中获取了一条新的消息,消息内容:" + new String(message.getBody()));}}

这样我们就定义好了一个消息监听器,当订阅的频道有一条新的消息发送过来之后,通过此监听器中的onMessage方法处理

当监听器程序写好之后,我们还需要在springData redis的配置文件中添加监听器以及订阅的频道主题,

我们测试订阅的频道为ITCAST,配置如下:

<!-- 配置处理消息的消息监听适配器 --><bean class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter" id="messageListener"><!-- 构造方法注入:自定义的消息监听 -->    <constructor-arg>        <bean class="cn.itcast.redis.listener.RedisKeyExpiredMessageDelegate"/>    </constructor-arg></bean><!-- 消息监听者容器:对所有的消息进行统一管理 --><bean class="org.springframework.data.redis.listener.RedisMessageListenerContainer" id="redisContainer">    <property name="connectionFactory" ref="connectionFactory"/>    <property name="messageListeners">    <map>    <!-- 配置频道与监听器    将此频道中的内容交由此监听器处理    key-ref:监听,处理消息    ChannelTopic:订阅的消息频道     -->            <entry key-ref="messageListener">                <list>                    <bean class="org.springframework.data.redis.listener.ChannelTopic">                        <constructor-arg value="ITCAST"></constructor-arg>                    </bean>                </list>            </entry>    </map> </property></bean>

2 测试消息

配置好消息监听,已经订阅的主题之后就可以启动程序进行测试了。由于有监听程序在,只需要已java代码的形式启动,创建spring容器(当spring容器加载之后,会创建监听器一直监听对应的消息)。

public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-data-redis.xml");}

当程序启动之后,会一直保持运行状态。即订阅了ITCSAT频道的消息,这个时候通过redis的客户端程序(redis-cli)发布一条消息

spring整合redis消息监听通知使用的方法是什么

命令解释:

publish topic名称 消息内容 : 向指定频道发送一条消息

发送消息之后,我们在来看java控制台输出可验证获取到了此消息

结合redis的key失效机制和消息完成过期优惠券处理

解决过期优惠券的问题处理起来比较简单:

在redis的内部当一个key失效时,也会向固定的频道中发送一条消息,我们只需要监听到此消息获取数据库中的id,修改对应的优惠券状态就可以了。这也带来了一些繁琐的操作:用户获取到优惠券之后需要将优惠券存入redis服务器并设置超时时间。

由于要借助redis的key失效通知,有两个注意事项各位需要注意:

  1. 事件通过 Redis 的订阅与发布功能(pub/sub)来进行分发,故需要订阅(__keyevent@0__:expired)频道 0表示db0 根据自己的dbindex选择合适的数字

  2. 修改 redis.conf 文件

修改 notify-keyspace-events Ex

# K    键空间通知,以__keyspace@<db>__为前缀# E    键事件通知,以__keysevent@<db>__为前缀# g    del , expipre , rename 等类型无关的通用命令的通知, ...# $    String命令# l    List命令# s    Set命令# h    Hash命令# z    有序集合命令# x    过期事件(每次key过期时生成)# e    驱逐事件(当key在内存满了被清除时生成)# A    g$lshzxe的别名,因此”AKE”意味着所有的事件

1 模拟过期代金卷案例

前置性的内容已经和大家都介绍完毕,接下来我们就可以使用redis的消息通知结合springDataRedis完成一个过期优惠券的处理,为了更加直观的展示问题,这里准备了两个程序:

第一个程序(coupon-achieve)用来模拟用户获取一张优惠券并保存到数据库,存入redis缓存

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="classpath:applicationContext.xml")public class CouponTest {@Autowiredprivate CouponMapper couponMapper;@Autowiredprivate RedisTemplate<String, String> redisTemplate;@Testpublic void testSaveCoupon() {Date now = new Date();int timeOut = 1;//设置优惠券的失效时间(一分钟后失效)//自定义一张优惠券,Coupon coupon = new Coupon();coupon.setName("测试优惠券");//设置名称coupon.setMoney(BigDecimal.ONE);//设置金额coupon.setCouponDesc("全品类优惠10元");//设置说明coupon.setCreateTime(now);//设置获取时间//设置超时时间:优惠券有效期1分钟后超时coupon.setExpireTime(DataUtils.addTime(now, timeOut));//设置状态:0-可用 1-已失效 2-已使用coupon.setState(0);couponMapper.saveCoupon(coupon );redisTemplate.opsForValue().set("coupon:"+coupon.getId(), coupon.getId()+"", (long)timeOut, TimeUnit.MINUTES);}

第二个程序(coupon-expired)配置消息通知监听redis的key失效,获取通知之后修改优惠券状态

数据库表:

CREATE TABLE `t_coupon` (  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',  `name` varchar(60) DEFAULT NULL COMMENT '优惠券名称',  `money` decimal(10,0) DEFAULT NULL COMMENT '金额',  `coupon_desc` varchar(128) DEFAULT NULL COMMENT '优惠券说明',  `create_time` datetime DEFAULT NULL COMMENT '获取时间',  `expire_time` datetime DEFAULT NULL COMMENT '失效时间',  `state` int(1) DEFAULT NULL COMMENT '状态,0-有效,1-已失效,2-已使用',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

2 配置redis中key失效的消息监听

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="Http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><description>spring-data整合jedis</description><!-- springData Redis的核心API --><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="connectionFactory"></property><property name="keySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean></property><property name="valueSerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean></property></bean><!-- 连接工厂 --><bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><property name="hostName" value="127.0.0.1"></property><property name="port" value="6379"></property><property name="database" value="0"></property><property name="poolConfig" ref="poolConfig"></property></bean><!-- 连接池基本配置 --><bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxIdle" value="5"></property><property name="maxTotal" value="10"></property><property name="testOnBorrow" value="true"></property></bean><!-- 配置监听 --><bean class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter" id="messageListener">    <constructor-arg>        <bean class="cn.itcast.redis.listener.RedisKeyExpiredMessageDelegate"/>    </constructor-arg></bean><!-- 监听容器 --><bean class="org.springframework.data.redis.listener.RedisMessageListenerContainer" id="redisContainer">    <property name="connectionFactory" ref="connectionFactory"/>    <property name="messageListeners">    <map>            <entry key-ref="messageListener">                <list>                <!-- __keyevent@0__:expired  配置订阅的主题名称                此名称时redis提供的名称,标识过期key消息通知                0表示db0 根据自己的dbindex选择合适的数字                 -->                    <bean class="org.springframework.data.redis.listener.ChannelTopic">                        <constructor-arg value="__keyevent@0__:expired"></constructor-arg>                    </bean>                </list>            </entry>    </map> </property></bean></beans>

3 接收失效消息完成过期代金卷处理

package cn.itcast.redis.listener;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.connection.Message;import org.springframework.data.redis.connection.MessageListener;import cn.itcast.entity.Coupon;import cn.itcast.mapper.CouponMapper;public class RedisKeyExpiredMessageDelegate implements MessageListener {@Autowiredprivate CouponMapper couponMapper;public void onMessage(Message message, byte[] pattern) {//1.获取失效的keyString key = new String(message.getBody());//判断是否时优惠券失效通知if(key.startsWith("coupon")){//2.从key中分离出失效优惠券idString redisId = key.split(":")[1];//3.查询优惠卷信息Coupon coupon = couponMapper.selectCouponById(Long.parseLong(redisId));//4.修改状态coupon.setState(1);//5.更新数据库couponMapper.updateCoupon(coupon);}}}

测试日志如下:

spring整合redis消息监听通知使用的方法是什么

通过日志我们发现,当优惠券到失效时,redis立即发送一条消息告知此优惠券失效,我们可以在监听程序中获取当前的id,查询数据库修改状态

“spring整合redis消息监听通知使用的方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!

--结束END--

本文标题: spring整合redis消息监听通知使用的方法是什么

本文链接: https://www.lsjlt.com/news/301440.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • spring整合redis消息监听通知使用的方法是什么
    本篇内容介绍了“spring整合redis消息监听通知使用的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!问题引入在电商系统中,秒...
    99+
    2023-06-22
  • spring整合redis消息监听通知使用的实现示例
    目录问题引入1.1 过期问题描述1.2 常用解决方案分析1.3.整合SpringData Redis开发spring整合redis监听消息1. 配置监听redis消息2 测试消息结合...
    99+
    2022-11-12
  • SpringBoot整合消息队列RabbitMQ的方法是什么
    本篇内容介绍了“SpringBoot整合消息队列RabbitMQ的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!简介在Spring...
    99+
    2023-07-05
  • Spring Cloud Alibaba整合Nacos使用的方法是什么
    今天小编给大家分享一下Spring Cloud Alibaba整合Nacos使用的方法是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有...
    99+
    2023-07-05
  • springcloud整合openfeign使用的方法是什么
    这篇文章主要介绍“springcloud整合openfeign使用的方法是什么”,在日常操作中,相信很多人在springcloud整合openfeign使用的方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家...
    99+
    2023-07-05
  • Spring Boot使用注解集成Redis缓存的方法是什么
    这篇文章主要介绍“Spring Boot使用注解集成Redis缓存的方法是什么”,在日常操作中,相信很多人在Spring Boot使用注解集成Redis缓存的方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家...
    99+
    2023-06-04
  • tk-mybatis整合springBoot使用两个数据源的方法是什么
    这篇文章主要介绍“tk-mybatis整合springBoot使用两个数据源的方法是什么”,在日常操作中,相信很多人在tk-mybatis整合springBoot使用两个数据源的方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的...
    99+
    2023-06-21
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作