主要提供接口幂等性校验功能,只需要通过一个注解 '@Idempotent' 即可完成校验。
-
git clone https://github.com/727474430/idempotent-spring-boot-starter.git
-
cd idempotent-spring-boot-starter
-
mvn install
-
在需要使用的 SpringBoot 项目中引入依赖关系
<dependency> <groupId>com.raindrop</groupId> <artifactId>idempotent-spring-boot-starter</artifactId> <version>1.0.RELEASE</version> </dependency>
-
在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