Redis订阅广播实现多级缓存
Redis应用场景很多,现在介绍一下它的几大特性之一 发布订阅(pub/sub)
特性介绍:
什么是redis的发布订阅(pub/sub)? Pub/Sub功能(means Publish, Subscribe)即发布及订阅功能。基于事件的系统中,Pub/Sub是目前广泛使用的通信模型,它采用事件作为基本的通信机制,提供大规模系统所要求的松散耦合的交互模式:订阅者(如客户端)以事件订阅的方式表达出它有兴趣接收的一个事件或一类事件;发布者(如服务器)可将订阅者感兴趣的事件随时通知相关订阅者。熟悉设计模式的朋友应该了解这与23种设计模式中的观察者模式极为相似。
同样,Redis的pub/sub是一种消息通信模式,主要的目的是解除消息发布者和消息订阅者之间的耦合, Redis作为一个pub/sub的server, 在订阅者和发布者之间起到了消息路由的功能。
上面的都是概念,不知道没关系,其实我也看不懂。
简单来讲,这里面还有个channel的概念,这里就是频道的意思,比如你订阅了银行的频道,当你的资金发生变动时,银行就会通过它的频道给你发送信息,在这里,你是属于被动接收的,而不是向银行索要信息,这个例子中,你就是sub(订阅者),而银行就是pub(发布者)。
代码:
先引入依赖
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.9.0</version>
- </dependency>
新建一个发布者
- public class Publisher extends Thread{
- private final JedisPool jedisPool;
- public Publisher(JedisPool jedisPool) {
- this.jedisPool = jedisPool;
- }
- @Override
- public void run() {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- Jedis jedis = jedisPool.getResource(); //连接池中取出一个连接
- while (true) {
- String line = null;
- try {
- line = reader.readLine();
- if (!"quit".equals(line)) {
- jedis.publish("mychannel", line); //从 mychannel 的频道上推送消息
- } else {
- break;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
新建一个订阅者
- public class Subscriber extends JedisPubSub {
- public Subscriber(){}
- @Override
- public void onMessage(String channel, String message) { //收到消息会调用
- System.out.println(String.format("receive redis published message, channel %s, message %s", channel, message));
- }
- @Override
- public void onSubscribe(String channel, int subscribedChannels) { //订阅了频道会调用
- System.out.println(String.format("subscribe redis channel success, channel %s, subscribedChannels %d",
- channel, subscribedChannels));
- }
- @Override
- public void onUnsubscribe(String channel, int subscribedChannels) { //取消订阅 会调用
- System.out.println(String.format("unsubscribe redis channel, channel %s, subscribedChannels %d",
- channel, subscribedChannels));
- }
- }
订阅频道
- public class SubThread extends Thread {
- private final JedisPool jedisPool;
- private final Subscriber subscriber = new Subscriber();
- private final String channel = "mychannel";
- public SubThread(JedisPool jedisPool) {
- super("SubThread");
- this.jedisPool = jedisPool;
- }
- @Override
- public void run() {
- System.out.println(String.format("subscribe redis, channel %s, thread will be blocked", channel));
- Jedis jedis = null;
- try {
- jedis = jedisPool.getResource(); //取出一个连接
- jedis.subscribe(subscriber, channel); //通过subscribe 的api去订阅,入参是订阅者和频道名
- } catch (Exception e) {
- System.out.println(String.format("subsrcibe channel error, %s", e));
- } finally {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
- }
测试下
- public class PubSubDemo {
- public static void main( String[] args )
- {
- // 连接redis服务端
- JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "172.31.12.18", 7379);
- System.out.println(String.format("redis pool is starting, redis ip %s, redis port %d", "127.0.0.1", 7379));
- SubThread subThread = new SubThread(jedisPool); //订阅者
- subThread.start();
- Publisher publisher = new Publisher(jedisPool); //发布者
- publisher.start();
- }
- }
打印信息

Redis订阅广播实现多级缓存的更多相关文章
- redis订阅发布消息操作本地缓存
Redis 本地缓存+远程缓存方案 使用纯java的ehcache作为本地缓存 Reids 作为远程分布式缓存 解决redis缓存压力过大,提高缓存速度,以及缓存性能. Redis和ehcache缓存 ...
- Redis 多级缓存架构和数据库与缓存双写不一致问题
采用三级缓存:nginx本地缓存+redis分布式缓存+tomcat堆缓存的多级缓存架构 时效性要求非常高的数据:库存 一般来说,显示的库存,都是时效性要求会相对高一些,因为随着商品的不断的交易,库存 ...
- Redis: 缓存过期、缓存雪崩、缓存穿透、缓存击穿(热点)、缓存并发(热点)、多级缓存、布隆过滤器
Redis: 缓存过期.缓存雪崩.缓存穿透.缓存击穿(热点).缓存并发(热点).多级缓存.布隆过滤器 2019年08月18日 16:34:24 hanchao5272 阅读数 1026更多 分类专栏: ...
- 【开源项目系列】如何基于 Spring Cache 实现多级缓存(同时整合本地缓存 Ehcache 和分布式缓存 Redis)
一.缓存 当系统的并发量上来了,如果我们频繁地去访问数据库,那么会使数据库的压力不断增大,在高峰时甚至可以出现数据库崩溃的现象.所以一般我们会使用缓存来解决这个数据库并发访问问题,用户访问进来,会先从 ...
- 带你100% 地了解 Redis 6.0 的客户端缓存
近日 Redis 6.0.0 GA 版本发布,这是 Redis 历史上最大的一次版本更新,包括了客户端缓存 (Client side caching).ACL.Threaded I/O 和 Redis ...
- 有赞透明多级缓存解决方案(TMC)设计思路
引子 TMC 是什么 TMC,即"透明多级缓存(Transparent Multilevel Cache)",是有赞 PaaS 团队给公司内应用提供的整体缓存解决方案. TMC 在 ...
- Redis整合Spring结合使用缓存实例(三)
一.Redis介绍 什么是Redis? redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set( ...
- Redis整合Spring结合使用缓存实例
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的 ...
- Redis整合Spring结合使用缓存实例(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的 ...
随机推荐
- Django rest-framework框架-访问频率控制
第一版: from rest_frameworkclass VisitThrottle(object): def __init__(self): self.history = None def all ...
- 如何使用classnames模块库为react动态添加class类样式
摘要 在react中添加动态的css时,传统的方式较为繁琐,今天刚好学习到一个模块库可以便捷的解决这个问题.对的,它就是“classnames”. classnames模块库 npm安装 npm in ...
- 垃圾分类,javascript和python
首先,实现的步骤,首先在微信applet中设计一个简单的界面,开始映射到python服务器.有关具体界面,请参阅微信小程序设计指南.以下主要讨论后台服务器交互和处理点. 1.使用js将图像上传到pyt ...
- JavaScript02
一. 判断// 三元表达式// 循环// 判断: 用的最多的就是if判断// 1.// if(条件){// 当条件满足以后执行的语句// } // 2.// if(条件){// // }else{// ...
- split分离特殊字符
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ ) \b \t \n \f \r \ ...
- 3.web开发入门知识
/*web入门*/ /*互联网上常用的协议以及它的端口*/ http 80 http://localhost/ 相当于 http://localhost:80/ http协 ...
- flask开发环境
1. 创建虚拟环境flask_py3 虚拟环境是一个互相隔离的目录 mkvirtualenv flask_py3 2.安装flask包 pip install flask==0.10.1 其他:导入f ...
- 如何将公式插入到word
平台:win10 x64+ office 2010+ Mathpix Snipping Tool +mathtype6.9b 直接安装就行,下载好了以后,要和word连接起来还需要下载一个插件,有 ...
- 12_Hive实战案例_累计报表_级联求和
注:Hive面试题:累积报表 数据文件: 有如下访客访问次数统计表 t_access_times 需要输出报表:t_access_times_accumulate 实现步骤: 创建表,并将数据加载到表 ...
- TcxComboBoxProperties下拉框填充
原文地址:https://www1.devexpress.com/Support/Center/Question/Details/CQ30369 Actually, the corresponding ...