{"id":20269161,"url":"https://github.com/pig-mesh/idempotent-spring-boot-starter","last_synced_at":"2025-04-13T07:51:27.251Z","repository":{"id":40354507,"uuid":"298538516","full_name":"pig-mesh/idempotent-spring-boot-starter","owner":"pig-mesh","description":"spring boot idempotent starter","archived":false,"fork":false,"pushed_at":"2024-12-21T01:36:32.000Z","size":583,"stargazers_count":151,"open_issues_count":0,"forks_count":72,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-04T05:44:52.073Z","etag":null,"topics":["idempotent","java","redis","redisson","spring-boot"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pig-mesh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-25T10:15:56.000Z","updated_at":"2025-02-17T06:11:18.000Z","dependencies_parsed_at":"2025-02-09T23:00:26.245Z","dependency_job_id":"af53a0bf-272b-46d4-a020-3091ce2cbffb","html_url":"https://github.com/pig-mesh/idempotent-spring-boot-starter","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pig-mesh%2Fidempotent-spring-boot-starter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pig-mesh%2Fidempotent-spring-boot-starter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pig-mesh%2Fidempotent-spring-boot-starter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pig-mesh%2Fidempotent-spring-boot-starter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pig-mesh","download_url":"https://codeload.github.com/pig-mesh/idempotent-spring-boot-starter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248681494,"owners_count":21144700,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["idempotent","java","redis","redisson","spring-boot"],"created_at":"2024-11-14T12:23:38.680Z","updated_at":"2025-04-13T07:51:27.206Z","avatar_url":"https://github.com/pig-mesh.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# idempotent 幂等处理方案\n\n\n是对原有 [idempotent](https://github.com/it4alla/idempotent) 代码重构和功能增强。\n\n非常感谢 idempotent 作者的分享。\n\n### 1.原理\n\n1.请求开始前，根据key查询\n查到结果：报错\n未查到结果：存入key-value-expireTime\nkey=ip+url+args\n\n2.请求结束后，直接删除key\n不管key是否存在，直接删除\n是否删除，可配置\n\n3.expireTime过期时间，防止一个请求卡死，会一直阻塞，超过过期时间，自动删除\n过期时间要大于业务执行时间，需要大概评估下;\n\n4.此方案直接切的是接口请求层面。\n\n5.过期时间需要大于业务执行时间，否则业务请求1进来还在执行中，前端未做遮罩，或者用户跳转页面后再回来做重复请求2，在业务层面上看，结果依旧是不符合预期的。\n\n6.建议delKey = false。即使业务执行完，也不删除key，强制锁expireTime的时间。预防5的情况发生。\n\n7.实现思路：同一个请求ip和接口，相同参数的请求，在expireTime内多次请求，只允许成功一次。\n\n8.页面做遮罩，数据库层面的唯一索引，先查询再添加，等处理方式应该都处理下。\n\n9.此注解只用于幂等，不用于锁，100个并发这种压测，会出现问题，在这种场景下也没有意义，实际中用户也不会出现1s或者3s内手动发送了50个或者100个重复请求,或者弱网下有100个重复请求；\n\n\n### 2.使用\n\n- 1. 引入依赖\n\n| 版本    | 支持 |\n|-------|--|\n| 0.5.0 | 适配 SpringBoot3.x |\n| 0.1.0 | 适配 SpringBoot2.x |\n\n```java\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.pig4cloud.plugin\u003c/groupId\u003e\n    \u003cartifactId\u003eidempotent-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e0.5.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n- 2. 配置 redis 链接相关信息\n\n```yaml\nspring:\n  data:\n    redis:\n      host: 127.0.0.1\n      port: 6379\n```\n\n理论是支持 [redisson-spring-boot-starter](https://github.com/redisson/redisson/tree/master/redisson-spring-boot-starter) 全部配置\n\n\n- 3. 接口设置注解\n\n```java\n@Idempotent(key = \"#demo.username\", expireTime = 3, info = \"请勿重复查询\")\n@GetMapping(\"/test\")\npublic String test(Demo demo) {\n    return \"success\";\n}\n```\n\n\n### idempotent 注解 配置详细说明\n\n\n- 1. 幂等操作的唯一标识，使用spring el表达式 用#来引用方法参数 。 可为空则取 当前 url + args 做表示\n    \n```java\nString key();\n```\n\n\n- 2. 有效期 默认：1 有效期要大于程序执行时间，否则请求还是可能会进来\n\n```java\n\tint expireTime() default 1;\n```\n\n- 3. 时间单位 默认：s （秒）\n\n```java\nTimeUnit timeUnit() default TimeUnit.SECONDS;\n```\n\n- 4. 幂等失败提示信息，可自定义\n\n```java\nString info() default \"重复请求，请稍后重试\";\n```\n\n- 5. 是否在业务完成后删除key true:删除 false:不删除\n\n```java\nboolean delKey() default false;\n```\n- 6. 全局异常处理：当幂等性触发后，切面会抛出 IdempotentException 异常。需要在全局异常处理器中捕获此异常并处理，返回合适的错误信息。\n\n```java\n@ControllerAdvice\npublic class GlobalExceptionHandler {\n\n    @ExceptionHandler(IdempotentException.class)\n    public ResponseEntity\u003cString\u003e handleIdempotentException(IdempotentException ex) {\n        // 处理幂等异常，返回自定义的错误信息或业务逻辑\n        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());\n    }\n}\n```\n\n#### 微信群\n\n![](https://i.loli.net/2020/09/25/SsWqJt2H157VbfI.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpig-mesh%2Fidempotent-spring-boot-starter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpig-mesh%2Fidempotent-spring-boot-starter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpig-mesh%2Fidempotent-spring-boot-starter/lists"}