8种幂等解决重复提交方案(下)

简介: 8种幂等解决重复提交方案

image.png

在 pom.xml 中添加上 starter-web、starter-aop、starter-data-redis 的依赖即可:

<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-aopartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-data-redisartifactId>
    dependency>
dependencies>

属性配置 在 application.properites 资源文件中添加 redis 相关的配置项:

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=123456

主要实现方式: 熟悉 Redis 的朋友都知道它是线程安全的,我们利用它的特性可以很轻松的实现一个分布式锁,如 opsForValue().setIfAbsent(key,value)它的作用就是如果缓存中没有当前 Key 则进行缓存同时返回 true 反之亦然。


当缓存后给 key 在设置个过期时间,防止因为系统崩溃而导致锁迟迟不释放形成死锁;那么我们是不是可以这样认为当返回 true 我们认为它获取到锁了,在锁未释放的时候我们进行异常的抛出……

package com.battcn.interceptor;
import com.battcn.annotation.CacheLock;
import com.battcn.utils.RedisLockHelper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import java.lang.reflect.Method;
import java.util.UUID;
/**
 * redis 方案
 *
 * @author Levin
 * @since 2018/6/12 0012
 */
@Aspect
@Configuration
public class LockMethodInterceptor {
    @Autowired
    public LockMethodInterceptor(RedisLockHelper redisLockHelper, CacheKeyGenerator cacheKeyGenerator) {
        this.redisLockHelper = redisLockHelper;
        this.cacheKeyGenerator = cacheKeyGenerator;
    }
    private final RedisLockHelper redisLockHelper;
    private final CacheKeyGenerator cacheKeyGenerator;
    @Around("execution(public * *(..)) && @annotation(com.battcn.annotation.CacheLock)")
    public Object interceptor(ProceedingJoinPoint pjp) {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        CacheLock lock = method.getAnnotation(CacheLock.class);
        if (StringUtils.isEmpty(lock.prefix())) {
            throw new RuntimeException("lock key don't null...");
        }
        final String lockKey = cacheKeyGenerator.getLockKey(pjp);
        String value = UUID.randomUUID().toString();
        try {
            // 假设上锁成功,但是设置过期时间失效,以后拿到的都是 false
            final boolean success = redisLockHelper.lock(lockKey, value, lock.expire(), lock.timeUnit());
            if (!success) {
                throw new RuntimeException("重复提交");
            }
            try {
                return pjp.proceed();
            } catch (Throwable throwable) {
                throw new RuntimeException("系统异常");
            }
        } finally {
            // TODO 如果演示的话需要注释该代码;实际应该放开
            redisLockHelper.unlock(lockKey, value);
        }
    }
}

RedisLockHelper 通过封装成 API 方式调用,灵活度更加高:

package com.battcn.utils;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.util.StringUtils;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
/**
 * 需要定义成 Bean
 *
 * @author Levin
 * @since 2018/6/15 0015
 */
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisLockHelper {
    private static final String DELIMITER = "|";
    /**
     * 如果要求比较高可以通过注入的方式分配
     */
    private static final ScheduledExecutorService EXECUTOR_SERVICE = Executors.newScheduledThreadPool(10);
    private final StringRedisTemplate stringRedisTemplate;
    public RedisLockHelper(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }
    /**
     * 获取锁(存在死锁风险)
     *
     * @param lockKey lockKey
     * @param value value
     * @param time 超时时间
     * @param unit 过期单位
     * @return true or false
     */
    public boolean tryLock(final String lockKey, final String value, final long time, final TimeUnit unit) {
        return stringRedisTemplate.execute((RedisCallback) connection -> connection.set(lockKey.getBytes(), value.getBytes(), Expiration.from(time, unit), RedisStringCommands.SetOption.SET_IF_ABSENT));
    }
    /**
     * 获取锁
     *
     * @param lockKey lockKey
     * @param uuid UUID
     * @param timeout 超时时间
     * @param unit 过期单位
     * @return true or false
     */
    public boolean lock(String lockKey, final String uuid, long timeout, final TimeUnit unit) {
        final long milliseconds = Expiration.from(timeout, unit).getExpirationTimeInMilliseconds();
        boolean success = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, (System.currentTimeMillis() + milliseconds) + DELIMITER + uuid);
        if (success) {
            stringRedisTemplate.expire(lockKey, timeout, TimeUnit.SECONDS);
        } else {
            String oldVal = stringRedisTemplate.opsForValue().getAndSet(lockKey, (System.currentTimeMillis() + milliseconds) + DELIMITER + uuid);
            final String[] oldValues = oldVal.split(Pattern.quote(DELIMITER));
            if (Long.parseLong(oldValues[0]) + 1 <= System.currentTimeMillis()) {
                return true;
            }
        }
        return success;
    }
    /**
     * @see Redis Documentation: SET
     */
    public void unlock(String lockKey, String value) {
        unlock(lockKey, value, 0, TimeUnit.MILLISECONDS);
    }
    /**
     * 延迟unlock
     *
     * @param lockKey key
     * @param uuid client(最好是唯一键的)
     * @param delayTime 延迟时间
     * @param unit 时间单位
     */
    public void unlock(final String lockKey, final String uuid, long delayTime, TimeUnit unit) {
        if (StringUtils.isEmpty(lockKey)) {
            return;
        }
        if (delayTime <= 0) {
            doUnlock(lockKey, uuid);
        } else {
            EXECUTOR_SERVICE.schedule(() -> doUnlock(lockKey, uuid), delayTime, unit);
        }
    }
    /**
     * @param lockKey key
     * @param uuid client(最好是唯一键的)
     */
    private void doUnlock(final String lockKey, final String uuid) {
        String val = stringRedisTemplate.opsForValue().get(lockKey);
        final String[] values = val.split(Pattern.quote(DELIMITER));
        if (values.length <= 0) {
            return;
        }
        if (uuid.equals(values[1])) {
            stringRedisTemplate.delete(lockKey);
        }
    }
}
目录
相关文章
|
Ubuntu
避坑指南之Samba4在Ubuntu20.04 编译安装指南
避坑指南之Samba4在Ubuntu20.04 编译安装指南
1198 0
避坑指南之Samba4在Ubuntu20.04 编译安装指南
|
存储 设计模式 网络协议
AD域 概述以及结构与存储技术
AD域 概述以及结构与存储技术
1696 0
AD域 概述以及结构与存储技术
|
存储 Kubernetes 关系型数据库
在Kubernetes中,helm是什么?如何使用?
【4月更文挑战第9天】在Kubernetes中,helm是什么?如何使用?
1241 5
|
11月前
|
Web App开发 人工智能
UC伯克利:给大模型测MBTI,Llama更敢说但GPT-4像理工男
UC伯克利研究团队推出VibeCheck系统,自动比较大型语言模型(LLM)的输出特征,如语调、格式和写作风格。该系统通过迭代挖掘特征并利用LLM法官量化其实用性,验证结果显示其能有效捕捉模型的独特“vibes”。VibeCheck应用于对话、摘要、数学和字幕生成等任务,揭示了不同模型的行为差异,并在预测模型身份和用户偏好方面表现出色。尽管存在主观性和测试范围有限的局限性,VibeCheck为改进LLM评估提供了新视角。论文地址:https://arxiv.org/abs/2410.12851
315 98
|
存储 人工智能 自然语言处理
轻松改造公众号:10分钟实现智能客服自动化!
在阿里云平台上,仅需10分钟即可将微信公众号(订阅号)升级为AI智能客服,提供7x24小时客户支持,显著提升用户体验。方案包括四步:创建大模型问答应用、搭建微信公众号连接流、引入AI智能客服以及增加私有知识库,确保客服能精准回答复杂咨询,助力业务竞争力提升。整个过程简单快捷,在免费试用额度内费用为零。
747 7
轻松改造公众号:10分钟实现智能客服自动化!
|
11月前
|
并行计算 前端开发 异构计算
告别服务器繁忙,云上部署DeepSeek
本文以 DeepSeek-R1-Distill-Qwen-32B-FP8 为例,向您介绍如何在GPU实例上使用容器来部署量化的 DeepSeek-R1 蒸馏模型。
|
canal 存储 SQL
Mysql与Redis缓存同步方案详解
Mysql与Redis缓存同步方案详解
1926 1
Mysql与Redis缓存同步方案详解
|
开发工具 git
LLM-03 大模型 15分钟 FineTuning 微调 GPT2 模型 finetuning GPT微调实战 仅需6GB显存 单卡微调 数据 10MB数据集微调
LLM-03 大模型 15分钟 FineTuning 微调 GPT2 模型 finetuning GPT微调实战 仅需6GB显存 单卡微调 数据 10MB数据集微调
368 0
|
人工智能 测试技术
真相了!大模型解数学题和人类真不一样:死记硬背、知识欠缺明显,GPT-4o表现最佳
【8月更文挑战第15天】WE-MATH基准测试揭示大型多模态模型在解决视觉数学问题上的局限与潜力。研究涵盖6500题,分67概念5层次,评估指标包括知识与泛化不足等。GPT-4o表现最优,但仍存多步推理难题。研究提出知识概念增强策略以改善,为未来AI数学推理指明方向。论文见: https://arxiv.org/pdf/2407.01284
282 1
|
机器学习/深度学习 弹性计算 人工智能
大模型进阶微调篇(三):微调GPT2大模型实战
本文详细介绍了如何在普通个人电脑上微调GPT2大模型,包括环境配置、代码实现和技术要点。通过合理设置训练参数和优化代码,即使在无独显的设备上也能完成微调,耗时约14小时。文章还涵盖了GPT-2的简介、数据集处理、自定义进度条回调等内容,适合初学者参考。
2677 6