Skip to content

Latest commit

 

History

History
99 lines (72 loc) · 2.38 KB

README_CN.md

File metadata and controls

99 lines (72 loc) · 2.38 KB

一个自定义 spring boot starter 项目

主要提供接口幂等性校验功能,只需要通过一个注解 '@Idempotent' 即可完成校验。

如何使用.

  1. git clone https://github.com/727474430/idempotent-spring-boot-starter.git

  2. cd idempotent-spring-boot-starter

  3. mvn install

  4. 在需要使用的 SpringBoot 项目中引入依赖关系

    <dependency>
        <groupId>com.raindrop</groupId>
        <artifactId>idempotent-spring-boot-starter</artifactId>
        <version>1.0.RELEASE</version>
    </dependency>
  5. application.properties/application.yml文件中添加下列属性

    • idempotent.enable=true

      是否开启接口幂等性校验功能,默认为 "false" 不开启,选择 true 开启。

    • idempotent.tokenStorage=redis

      需要使用的 Token 存储类型,默认使用 "memory" 方式,支持 "redis""memory" 两种模式。如果使用 redis 模式, 你需要添加 redis 相关配置。

    • idempotent.tokenHeader=token

      通过请求头传递 token 的 key 名,默认 key 为 "token"

示例.

  • application.properties

    idempotent.enable=true
    idempotent.tokenStorage=redis
    idempotent.tokenHeader=token
    spring.redis.host=localhost
    spring.redis.port=6379
    spring.redis.password=
    spring.redis.database=0
  • application.yml

    idempotent:
      enable: true
      token-storage: redis
      token-header: token
    spring:
      redis:
        host: localhost
        port: 6379
        password:
        database: 0
  • @Idempotent

    • timeout: 幂等校验超时时间,默认 1 秒
    • timeUnit: 超时时间单位,默认秒
    • tips: 请求失败,重复请求接口,请稍后再试!
    • delKey: 执行完成,是否删除 Key,默认不删除
  • API 接口

    @RestController
    @RequestMapping("/order")
    public class OrderController {
    
      @Idempotent
      @GetMapping("/create")
      public String check() {
          return "ok";
      }
    
      @Idempotent(timeout = 3L, timeUnit = TimeUnit.SECONDS, delKey = false)
      @GetMapping("/update")
      public String update() {
          return "ok";
      }
    
    }
  • 请求接口

    curl http://ip:port/order/create