Redis之订阅发布

       消息的发送者与接收者之间通过 channel 绑定:channel 可以是确定的字符串,也可以基于模式匹配;客户端可以订阅任意多个 channel;发送者发送的消息无法持久化,所以可能会造成消息丢失;由于消息无法持久化,所以,消费者无法收到在订阅 channel 之前发送的消息;发送者与客户端之间的消息发送与接收不存在 ACK 机制

  由于没有消息持久化与 ACK 的保证,所以,Redis 的发布订阅功能并不可靠。这也就导致了它的应用场景很有限,建议用于实时与可靠性要求不高的场景。例如:

  • 消息推送
  • 内网环境的消息通知

  总之,Redis 发布订阅功能足够简单,如果没有过多的要求,且不想搭建 Kafka、RabbitMQ 这样的可靠型消息系统时,可以考虑尝试使用 Redis。

       Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。发布订阅机制包括三个部分,发布者,订阅者和Channel。

  

  • 发布者和订阅者是Redis客户端,Channel则为Redis服务器端,发布者将消息发送到某个的频道,订阅这个频道的订阅者就能接收到这条消息。
  • Redis的这种发布订阅机制与基于主题的发布订阅类似,Channel相当于主题。
SUBSCRIBE pattern [pattern ...]
订阅一个或者多个符合模式匹配的频道

PUBLISH channel message 
发送消息到指定的频道

SUBSCRIBE channel [channel ...] 
订阅一个或多个频道

PUNSUBSCRIBE [pattern [pattern ...]] 
退订所有符合模式匹配的频道

UNSUBSCRIBE [channel [channel ...]] 
退订一个或多个频道

  

   

   打开另一个redis-cli 客户端(client3),发布信息,如下:

  

   按照规则订阅 可以使用PSUBSCRIBE命令订阅指定的规则

127.0.0.1:6379> psubscribe ay_test*

  org.springframework.data.redis.listener.Topic  消息发送者与接收者之间的 channel 定义,有两个实现类:

  • org.springframework.data.redis.listener.ChannelTopic:一个确定的字符串
  • org.springframework.data.redis.listener.PatternTopic:基于模式匹配

       org.springframework.data.redis.connection.MessageListener

       一个回调接口,消息监听器,用于接收发送到 channel 的消息

    org.springframework.data.redis.listener.RedisMessageListenerContainer

  用于消息监听,需要将 Topic 和 MessageListener 注册到 RedisMessageListenerContainer 中。这样,当 Topic 上有消息时,由 RedisMessageListenerContainer 通知 MessageListener,客户端通过 onMessage 拿到消息后,自行处理

@Slf4j
@Configuration
public class ListenerConfig {

    @Bean
    public RedisMessageListenerContainer listenerContainer(RedisConnectionFactory connectionFactory,
                                                           MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("news"));
        container.addMessageListener(listenerAdapter, new PatternTopic("games"));
        return container;
    }
}
@Slf4j
@Component
public class RedisSubscriber extends MessageListenerAdapter {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        byte[] body = message.getBody();
        byte[] channel = message.getChannel();
        String data = redisTemplate.getStringSerializer().deserialize(body);
        String topic = redisTemplate.getStringSerializer().deserialize(channel);
        log.info("监听到topic为{}的消息为{}", topic, data);
    }
}
@Slf4j
@RestController
@RequestMapping("/topic")
public class TopicController {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("news")
    public String newsMessage(@RequestParam("data") String data) {
        redisTemplate.convertAndSend("news", data);
        return "ok";
    }
}

 

 

  

posted on 2023-03-25 20:28  溪水静幽  阅读(97)  评论(0)    收藏  举报