Spring+Redis 訂閱鍵過期通知

項目需求:redis緩存的數據生存時間過期,提醒用戶去更新緩存

 

♦♦ Step one:開啓Reids通知功能的配置

方法一:修改redis/redis.conf配置文件,添加如下設置   (永久有效)

notify-keyspace-events Ex

方法二:連接redis客戶端,執行如下語句  (重啓redis後失效)

config set notify-keyspace-events Ex

tip : Redis Config Set 命令可以動態地調整 Redis 服務器的配置(configuration),執行後無需重啓,直接生效。

 

 ♦♦ Step two: 添加maven依賴 (注意版本問題)

<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>
</dependency>

 

♦♦ Step three:Redis配置

一:redis.properties

#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

<?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>

 ♦♦ Step four:自定義消息處理類

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));
	}
}

♦♦ Step five:測試

一:啓動項目,斷點調試

二:連接客戶端,設置鍵生存時間

 三:方法執行,查看打印結果

 

大體流程就是這樣,下面總結一下自己實現該功能時遇到的一些問題

一: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版本過高,請嘗試降低版本,查看是否繼續報錯

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章