redis 实现发布订阅的功能
redis 除了作为缓存的功能外还可以用作消息中间件的功能,这片博客主要是介绍一下 redis 整合spring 实现消息的发布和订阅功能;
1:redis依赖,依赖两个包,redis 包, spring-redis 包用于整合redis,这里就不介绍了,具体可以参考上一篇博客 :redis 缓存 中的介绍;
2:redis和spring的整合:
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="1" />
<property name="maxTotal" value="5" />
<property name="blockWhenExhausted" value="true" />
<property name="maxWaitMillis" value="30000" />
<property name="testOnBorrow" value="true" />
</bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost" />
<property name="port" value="6379"/>
<property name="poolConfig" ref="jedisPoolConfig" />
<property name="usePool" value="true"/>
</bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
</bean> <!-- redis 消息发布订阅 -->
<bean id="myRedisListener" class="bz.beppe.redis.MyRedisListener" scope="prototype"/>
<!-- 这里配置线程池任务 -->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="4"/>
<property name="maxPoolSize" value="4"/>
<property name="queueCapacity" value="100000"/>
</bean>
<!-- 配置redis container 将监听类注入到redis容器中,实现监听容器中指定 主题 队列的功能 -->
<bean id="redisContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer"
destroy-method="destroy">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="taskExecutor">
<ref bean="taskExecutor" />
</property>
<property name="messageListeners">
<map>
<entry key-ref="myRedisListener">
<bean class="org.springframework.data.redis.listener.ChannelTopic">
<constructor-arg value="push:myredis"/>
</bean>
</entry>
</map>
</property>
</bean>
3:监听类 该类只要实现 MessageListener 接口即可,并且在重写的方法中进行需要的业务处理:
public class MyRedisListener implements MessageListener{
@Override
public void onMessage(Message message, byte[] pattern) {
byte[] body = message.getBody();
byte[] channel = message.getChannel();
// redisTemplate.convertAndSend("push:myredis","this is the redis subscribe!!");
String channelStr = new String(channel);
String bodyStr = new String(body);
System.out.println("渠道为:"+channelStr+"消息为:"+bodyStr); //这里的业务处理只做简单的打印输出
}
}
4:消息发布类:
在消息发布类中需要使用到 redisTemplate 来进行消息的发布,其中,消息发布的方法为 redisTemplate.convertAndSend(String channel,String mess);
需要指定消息发布的 通道名称,这里的通道和监听中配置的渠道名称一致 ;mess 就是你需要发布到该通道上的内容;
5:消息的发布:
@Test
public void redisSubTest() throws InterruptedException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-redis.xml");
RedisTemplate redisTemplate = (RedisTemplate) ctx.getBean("redisTemplate");
redisTemplate.convertAndSend("push:myredis","this is the redis subscribe!!");
Thread.sleep(5000);
}
到这里为止,一个简单的基于redis的订阅发布就实现了,在项目中你可以根据你的具体业务来实现功能
redis 实现发布订阅的功能的更多相关文章
- 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能
springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...
- StackExchange.Redis 使用-发布订阅 (二)
使用Redis的发布订阅功能 redis另一个常见的用途是发布订阅功能 . 它非常的简单 ,当连接失败时 ConnectionMultiplexer 会自动重新进行订阅 . ISubscriber s ...
- .net core 使用Redis的发布订阅
Redis是一个性能非常强劲的内存数据库,它一般是作为缓存来使用,但是他不仅仅可以用来作为缓存,比如著名的分布式框架dubbo就可以用Redis来做服务注册中心.接下来介绍一下.net core 使用 ...
- java实现 redis的发布订阅(简单易懂)
redis的应用场景实在太多了,现在介绍一下它的几大特性之一 发布订阅(pub/sub). 特性介绍: 什么是redis的发布订阅(pub/sub)? Pub/Sub功能(means Publ ...
- redis的发布订阅、持久化存储、redis的主从复制
redis的发布订阅 1. 创建redis配置文件 vim /opt/redis_conf/reids-6379.conf mkdir /data/6379 redis-server redis-6 ...
- Redis之发布订阅
一 什么是发布订阅 发布订阅模式又叫观察者模式,它定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖它的对象都将得到通知 Redis 发布订阅(pub/sub)是一种消息通信模式: ...
- [翻译] C# 8.0 新特性 Redis基本使用及百亿数据量中的使用技巧分享(附视频地址及观看指南) 【由浅至深】redis 实现发布订阅的几种方式 .NET Core开发者的福音之玩转Redis的又一傻瓜式神器推荐
[翻译] C# 8.0 新特性 2018-11-13 17:04 by Rwing, 1179 阅读, 24 评论, 收藏, 编辑 原文: Building C# 8.0[译注:原文主标题如此,但内容 ...
- python之上下文管理、redis的发布订阅、rabbitmq
使用with打开文件的方式,是调用了上下文管理的功能 #打开文件的两种方法: f = open('a.txt','r') with open('a.txt','r') as f 实现使用with关闭s ...
- 【spring boot】【redis】spring boot 集成redis的发布订阅机制
一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...
随机推荐
- MySQL中基于mysqldump和二进制日志log-bin进行逻辑备份以及基于时间点的还原
本文出处:http://www.cnblogs.com/wy123/p/6956464.html 本文仅模拟使用mysqldump和log-bin二进制日志进行简单测试,仅作为个人学习笔记,可能离实际 ...
- (转)在T-SQL语句中访问远程数据库
https://www.cnblogs.com/lgx5/p/7821887.html 1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasou ...
- mysql命令行常用指令
一. 启动mysql:service mysql start 停止mysql:service mysql stop 重启mysql:service mysql restart 查看mysql服务状态: ...
- c#: 任务栏进度显示(TaskbarManager)
Win7及以上系统支持任务栏进度条,为有进度类应用显示进度于任务栏,甚为方便. 以c#之WinForm实现其,大多采用Windows API Code Pack这个方案,加多引用,比较繁琐,而我总也打 ...
- 如何在3GPP下载协议
以下载AT Commands协议为例. 1. 打开3GPP网址:http://www.3gpp.org/ 2. 打开Specification-Specification Numbering,如图.去 ...
- 前后端跨域 _ cross domain
1. 解决跨域既可以从前端, 也可以从后端. 参考好的网络资源: http://www.cnblogs.com/vajoy/p/4295825.html
- Eclipse 中打开 python 交互窗口
- [Python] Window机器上同时安装Python 2 和 Python 3,如何兼容切换使用?
不论python2还是python3,python可执行文件都叫python.exe,在cmd下输入python得到的版本号取决于环境变量里哪个版本的python路径更靠前. 切换的方法有3种(方法3 ...
- redis在游戏服务器中的使用初探(一) 环境搭建
这里我们尝试在游戏服务器中的数据处理中使用redis 通过该系列文章能够学习 redis的基本操作 源码编译 客户端开源库的编译和使用 以及在游戏服务器中的缓存使用 作为初次摸索 尽量使得环境简单 ...
- 设计模式之模板模式 template
设计模式 模板模式如果有一个流程如下step1();step2();step3();step4();step5();其中step3() step5()是需要用户自己编写使用其他步骤是固定的那么可以写成 ...