一、概念
当redis中的值发生改变时,通过配置来监听key值的变化。
事件通过 redis 的订阅与发布功能(pub/sub)来进行分发, 因此所有支持订阅与发布功能的客户端都可以在无须做任何修改的情况下, 直接使用键空间通知功能。
二、配置
因为开启键空间通知功能需要消耗一些 cpu , 所以在默认配置下, 该功能处于关闭状态。
可以通过修改 redis.conf 文件, 或者直接使用 config set 命令来开启或关闭键空间通知功能。
notify-keyspace-events 的参数可以是以下字符的任意组合, 它指定了服务器该发送哪些类型的通知:
第一种方式:直接使用命令
第一步:开启
config set notify-keyspace-events kea
第二步:订阅
另起一个窗口,用于监听。
psubscribe '__key*__:*' #对所有库键空间通知 psubscribe '__keyspace@5__:*' #是对db5数据库键空间通知 psubscribe '__keyspace@5__:order*' #是对db5数据库,key前缀为order所有键的键空间通知
出现以上形式表明订阅成功。
第三步:添加数据
set k1 v1
这就是配置成功了。
【推荐】第二种方式:修改配置文件
如果使用第一种方式的话,当关闭窗口时,再次使用的话,还需要重新输入,而修改配置文件则不用重复操作。
第一步:找配置文件
在安装的路径中找到redis.windows.conf
点击快捷键"ctrl+f",输入:notify-keyspace-events
第二步:修改配置文件
改为:notify-keyspace-events kea
点击"ctrl+s"保存并关闭
第三步:使用命令启动redis
将redis注册成一个服务启动,这样就不用每次开机手动启动了,而且不用一直用黑窗口打开了。
找到redis的安装路径,然后进入,输入cmd
输入命令:
redis-server.exe --service-install redis.windows.conf
这样在服务中就可以找到了。
三、整合springboot
1.加入依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency>
2.配置文件
spring: redis: # redis服务器地址 host: 127.0.0.1 # redis服务器端口号 port: 6379 # 使用的数据库索引,默认是0 database: 0 # 连接超时时间 timeout: 1800000 # 设置密码 password: lettuce: pool: # 最大阻塞等待时间,负数表示没有限制 max-wait: -1 # 连接池中的最大空闲连接 max-idle: 5 # 连接池中的最小空闲连接 min-idle: 0 # 连接池中最大连接数,负数表示没有限制 max-active: 20
3.配置类
package com.redisdemo.config; import com.example.redisdemo.controller.redisreceiver; import com.example.redisdemo.listener.*; import com.fasterxml.jackson.annotation.jsonautodetect; import com.fasterxml.jackson.annotation.propertyaccessor; import com.fasterxml.jackson.databind.objectmapper; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.data.redis.connection.redisconnectionfactory; import org.springframework.data.redis.core.redistemplate; import org.springframework.data.redis.listener.channeltopic; import org.springframework.data.redis.listener.patterntopic; import org.springframework.data.redis.listener.redismessagelistenercontainer; import org.springframework.data.redis.listener.adapter.messagelisteneradapter; import org.springframework.data.redis.serializer.genericjackson2jsonredisserializer; import org.springframework.data.redis.serializer.jackson2jsonredisserializer; import org.springframework.data.redis.serializer.redisserializer; import org.springframework.data.redis.serializer.stringredisserializer; import java.net.unknownhostexception; /** * @author lenovo */ @configuration public class redisconfig { @autowired private redistemplate<string, object> redistemplate; @autowired private redisupdateandaddlistener redisupdateandaddlistener; @autowired private redisdeletelistener redisdeletelistener; @autowired private redishashupdateandaddlistener redishashupdateandaddlistener; @autowired private redislistupdateandaddlistener redislistupdateandaddlistener; @autowired private redissetupdateandaddlistener redissetupdateandaddlistener; @autowired private redissortupdateandaddlistener redissortupdateandaddlistener; @bean redismessagelistenercontainer container(redisconnectionfactory connectionfactory) { redismessagelistenercontainer container = new redismessagelistenercontainer(); container.setconnectionfactory(connectionfactory); //监听所有的key的set事件 container.addmessagelistener(redisupdateandaddlistener, redisupdateandaddlistener.gettopic()); //监听所有key的删除事件 container.addmessagelistener(redisdeletelistener,redisdeletelistener.gettopic()); container.addmessagelistener(redishashupdateandaddlistener,redishashupdateandaddlistener.gettopic()); container.addmessagelistener(redislistupdateandaddlistener,redislistupdateandaddlistener.gettopic()); container.addmessagelistener(redissetupdateandaddlistener,redissetupdateandaddlistener.gettopic()); container.addmessagelistener(redissortupdateandaddlistener,redissortupdateandaddlistener.gettopic()); //监听所有key的过期事件 // container.addmessagelistener(redisexpiredlistener,redisexpiredlistener.gettopic()); return container; } @bean public redistemplate<string, object> redistemplate(redisconnectionfactory redisconnectionfactory) throws unknownhostexception { // 创建模板 redistemplate<string, object> redistemplate = new redistemplate<>(); // 设置连接工厂 redistemplate.setconnectionfactory(redisconnectionfactory); // 设置序列化工具 genericjackson2jsonredisserializer jsonredisserializer = new genericjackson2jsonredisserializer(); // key和 hashkey采用 string序列化 redistemplate.setkeyserializer(redisserializer.string()); redistemplate.sethashkeyserializer(redisserializer.string()); // value和 hashvalue采用 json序列化 redistemplate.setvalueserializer(redisserializer.string()); redistemplate.sethashvalueserializer(redisserializer.string()); return redistemplate; } }
4.监听类
监听所有类型的删除操作:
package com.redisdemo.listener; import lombok.data; import org.springframework.data.redis.connection.message; import org.springframework.data.redis.connection.messagelistener; import org.springframework.data.redis.listener.patterntopic; import org.springframework.stereotype.component; @component @data public class redisdeletelistener implements messagelistener { //监听主题 private final patterntopic topic = new patterntopic("__keyevent@*__:del"); /** * * @param message 消息 * @param pattern 主题 */ @override public void onmessage(message message, byte[] pattern) { string topic = new string(pattern); string msg = new string(message.getbody()); system.out.println("收到key的删除,消息主题是:"+ topic+",消息内容是:"+msg); } }
监听hash类型新增/修改:
package com.redisdemo.listener; import lombok.data; import org.springframework.data.redis.connection.message; import org.springframework.data.redis.connection.messagelistener; import org.springframework.data.redis.listener.patterntopic; import org.springframework.stereotype.component; @component @data public class redishashupdateandaddlistener implements messagelistener { //监听的主题 private final patterntopic topic = new patterntopic("__keyevent@*__:hset"); @override public void onmessage(message message, byte[] pattern){ string topic = new string(pattern); string msg = new string(message.getbody()); system.out.println("收到key更新或修改,消息主题是:"+ topic+",消息内容是:"+msg); } }
监听list类型的添加/修改:
package com.redisdemo.listener; import lombok.data; import org.springframework.data.redis.connection.message; import org.springframework.data.redis.connection.messagelistener; import org.springframework.data.redis.listener.patterntopic; import org.springframework.stereotype.component; @component @data public class redislistupdateandaddlistener implements messagelistener { //监听的主题 private final patterntopic topic = new patterntopic("__keyevent@*__:lpush"); @override public void onmessage(message message, byte[] pattern){ string topic = new string(pattern); string msg = new string(message.getbody()); system.out.println("收到key更新或修改,消息主题是:"+ topic+",消息内容是:"+msg); } }
监听set类型新增/修改:
package com.redisdemo.listener; import lombok.data; import org.springframework.data.redis.connection.message; import org.springframework.data.redis.connection.messagelistener; import org.springframework.data.redis.listener.patterntopic; import org.springframework.stereotype.component; @component @data public class redissetupdateandaddlistener implements messagelistener { //监听的主题 private final patterntopic topic = new patterntopic("__keyevent@*__:sadd"); @override public void onmessage(message message, byte[] pattern){ string topic = new string(pattern); string msg = new string(message.getbody()); system.out.println("收到key更新或修改,消息主题是:"+ topic+",消息内容是:"+msg); } }
监听zset类型新增/修改:
package com.redisdemo.listener; import lombok.data; import org.springframework.data.redis.connection.message; import org.springframework.data.redis.connection.messagelistener; import org.springframework.data.redis.listener.patterntopic; import org.springframework.stereotype.component; @component @data public class redissortupdateandaddlistener implements messagelistener { //监听的主题 private final patterntopic topic = new patterntopic("__keyevent@*__:zadd"); @override public void onmessage(message message, byte[] pattern){ string topic = new string(pattern); string msg = new string(message.getbody()); system.out.println("收到key更新或修改,消息主题是:"+ topic+",消息内容是:"+msg); } }
监听string类型新增/修改:
package com.redisdemo.listener; import lombok.data; import org.springframework.data.redis.connection.message; import org.springframework.data.redis.connection.messagelistener; import org.springframework.data.redis.listener.patterntopic; import org.springframework.stereotype.component; @component @data public class redisupdateandaddlistener implements messagelistener { //监听的主题 private final patterntopic topic = new patterntopic("__keyevent@*__:set"); @override public void onmessage(message message, byte[] pattern){ string topic = new string(pattern); string msg = new string(message.getbody()); system.out.println("收到key更新或修改,消息主题是:"+ topic+",消息内容是:"+msg); } }
5.启动项目
在redis图形化界面添加一个数据
这下就成功监听到了。
如果没有监听到,请确保redis正常启动了。
总结
到此这篇关于如何监听redis中key值的变化的文章就介绍到这了,更多相关监听redis中key值变化内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论