(三)延时任务篇——通过redis的zset数据结构,实现延迟任务实战

前言

在前一篇内容中我们介绍了如何使用redis key过期失效的监控,完成任务延时关闭的功能,同时官方并不支持使用此种方式实现,由于其安全性较低,存在数据丢失的情况。本节内容是对延迟任务的又一实现方案,通过redis zset的数据结构完成延迟任务。使用一个常量order作为key,订单id作为value值,任务的过期时间作为score值,通过定时任务取前100名的数据,比较score值和当前时间的大小,如果该值小于当前时间,则证明该订单已过期,完成关单的操作,并将该订单数据在redis中删除。

正文

  • 项目集成redis,可参考上一节内容

- 引入redis的pom依赖

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-pool2</artifactId>
</dependency>
 
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>

- 在application.yml中添加redis的配置信息

spring:
  data:
    redis:
      host: 127.0.0.1
      port: 6379
      database: 0
      connect-timeout: 30000
      timeout: 30000
      lettuce:
        pool:
          enabled: true
          max-active: 200
          max-idle: 50
          max-wait: -1
          min-idle: 10
        shutdown-timeout: 100
  •  创建订单测试接口
    @Operation(summary = "创建订单-redis-zset")
    @PostMapping("saveOrderByRedisZet")
    public ApiResponse saveOrderByRedisZet() {
        String orderId = String.valueOf(IdWorker.getId());
        stringRedisTemplate.opsForZSet().add("order", orderId, System.currentTimeMillis() + 60000);
        return ApiResponse.ok();
    }

  •  开启定时任务,监控订单的状态
package com.yundi.xyxc.tps.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;


@Slf4j
@EnableScheduling
@Component
public class OrderJob {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 注意:如果是分布式架构,这里需要使用分布式任务的方式,例如xxl-job或者使用分布式锁+定时任务
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void createOrder() {
        ZSetOperations<String, String> operations = stringRedisTemplate.opsForZSet();
        //查询过期排名前100的订单
        Set<ZSetOperations.TypedTuple<String>> top100 = operations.rangeWithScores("order", 0, 99);
        assert top100 != null;
        for (ZSetOperations.TypedTuple<String> tuple : top100) {
            log.info("Member: {}, Score: {}", tuple.getValue(), tuple.getScore());
            //订单小于当前时间,订单过期,删除该订单
            if (tuple.getScore() < System.currentTimeMillis()) {
                log.info("订单过期,删除订单:{}", tuple.getValue());
                operations.remove("order", tuple.getValue());
            }
        }
        log.info("--------------------------------------------------------------");

    }

}

PS: 此处需要注意的是,如果是分布式系统,此处需要使用分布锁+@Scheduled定时任务,保证数据的安全性,以免产生并发问题。同时需要保证定时任务的执行时间合理根据任务的实际完成时间。

  • 启动项目,通过接口调用订单创建接口

  • 查看关单输出,过期的订单被关单并删除

结语

使用redis的zset数据结构完成延迟任务,相较于监控key失效更加安全,不会出现数据的丢失,同时相较于某些mq消息中间件的延迟对列,更加灵活,可以设置任意时间的延迟过期任务。关于使用redis的zset数据结构,实现延迟任务的内容到这里就结束了。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厉害哥哥吖

您的支持是我创作下去的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值