springboot 整合redis 实现KeySpaceNotification 键空间通知
2019独角兽企业重金招聘Python工程师标准>>>
目录结构如下:
application.properties配置文件(redis的配置):
spring.redis.host=localhost
spring.redis.pool.max-idle=300
spring.redis.pool.max-wait=3000
spring.redis.timeout=3000
spring.redis.port=6379
SbootInitializer.java文件 :
该类的作用是为了让spingboot项目可以从我的tomcat服务器启动
package com.sboot.boot;import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;import com.sboot.builder.SbootBuilder;/*** 用于从外部容器启动* * @author * @version [版本号, 2017年12月21日]* @see [相关类/方法]* @since [产品/模块版本]*/
public class SbootInitializer extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(SbootBuilder.class);}
}
SbootBuilder.java文件
package com.sboot.builder;import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;/*** 工程组建类* * @author * @version [版本号, 2017年12月21日]* @see [相关类/方法]* @since [产品/模块版本]*/
@SpringBootApplication
//扫描包,使类中的注解组建生效
@ComponentScan(value = {"com.sboot"})
public class SbootBuilder {}
RedisConfig.java 文件
package com.sboot.redis;import java.util.ArrayList;
import java.util.List;import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;@Configuration
public class RedisConfig extends RedisAutoConfiguration { @Autowiredprivate RedisProperties properties;@Autowiredprivate ObjectProvider<RedisSentinelConfiguration> sentinelConfiguration;@Autowiredprivate ObjectProvider<RedisClusterConfiguration> clusterConfiguration;@Beanpublic RedisKeyListener redisKeyListener(){return new RedisKeyListener();}@Beanpublic RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);List<Topic> topics = new ArrayList<>();//监听0号库和1号库中的string 键的相关操作topics.add(new PatternTopic("__keyevent@0__:*"));topics.add(new PatternTopic("__keyevent@1__:*"));container.addMessageListener(redisKeyListener(), topics);return container;}
}
RedisKeyListener.java 文件
package com.sboot.redis;import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;/*** <一句话功能简述>* <功能详细描述>* * @version [版本号, 2017年12月29日]* @see [相关类/方法]* @since [产品/模块版本]*/
public class RedisKeyListener implements MessageListener{/***当0号库和1号库中的键发生相关改动该函数会被出发* @param message* @param pattern*/@Overridepublic void onMessage(Message message, byte[] pattern) {// TODO Auto-generated method stubSystem.out.println("********hasdfasdf***********");}}
pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.adyx.hubin.springboot</groupId><artifactId>sboot</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>sboot Maven Webapp</name><url>http://maven.apache.org</url><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><!-- Java操作redis使用的jar包 --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.7.4.RELEASE</version></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version></dependency><dependency><groupId>org.jdom</groupId><artifactId>jdom</artifactId><version>2.0.2</version></dependency><!-- junit单元测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency></dependencies><build><finalName>sboot</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><!-- <version>2.3.2</version> --><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build>
</project>
最后修改redis的配置文件
# K Keyspace events, published with __keyspace@<db>__ prefix.
# E Keyevent events, published with __keyevent@<db>__ prefix.
# g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
# $ String commands
# l List commands
# s Set commands
# h Hash commands
# z Sorted set commands
# x Expired events (events generated every time a key expires)
# e Evicted events (events generated when a key is evicted for maxmemory)
# A Alias for g$lshzxe, so that the "AKE" string means all the events.
#
# The "notify-keyspace-events" takes as argument a string that is composed
# of zero or multiple characters. The empty string means that notifications
# are disabled.
#
# Example: to enable list and generic events, from the point of view of the
# event name, use:
#
# notify-keyspace-events Elg
#
# Example 2: to get the stream of the expired keys subscribing to channel
# name __keyevent@0__:expired use:
#
# notify-keyspace-events Ex
#
# By default all notifications are disabled because most users don't need
# this feature and the feature has some overhead. Note that if you don't
# specify at least one of K or E, no events will be delivered.
#notify-keyspace-events ""
notify-keyspace-events Eg$
重启redis服务器
转载于:https://my.oschina.net/huhaoren/blog/1596702
springboot 整合redis 实现KeySpaceNotification 键空间通知相关推荐
- 大神教你实现redis键空间通知
最近在开发一个定时活动,而且活动是多个场次的.这个是后就需要在活动开始的时候推送信息给客户端,结束的时候也要推送一次.简单的设计方案就是将配置缓存在redis,然后每隔一秒就轮询reids,获取配置信 ...
- 集群空间服务器接收不到消息,解决Redis集群条件下键空间通知服务器接收不到消息的问题...
解决Redis集群条件下键空间通知服务器接收不到消息的问题 键空间通知介绍 键空间通知使得客户端可以通过订阅频道或模式, 来接收那些以某种方式改动了 Redis 数据集的事件. 可以通过对redis的 ...
- Redis键空间通知(Keyspace Notifications)
Redis Keyspace Notifications https://redis.io/topics/notifications Redis 是一个键值对数据库服务器,服务器中每个数据库都由 re ...
- springboot整合redis消息队列
前言 消息队列作为一种常用的异步通信解决方案,而redis是一款高性能的nosql产品,今天就给大家介绍一下,如何使用redis实现消息队列,并整合到springboot. 两个消息模型 1. 队列模 ...
- Redis学习(含 Springboot 整合 Redis)
Redis NoSQL (not only sql) 在现代的计算系统上每天网络上都会产生庞大的数据量. 这些数据有很大一部分是由关系数据库管理系统(RDBMS)来处理. 1970年 E.F.Codd ...
- SpringBoot整合redis实现发布订阅模式
Redis的发布订阅模式 发布订阅(Pub/Sub):目前广泛使用的通信模型,它采用事件作为基本的通信机制,提供大规模系统所要求的松散耦合的交互模式:订阅者(如客户端)以事件订阅的方式表达出它有兴趣接 ...
- SpringBoot整合Redis缓存
SpringBoot整合Redis缓存 一.缓存概念知识 1.是什么缓存 2.缓存的优缺点 3.为什么使用缓存 二.Redis概念知识 1.Redis简介 2.为什么用Redis作为缓存 3.Redi ...
- springboot整合redis实现HyperLogLog统计文章浏览量使用过期策略完成数据库同步
springboot整合redis实现HyperLogLog统计文章浏览量&&使用过期策略完成数据库同步 本文目录 springboot整合redis实现HyperLogLog统计文章 ...
- SpringBoot整合Redis+Redis缓存应用+Redis实现Session共享+...
一.SpringBoot整合Redis 1.导入依赖 <!--存在Redis依赖--> <dependency><groupId>org.springframewo ...
最新文章
- 江苏省三级偏硬试题样卷
- 您的请求参数与订单信息不一致_[淘客订单检测]淘宝客订单检测接口,淘客订单查询API...
- 2020计算机单招的大专,2020专科和单招的区别
- Java读取hdfs目录下所有文件_Java API 读取HDFS目录下的所有文件
- android 广告设置秒数,Android动态显示具体到秒的相聚时间
- C# WebService获取天气信息
- [2018.04.23 T3] 最大值
- 【图论】spfa算法详解
- 实现人脸手动祛痘效果---OpenCV-Python开发指南(60)
- com.itextpdf.text.exceptions.IllegalPdfSyntaxException: Unbalanced begin/end text operators.
- Java Web项目是怎么跑起来的?
- 鸿蒙 谷歌怕了,鸿蒙系统展示了华为的野心,难怪谷歌害怕
- 《穷查理宝典》思维导图
- YFCMF,设置跳转后台首页
- 什么是EJB?EJB是基于哪些技术实现的?
- Matlab中的FCM算法代码及中文详解
- WIFI等无线射频产品为什么要校准?
- 20171218Capstone培训班
- 摄影测量-后方交会与前方交会,相对定向与绝对定向,光束法
- 为什么公司的运营都会有负债的出现?
热门文章
- OpenCV代码提取:resize函数的实现
- 使用纯C++实现SQL Server2005 数据库读写操作详细步骤
- 【Ubuntu】ubuntu设置GUI程序自启动
- 如何拼通网络ip地址_如何解决IP地址冲突
- MySQL数据库job怎么写_数据库中job是什么意思
- 计算机网络7层协议模型,计算机网络(一) OSI七层模型及TCP/IP dubbo协议
- Java对线_新手如何通过练习打好Java基础?
- c文本框只能输入数字_VBA代码限制文本框的输入
- Java项目:平行志愿管理系统(java+Springboot+Maven+mybatis+Vue+Mysql)
- Java项目:学生管理系统(java+Springboot+Maven+mybatis+Vue+Mysql)