springboot+redis实现发布订阅模式

  • 引入依赖
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
  </dependency>
  • 自定义监听器,处理订阅后收到的msg
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;

public class ConsumerRedisListener implements MessageListener {


    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        doBusiness(message);
    }

    /**
     * 打印 message body 内容
     * @param message
     */
    public void doBusiness(Message message) {
        Object value = redisTemplate.getValueSerializer().deserialize(message.getBody());
        System.out.println("consumer message: " + value);
    }
}
  • 定义一个top常量
public class RedisTopicConst {

    public static final String  SCHEDULE_TOPIC = "schedule-topic";

}

  • 配置类

import com.xfr.consts.RedisTopicConst;
import com.xfr.listener.ConsumerRedisListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * @author osy
 */
@Configuration
public class RedisConfig {


    /**
     * 自动注入链接工厂
     */
    @Autowired
    private LettuceConnectionFactory lettuceConnectionFactory;

    /**
     * 将自定义的监听器加入spring容器
     * @return
     */
    @Bean
    public ConsumerRedisListener consumerRedis() {
        return new ConsumerRedisListener();
    }

    /**
     * 将topic加入spring容器
     * @return
     */
    @Bean
    public ChannelTopic topic() {
        return new ChannelTopic(RedisTopicConst.SCHEDULE_TOPIC);
    }

    /**
     * 注册消息监听容器,使监听器和topic进行绑定
     * @return
     */
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer() {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(lettuceConnectionFactory);
        container.addMessageListener(consumerRedis(),topic());
        return container;

    }
}
  • 测试一下,我这里是通过get请求跑接口测试的,也可以使用junit测试
import com.xfr.consts.RedisTopicConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author osy
 * @Date 2022/1/29
 * @Description: TODO
 **/
@RestController
public class TestController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("test")
    public String testRedisPublishSubscribe(){
        // 发布msg
        redisTemplate.convertAndSend(RedisTopicConst.SCHEDULE_TOPIC, "5");
        return "success";
    }

}
  • 最后看到控制台打印如下:
    在这里插入图片描述
  • 和我们自定义监听器那里对应起来

在这里插入图片描述

  • 使用redis和mq做发布订阅模式的区别:

redis: 轻量级,低延迟,高并发,低可靠性;
rabbitmq:重量级,高可靠,异步,不保证实时;
具体看实际业务进行选择

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值