不操千曲而后晓声,观千剑而后识器。这篇文章主要讲述SpringBoot整合Redis实现简单的Pub/Sub(发布/订阅)相关的知识,希望能为你提供帮助。
直接上代码
引入依赖
```html/xml
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-data-redis<
/artifactId>
<
/dependency>
创建RedisListener实现MessageListener
```java
package com.test.demotest.service;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
/**
* @Author: yipeng.liu
* @Date: 2022/3/2 18:20
* @Description: RedisListener
*/
@Log4j2
@Configuration
public class RedisListener implements MessageListener @Override
public void onMessage(Message message, byte[] bytes)
//获取订阅消息内容
String topic = new String(bytes);
String context = new String(message.getBody());
log.info("topic:,context:",topic,context);
重写RedisMessageListenerContainer Bean订阅所要监听的topic
package com.test.demotest.service;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
/**
* @Author: yipeng.liu
* @Date: 2022/3/2 18:20
* @Description: RedisListener
*/
@Log4j2
@Configuration
public class RedisListener implements MessageListener @Override
public void onMessage(Message message, byte[] bytes)
String topic = new String(bytes);
String context = new String(message.getBody());
log.info("topic:,context:",topic,context);
@Bean
RedisMessageListenerContainer redisMessageListenerContainer(
RedisConnectionFactory redisConnectionFactory,RedisListener redisListener)
RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
//订阅topic - subscribe
redisMessageListenerContainer.addMessageListener(redisListener,new ChannelTopic("subscribe"));
return redisMessageListenerContainer;
编写测试类
package com.test.demotest.contoller;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
/** @Author: yipeng.liu @Date: 2022/3/2 18:43 @Description: RedisTest */
@SpringBootTest
public class RedisTest @Autowired private StringRedisTemplate stringRedisTemplate;
@Test
void redisTest() throws InterruptedException
int i = 0;
while (i <
10)
i++;
stringRedisTemplate.convertAndSend("subscribe", "发布信息" + i);
Thread.sleep(3000);
【SpringBoot整合Redis实现简单的Pub/Sub(发布/订阅)】运行结果有如下:
文章图片
这里要说一下
为什么使用StringRedisTemplate而不使用RedisTemplate
RedisTemplate是使用JdkSerializationRedisSerializer进行序列化
StringRedisTemplate是使用StringRedisSerializer进行序列化
RedisTemplate会将数据序列化为字节数组储存到Redis中
StringRedisTemplate会将数据序列化为可读数组储存下来
注意:只要当你要存的数据是字符串时才是用StringRedisTemplate
推荐阅读
- 自编码器
- Ubuntu系统上双节点部署OpenStack
- CentOS7.2调整Mysql数据库最大连接数
- Linux中一个网卡含有多个IP,将从IP升级为主IP的方法
- OpenStack中的虚拟机(/dev/mapper/centos-root)进行磁盘扩容
- Numpy求均值中位数众数的方法
- Android包体积优化上篇- 资源混淆优化
- 聊聊redo log是什么()
- 由于 VoIP 成为目标,DDoS 攻击在第三季度激增 35%