SpringBoot Redis 消息队列

参考

https://www.cnblogs.com/uniqueDong/p/15904837.html
https://www.cnblogs.com/wzh2010/p/17205390.html
https://blog.youkuaiyun.com/qq_16557637/article/details/121015736
https://developer.aliyun.com/article/1095035
https://blog.youkuaiyun.com/sco5282/article/details/132904956

消息队列

消息队列可以实现消息解耦、消息路由、异步处理、流量削峰填谷。主流消息队列有kafka, rabbitmq, rocketmq
Redis也可以实现消息队列。方式有

  1. list
  2. pub/sub
  3. stream

list

redis的list底层是链表,满足先进先出。
list实现队列比较方便。同时可以满足有序,消息去重。缺点是

  1. 没有订阅功能,消费者要主动查询队列。而为了避免频繁查询队列消耗CPU资源,可以采用阻塞式查询。redis中阻塞查询命令是brpop
  2. 无法保证可靠性。缺少消息确认机制,无法及时感知遗漏消息,导致数据不一致。

源码

完整项目在https://gitcode.com/zsss1/redis_mq/overview
pom.xml添加redisson依赖。

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
 <dependency>
     <groupId>org.redisson</groupId>
     <artifactId>redisson-spring-boot-starter</artifactId>
     <version>3.40.2</version>
 </dependency>

Redission封装依赖。

@SpringBootTest(classes = DemoApplication.class)
public class RedisListTest {
   
    @Autowired
    private RedissonClient client;

    private static final String REDIS_QUEUE = "list_queue";

    private static final Logger LOGGER = LoggerFactory.getLogger(RedisListTest.class);
    
    @Test
    public void test_redis_list_mq() throws Exception {
   
        RedissonBlockingDeque r;
        new Thread(() -> {
   
            for (
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值