A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

Spring+Reids 订阅键过期通知



项目需求:redis缓存的数据生存时间过期,提醒用户去更新缓存

1、开启Reids通知功能的配置
方法一:修改redis/redis.conf配置文件,添加如下设置   (永久有效)
[Shell] 纯文本查看 复制代码
notify-keyspace-events Ex

方法二:连接redis客户端,执行如下语句  (重启redis后失效)
[Shell] 纯文本查看 复制代码
config set notify-keyspace-events Ex

tip : Redis Config Set 命令可以动态地调整 Redis 服务器的配置(configuration),执行后无需重启,直接生效。
2、添加maven依赖 (注意版本问题)
[Java] 纯文本查看 复制代码
<dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>1.8.4.RELEASE</version>
</dependency>
<dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>

3、Redis配置
一:redis.properties
[Java] 纯文本查看 复制代码
#ip地址
redis.host = 114.215.83.3
#redis端口
redis.port = 6380
#auth 验证密码
redis.passwd = 123
#最大能够保持idle状态的对象数  
redis.maxIdle=300  
#最大分配的对象数    注意:低版本的jedis该属性名称是 maxActive
redis.maxTotal=600 
#当池内没有返回对象时,最大等待时间  注意:低版本的jedis该属性名称是 maxWait
redis.maxWaitMillis=1000  
#当调用borrow Object方法时,是否进行有效性检查  
redis.testOnBorrow=true 
#超时时间
redis.timeout=100000

二:spring整合redis的配置文件   applicationContext-redis.xml
[Java] 纯文本查看 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
 
 
        <!-- redis配置文件 -->
        <context:property-placeholder location="classpath:/redis.properties"
                ignore-unresolvable="true" />
 
        <!-- 配置连接池参数 -->
        <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
                <property name="maxIdle" value="${redis.maxIdle}" />
                <property name="maxTotal" value="${redis.maxTotal}" />
                <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
                <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        </bean>
 
        <!-- 配置连接工厂 -->
        <bean id="connectionFactory"
                class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
                <property name="hostName" value="${redis.host}" />
                <property name="port" value="${redis.port}" />
                <!-- 如果redis没有开启验证,password不需要配置 -->
                <property name="password" value="${redis.passwd}"></property>
                <property name="timeout" value="${redis.timeout}"></property>
                <property name="poolConfig" ref="poolConfig" />
        </bean>
 
        <!-- 配置redis消息订阅 -->
        <bean id="messageListener"
                class="org.springframework.data.redis.listener.adapter.MessageListenerAdapter">
                <constructor-arg>
                        <!-- 自定义消息处理类 -->
                        <bean class="com.mote.redis.listener.KeyExpiredNotice" />
                </constructor-arg>
        </bean>
        <bean id="redisContainer"
                class="org.springframework.data.redis.listener.RedisMessageListenerContainer">
                <property name="connectionFactory" ref="connectionFactory" />
                <property name="messageListeners">
                        <map>
                                <entry key-ref="messageListener">
                                        <list>
                                                <!-- 通配符:匹配消息通知类型 -->
                                                <bean class="org.springframework.data.redis.listener.PatternTopic">
                                                        <constructor-arg value="__key*__:expired" />
                                                </bean>
                                        </list>
                                </entry>
                        </map>
                </property>
        </bean>
</beans>

4、自定义消息处理类
[Java] 纯文本查看 复制代码
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;
 
@Component
public class KeyExpiredNotice extends MessageListenerAdapter {
 
        /**
         * msg : 通知消息,内含 key 
         * topic : 管道类型
         */
        @Override
        public void onMessage(Message msg, byte[] topic) {
                String key = new String(msg.getBody());
                // TODO 根据业务书写处理逻辑
                System.out.println(key);
                System.out.println(new String(topic));
        }
}





大体流程就是这样,下面总结一下自己实现该功能时遇到的一些问题

一:java.lang.NoSuchMethodError: org.springframework.util.Assert.isTrue(ZLjava/util/function/Supplier;)V

可能原因:spring-data-redis的版本和spring版本不兼容

网传 spring-data-redis 2的只支持spring5和spring boot2+ ,读者需要根据自己的开发环境,导入相应的版本依赖

二:java.lang.NoClassDefFoundError: redis/clients/util/Pool

可能原因:jedis版本过高,请尝试降低版本,查看是否继续报错




0 个回复

您需要登录后才可以回帖 登录 | 加入黑马