Code Monkey home page Code Monkey logo

heaofei / tomato Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lxchinesszz/tomato

0.0 0.0 0.0 49 KB

专门为SpringBoot项目设计的幂等组件,让天下没有难写的防重代码。 基于控制时间两种防重策略 策略一: 滑动窗口策略 每次请求设置当前请求的控制时间,控制时间内请求均会被拦截。 策略二: 固定窗口策略 仅仅为第一次请求生成一个控制时间,控制时间内相同的请求会被拦截,控制时间过期后,以此类推。

Home Page: https://blog.springlearn.cn

License: Apache License 2.0

Java 100.00%

tomato's Introduction


996.icuLICENSE

是一款专门为SpringBoot项目设计的幂等组件

本文以下的讨论,都是假设我们数据库没有做唯一约束和乐观锁的场景下的分析。关于防重和幂等判断的讨论,欢迎留言讨论


一、问题重现

下面这段逻辑,在正常情况下是没有问题的,①也算进行了幂等校验,先判断状态在进行处理。但是当用户重复提交导致并发问题,两次请求都执行到了④步骤,而因为④并没有用乐观锁处理,就会导致幂等性问题。两次提交都对数据状态进行的修改。 在这里插入图片描述

所以我们得出结论凡是通过根据查询状态来做防重或者幂等校验的,理论情况下都会因为并发问题而被击穿。 所以如果数据库表设计允许的情况下,建议设置唯一约束,用来做幂等或者防重。(sql尽量加上乐观锁操作)

二、如何防止重复提交?

方案 1: 幂等表

利用数据库唯一索引做防重处理,当第一次插入是没有问题的,第二次在进行插入会因为唯一索引报错。从而达到拦截的目的。

方案 2: token令牌

如何防止重复提交, 为每次请求生成请求唯一键,服务端对每个唯一键进行生命周期管控。规定时间内只允许一次请求,非第一次请求都属于重复提交。但是前后端改造大,后端要给出单独生成token令牌接口,前端要在每次调用时候先获取token令牌。

方案 3: 基于方法粒度指定唯一键【Tomato】

基于Web方法,从参数中寻找可以作为唯一键,进行控制。改造难度低,仅需要服务端改造,前端无感知。

本文是基于方案 3提供的解决方案,具体实现是利用 Redis进行实现。

三、具体实现原理

原理非常简单,我们为每次请求声明一个防重的时间范围,范围内的重复请求都会当做重复提交被拦截。核心原理就是这么简单。 在这里插入图片描述

基于控制时间两种防重策略

策略一: 滑动窗口策略

每次请求设置当前请求的控制时间,控制时间内请求均会被拦截。

在这里插入图片描述

策略二: 固定窗口策略

仅仅为第一次请求生成一个控制时间,控制时间内相同的请求会被拦截,控制时间过期后,以此类推。 在这里插入图片描述

四、代码演示

1.利用拦截器实现

class Example{

        @Repeat
        @GetMapping("/tt")
        public String getUser(@TomatoToken String name) {
            System.out.println(System.currentTimeMillis() + ":" + name);
            String s = System.currentTimeMillis() + ":" + name;
            return s;
        }


        @Repeat
        @PostMapping(value = "/post", consumes = MediaType.APPLICATION_JSON_VALUE)
        public String postUserName(@TomatoToken("userName") @RequestBody UserRequest userRequest) {
            System.out.println(System.currentTimeMillis() + ":" + userRequest.getUserName());
            String s = System.currentTimeMillis() + ":" + userRequest.getUserName();
            return s;
        }

        @Repeat(throwable = NullPointerException.class, message = "禁止重复提交")
        @PostMapping(value = "/form")
        public String postUserName(@TomatoToken("userName") HttpServletRequest userRequest) {
            System.out.println(System.currentTimeMillis() + ":" + userRequest.getParameter("userName"));
            String s = System.currentTimeMillis() + ":" + userRequest.getParameter("userName");
            return s;
        }
}

2.硬编码实现

class Example{
    public VoidResponse addWhite(String name) {
  			Idempotent.idempotent(name,1000,()->new RuntimeException("重复提交"))
        return VoidResponse.SUCCESS();
    }
}

五、快速使用

1. 加入依赖

Maven

<dependency>
  <groupId>com.github.lxchinesszz</groupId>
  <artifactId>tomato-spring-boot-starter</artifactId>
  <version>1.0.1.RELEASE</version>
</dependency>

Gradle

implementation 'com.github.lxchinesszz:tomato-spring-boot-starter:1.0.1.RELEASE'

2. 启用成功

当出现tomato Logo即说明启用成功

六、版本发布记录

1.0.1-RELEASE

  • 修复 bug

1.0.2-RELEASE

  • Repeat 新增拦截策略
  • 代码逻辑精简, 增强扩展性
  • 增强 StaticContext 上下文,支持用户获取令牌值及令牌初始化参数

tomato's People

Contributors

lxchinesszz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.