SpringBoot之使用Redis和注解实现接口幂等性

news2024/9/23 3:30:12

文章目录

  • 1 接口幂等性
    • 1.1 概念
    • 1.2 实现思路
    • 1.3 代码实现
      • 1.3.1 pom
      • 1.3.2 JedisUtil
      • 1.3.3 自定义注解@ApiIdempotent
      • 1.3.4 ApiIdempotentInterceptor拦截器
      • 1.3.5 TokenServiceImpl
      • 1.3.6 TestApplication
    • 1.4 测试验证
      • 1.4.1 获取token的控制器TokenController
      • 1.4.2 TestController
    • 1.5 注意点(非常重要)

1 接口幂等性

1.1 概念

幂等性 通俗的说就是一个接口, 多次发起同一个请求, 必须保证操作只能执行一次
比如:

  • 订单接口, 不能多次创建订单
  • 支付接口, 重复支付同一笔订单只能扣一次钱
  • 支付宝回调接口, 可能会多次回调, 必须处理重复回调
  • 普通表单提交接口, 因为网络超时等原因多次点击提交, 只能成功一次
    等等

常见解决方案:

  • 唯一索引 – 防止新增脏数据
  • token机制 – 防止页面重复提交
  • 悲观锁 – 获取数据的时候加锁(锁表或锁行)
  • 乐观锁 – 基于版本号version实现, 在更新数据那一刻校验数据
  • 分布式锁 – redis(jedis、redisson)或zookeeper实现
  • 状态机 – 状态变更, 更新数据时判断状态

1.2 实现思路

为需要保证幂等性的每一次请求创建一个唯一标识token, 先获取token, 并将此token存入redis, 请求接口时, 将此token放到header或者作为请求参数请求接口, 后端接口判断redis中是否存在此token

  • 如果存在, 正常处理业务逻辑, 并从redis中删除此token, 那么, 如果是重复请求, 由于token已被删除, 则不能通过校验, 返回请勿重复操作提示
  • 如果不存在, 说明参数不合法或者是重复请求, 返回提示即可

1.3 代码实现

1.3.1 pom

<!-- Redis-Jedis -->
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>2.9.0</version>
</dependency>
<!--lombok 本文用到@Slf4j注解, 也可不引用, 自定义log即可-->
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.16.10</version>
</dependency>

1.3.2 JedisUtil

package com.test.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Component
@Slf4j
public class JedisUtil {
    @Autowired
    private JedisPool jedisPool;
    private Jedis getJedis() {
        return jedisPool.getResource();
    }
    /**
     * 设值
     *
     * @param key
     * @param value
     * @return
     */
    public String set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.set(key, value);
        } catch (Exception e) {
            log.error("set key:{} value:{} error", key, value, e);
            return null;
        } finally {
            close(jedis);
        }
    }
    /**
     * 设值
     *
     * @param key
     * @param value
     * @param expireTime 过期时间, 单位: s
     * @return
     */
    public String set(String key, String value, int expireTime) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.setex(key, expireTime, value);
        } catch (Exception e) {
            log.error("set key:{} value:{} expireTime:{} error", key, value, expireTime, e);
            return null;
        } finally {
            close(jedis);
        }
    }
    /**
     * 取值
     *
     * @param key
     * @return
     */
    public String get(String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.get(key);
        } catch (Exception e) {
            log.error("get key:{} error", key, e);
            return null;
        } finally {
            close(jedis);
        }
    }
    /**
     * 删除key
     *
     * @param key
     * @return
     */
    public Long del(String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.del(key.getBytes());
        } catch (Exception e) {
            log.error("del key:{} error", key, e);
            return null;
        } finally {
            close(jedis);
        }
    }
    /**
     * 判断key是否存在
     *
     * @param key
     * @return
     */
    public Boolean exists(String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.exists(key.getBytes());
        } catch (Exception e) {
            log.error("exists key:{} error", key, e);
            return null;
        } finally {
            close(jedis);
        }
    }
    /**
     * 设值key过期时间
     *
     * @param key
     * @param expireTime 过期时间, 单位: s
     * @return
     */
    public Long expire(String key, int expireTime) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.expire(key.getBytes(), expireTime);
        } catch (Exception e) {
            log.error("expire key:{} error", key, e);
            return null;
        } finally {
            close(jedis);
        }
    }
    /**
     * 获取剩余时间
     *
     * @param key
     * @return
     */
    public Long ttl(String key) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            return jedis.ttl(key);
        } catch (Exception e) {
            log.error("ttl key:{} error", key, e);
            return null;
        } finally {
            close(jedis);
        }
    }
    private void close(Jedis jedis) {
        if (null != jedis) {
            jedis.close();
        }
    }
}

1.3.3 自定义注解@ApiIdempotent

package com.test.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 在需要保证 接口幂等性 的Controller的方法上使用此注解
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiIdempotent {
}

1.3.4 ApiIdempotentInterceptor拦截器

package com.test.interceptor;

import com.test.annotation.ApiIdempotent;
import com.test.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * 接口幂等性拦截器
 */
public class ApiIdempotentInterceptor implements HandlerInterceptor {

    @Autowired
    private TokenService tokenService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();

        ApiIdempotent methodAnnotation = method.getAnnotation(ApiIdempotent.class);
        if (methodAnnotation != null) {
            check(request);// 幂等性校验, 校验通过则放行, 校验失败则抛出异常, 并通过统一异常处理返回友好提示
        }

        return true;
    }

    private void check(HttpServletRequest request) {
        tokenService.checkToken(request);
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}

1.3.5 TokenServiceImpl

package com.test.service.impl;

import com.test.common.Constant;
import com.test.common.ResponseCode;
import com.test.common.ServerResponse;
import com.test.exception.ServiceException;
import com.test.service.TokenService;
import com.test.util.JedisUtil;
import com.test.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.StrBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class TokenServiceImpl implements TokenService {

    private static final String TOKEN_NAME = "token";

    @Autowired
    private JedisUtil jedisUtil;

    @Override
    public ServerResponse createToken() {
        String str = RandomUtil.UUID32();
        StrBuilder token = new StrBuilder();
        token.append(Constant.Redis.TOKEN_PREFIX).append(str);

        jedisUtil.set(token.toString(), token.toString(), Constant.Redis.EXPIRE_TIME_MINUTE);

        return ServerResponse.success(token.toString());
    }

    @Override
    public void checkToken(HttpServletRequest request) {
        String token = request.getHeader(TOKEN_NAME);
        if (StringUtils.isBlank(token)) {// header中不存在token
            token = request.getParameter(TOKEN_NAME);
            if (StringUtils.isBlank(token)) {// parameter中也不存在token
                throw new ServiceException(ResponseCode.ILLEGAL_ARGUMENT.getMsg());
            }
        }

        if (!jedisUtil.exists(token)) {
            throw new ServiceException(ResponseCode.REPETITIVE_OPERATION.getMsg());
        }

        Long del = jedisUtil.del(token);
        if (del <= 0) {
            throw new ServiceException(ResponseCode.REPETITIVE_OPERATION.getMsg());
        }
    }

}

1.3.6 TestApplication

package com.test;

import com.test.interceptor.ApiIdempotentInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@MapperScan("com.test.mapper")
public class TestApplication  extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    /**
     * 跨域
     * @return
     */
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 接口幂等性拦截器
        registry.addInterceptor(apiIdempotentInterceptor());
        super.addInterceptors(registry);
    }

    @Bean
    public ApiIdempotentInterceptor apiIdempotentInterceptor() {
        return new ApiIdempotentInterceptor();
    }

}

OK, 目前为止, 校验代码准备就绪, 接下来测试验证。

1.4 测试验证

1.4.1 获取token的控制器TokenController

package com.test.controller;

import com.test.common.ServerResponse;
import com.test.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/token")
public class TokenController {

    @Autowired
    private TokenService tokenService;

    @GetMapping
    public ServerResponse token() {
        return tokenService.createToken();
    }

}

1.4.2 TestController

TestController, 注意@ApiIdempotent注解, 在需要幂等性校验的方法上声明此注解即可, 不需要校验的无影响

package com.test.controller;

import com.test.annotation.ApiIdempotent;
import com.test.common.ServerResponse;
import com.test.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {

    @Autowired
    private TestService testService;

    @ApiIdempotent
    @PostMapping("testIdempotence")
    public ServerResponse testIdempotence() {
        return testService.testIdempotence();
    }
}

1.5 注意点(非常重要)

在这里插入图片描述
上图中, 不能单纯的直接删除token而不校验是否删除成功, 会出现并发安全性问题, 因为, 有可能多个线程同时走到第46行, 此时token还未被删除, 所以继续往下执行, 如果不校验jedisUtil.del(token)的删除结果而直接放行, 那么还是会出现重复提交问题, 即使实际上只有一次真正的删除操作,但由于没有对删除结果进行校验, 所以还是有并发问题, 因此, 必须校验

稍微修改一下代码:
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1075464.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

[ICCV-23] DeformToon3D: Deformable Neural Radiance Fields for 3D Toonification

pdf | code 将3D人脸风格化问题拆分为几何风格化与纹理风格化。提出StyleField&#xff0c;学习以风格/ID为控制信号的几何形变残差&#xff0c;实现几何风格化。通过对超分网络引入AdaIN&#xff0c;实现纹理风格化。由于没有修改3D GAN空间&#xff0c;因此可以便捷实现Edit…

Matlab导入log(或txt)文件,提取数据或其他信息

导入log&#xff08;或txt)文件 先上代码&#xff1a; clc; clear; %数据导入 file1fullfile(文件路径, test1.log); % 导入文件test1.log f1fopen(file1); dt1textscan(f1,%s); %采用textscan 读取数据 %得到的dt1是元胞数组格式比如&#xff1a;有如下内容文件 导入…

java案例20:库存管理系统

思路&#xff1a; 像商场、超市这样的地方&#xff0c;都需要有自己的库房&#xff0c;并且库房商品的库存变化有专人记录&#xff0c;这样才能保证商城和超市正常运转编写程序&#xff0c;模拟库存管理系统 该系统主要包括系统首页、商品入库、商品显示和删除商品功能 每个功…

spark-03

RDD是抽象概念&#xff0c;分区是物理概念

区分 关系代数中的笛卡尔积、等值连接、自然连接(图文界面)

目录 前言1. 笛卡尔积2. 等值连接3. 自然连接 前言 深度卷积的笛卡尔积、软考高级等常出现类似这种知识点 归根到底是关系代数的数学知识 今天就专门区分一下 概念定义笛卡尔积两个关系R和S进行操作&#xff0c;产生的关系中元组个数为两个关系中元组个数之积等值联接&#…

C++QT-day3

#include <iostream> /*设计一个Per类&#xff0c;类中包含私有成员:姓名、年龄、* 指针成员身高、体重&#xff0c;再设计一个Stu类&#xff0c;* 类中包含私有成员:成绩、Per类对象p1&#xff0c;* 设计这两个类的构造函数、析构函数和拷贝构造函数。*/ using namespac…

机器学习1:k 近邻算法

k近邻算法&#xff08;k-Nearest Neighbors, k-NN&#xff09;是一种常用的分类和回归算法。它基于一个简单的假设&#xff1a;如果一个样本的k个最近邻居中大多数属于某一类别&#xff0c;那么该样本也很可能属于这个类别。 k近邻算法的步骤如下&#xff1a; 输入&#xff1a…

设计模式 - 行为型模式考点篇:模板方法模式(概念 | 案例实现 | 优缺点 | 使用场景)

目录 一、行为型模式 1.1、模板方法模式 1.1.1、概念 1.1.2、案例实现 1.1.3、优缺点 1.1.4、使用场景 一、行为型模式 一句话概括行为型模式 行为型模式&#xff1a;类或对象间如何交互、如何划分职责&#xff0c;从而更好的完成任务. 1.1、模板方法模式 1.1.1、概念 …

COCI 2021-2022 #1 - Volontiranje 题解

题目大意 让你求最多有多少个不相交的最长上升子序列。 思路 我们可以将数 a i a_i ai​ 看成一个点 ( i , a [ i ] ) (i,a[i]) (i,a[i])&#xff0c;于是我们可以按照 x x x 递增 y y y 递减考虑分层。 分层的方法就是从 1 1 1 到当前点的 LIS 即为其所在层数。 此时…

Spring源码解析——IOC之bean 的初始化

正文 一个 bean 经历了 createBeanInstance() 被创建出来&#xff0c;然后又经过一番属性注入&#xff0c;依赖处理&#xff0c;历经千辛万苦&#xff0c;千锤百炼&#xff0c;终于有点儿 bean 实例的样子&#xff0c;能堪大任了&#xff0c;只需要经历最后一步就破茧成蝶了。…

Git 学习笔记 | Git 分支

Git 学习笔记 | Git 分支 Git 学习笔记 | Git 分支分支的概念为什么要使用分支&#xff1f;常用指令 Git 学习笔记 | Git 分支 分支的概念 几乎所有的版本控制系统都以某种形式支持分支。 使用分支意味着你可以把你的工作从开发主线上分离开来&#xff0c;避免影响开发主线。…

小白网络安全学习手册(黑客技术)

一、网络安全应该怎么学&#xff1f; 1.计算机基础需要过关 这一步跟网安关系暂时不大&#xff0c;是进入it行业每个人都必须掌握的基础能力。 计算机网络计算机操作系统算法与数据架构数据库 Tips:不用非要钻研至非常精通&#xff0c;可以与学习其他课程同步进行。 2.渗透技…

[idekCTF 2022]Paywall - LFI+伪协议+filter_chain

[idekCTF 2022]Paywall 一、解题流程&#xff08;一&#xff09;、分析&#xff08;二&#xff09;、解题 二、思考总结 一、解题流程 &#xff08;一&#xff09;、分析 点击source可以看到源码&#xff0c;其中关键部分&#xff1a;if (isset($_GET[p])) {$article_content…

【C语言】进阶——动态内存管理

一、为什么存在动态内存管理 我们已经掌握的内存开辟方式有&#xff1a; int val 20; //在栈空间开辟四个字节char arr[10] {0}; //在栈空间开辟10个字节的连续空间 但上述开辟空间的方式有两个特点&#xff1a; 空间开辟大小是固定的数组在声明的时候&#xf…

Maven最新版本安装及配置

Maven是一个Java项目管理和构建工具&#xff0c;它可以定义项目结构、项目依赖&#xff0c;并使用统一的方式进行自动化构建&#xff0c;是Java项目不可缺少的工具。 本章我们详细介绍如何使用Maven。 一、Maven是什么&#xff1f; 如果每一个项目都自己搞一套配置&#xf…

【java学习】一维数组(9)

文章目录 1. 一维数组声明2. 一维数组初始化3. 数组元素的引用4. 数组元素的默认初始化 1. 一维数组声明 声明方式&#xff1a; type var[] 或 type[] var 例如&#xff1a; int a[]; int[] a1; double b[]; Mydate[] c; //对象数组2. 一维数组初始化 动态初始化&#xf…

python练习4

前言&#xff1a;相信看到这篇文章的小伙伴都或多或少有一些编程基础&#xff0c;懂得一些linux的基本命令了吧&#xff0c;本篇文章将带领大家服务器如何部署一个使用django框架开发的一个网站进行云服务器端的部署。 文章使用到的的工具 Python&#xff1a;一种编程语言&…

2023年十大顶尖远程控制软件

随着科技的不断进步和时代的发展&#xff0c;远程控制软件已经成为现代工作、教育等领域中必不可少的工具。这些软件可以实现远程桌面连接、远程支持和远程维护等功能&#xff0c;大大提高了工作效率&#xff0c;同时也让用户拥有更加灵活的操作方式。当然&#xff0c;不同的远…

C#:出题并判断

C#:出题并判断 //出题并判断 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace Test_…

【高阶数据结构】图详解第二篇:图的遍历(广度优先+深度优先)

文章目录 图的遍历1. 图的广度优先遍历&#xff08;一石激起千层浪&#xff09;思路分析代码实现测试美团2020校招笔试题&#xff1a;六度人脉 2. 图的深度优先遍历&#xff08;一条道走到黑&#xff09;思路分析代码实现测试 3. 对于非连通图情况的处理4.源码BFSDFS 图的遍历 …