栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Delay - 如何用 Redis 打造一个延迟队列、广播(附加工具)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Delay - 如何用 Redis 打造一个延迟队列、广播(附加工具)

Delay - 如何用 Redis 打造一个延迟队列、广播(附加描述) 1. 工具

当然在写一个项目的时候肯定是会造一些工具类的轮子的,在 Redis 延迟队列中,涉及到的工具如下:

1.1 智能睡眠工具

睡眠工具是根据搬运线程每次搬运之后返回剩余的队列中最小的分数,因为在 zset 中分数是用时间戳,所以拿到最小的时间戳就可以计算出本次睡眠的时间:

public class SmartSleepUtil {
    
    private static final int MAX_SLEEP_SEC = 5 * 60 * 1000;
    
    private volatile static Thread transferThread = null;

    
    public static synchronized void immediatelyArouse () {
        if (Objects.nonNull(transferThread)) {
            LockSupport.unpark(transferThread);
            transferThread = null;
        }
    }

    
    public static void sleep (long min) {
        transferThread = Thread.currentThread();
        if (min == 0) {
            LockSupport.parkNanos(MAX_SLEEP_SEC * Constant.nanos);
            transferThread = null;
            return;
        }
        if (System.currentTimeMillis() >= min) {
            transferThread = null;
            return;
        }
        long sleepTime = min - System.currentTimeMillis();
        if (sleepTime == 0) {
            transferThread = null;
            return;
        }
        LockSupport.parkNanos(sleepTime * Constant.nanos);
        transferThread = null;
    }
}
1.2 spring boot starter 注解驱动
@Target(ElementType.TYPE)
@Import(RedisDelayQueueAutoConfiguration.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface EnableDelayQueue {
}


@AllArgsConstructor
@EnableConfigurationProperties(RedisDelayQueueProperties.class)
public class RedisDelayQueueAutoConfiguration {

    private Environment environment;
    private RedisDelayQueueProperties properties;

    
    @PostConstruct
    public void parseServerPort () {
        final String port = this.environment.getProperty(Constant.serverPort);
        final String address = IpUtils.getAddress();
        String isolation = address + Constant.underline + port;
        isolation = DigestUtils.md5DigestAsHex(isolation.getBytes(StandardCharsets.UTF_8)).toUpperCase(Locale.ROOT);
        isolation = isolation.substring(0, Constant.isolation);
        this.properties.setIsolation(isolation);
    }

    @Bean
    public DelayQueueContextFactory delayQueueCoreContext (RedissonClient client) {
        return new DelayQueueContextFactory(this.properties, client);
    }

    @Bean
    public RedissonClient redissonClient () {
        Config config = this.properties.getConfig();
        return Redisson.create(config);
    }

    @Bean
    public ProviderDelayJob providerDelayQueue (RedissonClient client) {
        return this.delayQueueCoreContext(client).getProviderDelayQueue();
    }

    @Bean
    public CrashHandlerContext shutdownHandlerContext (DelayQueueContextFactory factory) {
        return new CrashHandlerContext<>(factory, this.properties);
    }

    
    @Bean
    @ConditionalOnProperty(prefix = "delay.queue", name = "enable-radio", havingValue = "true")
    public DelayRadioContextFactory radioQueueContextFactory (RedissonClient redissonClient, RedisDelayQueueProperties properties) {
        return new DelayRadioContextFactory(redissonClient, properties);
    }

}
2. lua 脚本

lua 脚本只展示搬运脚本,其他脚本都非常简单

## 构造返回结果
local resultMap = {}
## 查看 zset 中剩余未搬运 TOPIC 数量
local topicCount = redis.call('ZCard', KEYS[2])
if (tonumber(topicCount) > 0)
then
	## 根据当前传入的时间戳,拿到时间都小于当前的所有 topic
    local member = redis.call('ZRangeByScore', KEYS[2], 0, tonumber(ARGV[1]))
    if next(member) == nil
    then
    	## 成功搬运的 TOPIC
        table.insert(resultMap, '')
        local min = redis.call('ZRangeByScore', KEYS[2], '-inf', '+inf', 'WithScores', 'limit', 0, 1)
        ## 剩余的 TOPIC 数量
        table.insert(resultMap, min)
        ## 广播的 TOPIC
        table.insert(resultMap, '')
        return resultMap
    else
    	## 准备变量,装填已经搬运的 topic,返回给 Java
        local readyTopic = ''
        ## 准备变量,装填需要广播的 topic
        local publishTopic = ''
        ## 便利读取出来的内容
        for key, value in ipairs(member)
        do	
        	## 循环内,每个都要去验证以下时间是不是满足被消费的条件
            local score = redis.call('ZScore', KEYS[2], value)
            if tonumber(ARGV[1]) >= tonumber(score)
            then
            	## 字符串切分,没啥好说的
                local index = string.find(value, ':')
                local subTopic = string.sub(value, 1, index - 1)
                local topic = string.gsub(subTopic, '"', '')
                ## 取值,看看在 TOPIC 上是否有广播的标识
                local radioTag = string.find(topic, ARGV[3])
                if radioTag == nil
                then
                	## 这种写法是为了避免 阿里云 对 lua 脚本的限制,实际上
                	## Java 传入的参数中,没有数组下标为 4 的内容
                    KEYS[4] = KEYS[1] ..':'.. ARGV[2] ..':'..topic
                    ## 将数据添加到 set 集合中
                    redis.call('SAdd', KEYS[4], value)
                    ## 删除掉 zset 里面的内容
                    redis.call('ZRem', KEYS[2], value)
                    ## 重新赋值,不然会内容叠加
                    KEYS[4] = ''
                    readyTopic = readyTopic .. ',' .. value
                else
                	## 处理广播,只需要返回给 Java 即可,后续的逻辑由 Java 来发布
                    redis.call('ZRem', KEYS[2], value)
                    publishTopic = publishTopic .. ',' .. value
                end
            else
                return nil;
            end
        end
        ## 重新读取一次剩余队列中最小的分数,提供给下次计算
        local min = redis.call('ZRangeByScore', KEYS[2], '-inf', '+inf', 'WithScores', 'limit', 0, 1)
        ## 装填数据,返回
        table.insert(resultMap, readyTopic)
        table.insert(resultMap, min)
        table.insert(resultMap, publishTopic)
        return resultMap
    end
    return nil
end
return nil
3. 完结总结

说明:本文所写的项目是 无法开源 的,通过几篇文章的表述,已经将核心的代码与设计思想梳理清楚,如果疑问,可以联系博主一起优化和重新设计。

本项目的来源也是对公司的一个 2017 年的现有功能进行了全面优化和提升性能和重构,项目开发周期大概一个月时间,因为其中也遇到过一些棘手的问题需要解决,并且利用的是业余时间进行开发的,所以周期相对拉长了很多。

对我而言,开发这个项目确实有一定的提升,起码奠定了我对中间件的认知程度,让我对中间件开发有了更深入的兴趣。

完 …

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/919883.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号