前言
Redis相信对于各位开发者的朋友来说都不会陌生,特别是后端的朋友会经常性的用到Redis来进行数据缓存等操作,今日我们就来聊聊Redis的发布(Pub)与订阅(Sub)模式的在SpringBoot下如何使用,并谈谈相关的应用场景。
使用方法
首先我们需要配置redis的相关依赖包:
org.springframework.boot
spring-boot-starter-data-redis
然后我们需要配置Redis消息的监听容器:
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(listenerAdapter, new PatternTopic("topicName")); //配置名称
return container;
}
接着我们新建一个消息实体,用于发送数据:
@Data
@AllArgsConstructor
public class Notice implements Serializable {
private String name;
private String val;
}
现在基本配置和消息数据体我们都定义好了,接着我们需要配置一个订阅端,用于监听订阅的消息:
@Component
@Slf4j
public class LicoyWorkMessageListener extends MessageListenerAdapter {
@Autowired
private RedisTemplate redisTemplate;
@Override
public void onMessage(Message message, byte[] pattern) {
byte[] body = message.getBody();
byte[] channel = message.getChannel();
String topic = (String) redisTemplate.getStringSerializer().deserialize(channel);
if(!topic.equals("topicName")){
return;
}
Object res = redisTemplate.getValueSerializer().deserialize(body);
if(res instanceof Notice){ //如果反序列化得到的是我们定义的消息数据体类型
Notice notice = (Notice)res;
System.out.println(notice);
}else{
//其他处理
}
}
}
到这一步,万事俱备只欠东风了,现在我们只需要在需要用到的地方发布消息即可,如(作为演示,我直接在部分逻辑写在controller层,实际业务不建议这么做):
@GetMapping("/notice/{name}/{val}")
public ResponseResult notice(@PathVariable String name,@PathVariable String val){
redisTemplate.convertAndSend("topicName", new Notice(name,val));
return ResponseResult.ok();
}
使用场景
对于使用场景,就谈谈博主在开发中所用到的地方之一,一个平台,其分别有用户端/商家端,包含一个简单的Socket连接,用于商家及用户工单会话的消息通知,但是这两个是单独的Application,所以各自的Socket-Session分别保存在对应的内存栈中,因为不想让此复杂所以就没有采用其他框架就默认使用Springboot内置的WebSocket,但是WebSocketSession这个对象不支持序列化,所以就无法缓存到Redis中,那么只有(方法不一)两个端同时订阅一个频道,然后有新消息的时候通知两个端分别进行处理来发送消息给用户还是商家。
尾记
在实际开发的过程中,我们所遇到的问题会远比自己所设想的多,但是这并不是100%的,只要思想不滑坡,办法总比困难多,加油!
另外双十二即将来临,你准备再一波剁手了么