当前位置: 首页 > news >正文

java使用Redison自旋锁和mysql生成唯一编号

1. 数据库表设计(存储递增基准值)

CREATE TABLE `t_sequence` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `type_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '关键字段', `rule_date` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '规则日期', `max_number` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '自增序列号', `version` bigint(20) DEFAULT NULL COMMENT '版本号', `deleted` varchar(1) COLLATE utf8mb4_unicode_ci DEFAULT '0' COMMENT '删除状态:0-正常 1-删除', `create_user_id` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `create_time` datetime DEFAULT CURRENT_TIMESTAMP, `update_user_id` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `update_time` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='序列表';

2、生成实体

@Getter @Setter @Accessors(chain = true) @TableName("t_sequence") @ApiModel(value = "TSequence对象", description = "序列表") public class TSequence implements Serializable { @ApiModelProperty("主键ID") @TableId(value = "id", type = IdType.AUTO) private Long id; @ApiModelProperty("关键字段") private String typeKey; @ApiModelProperty("规则日期") private String ruleDate; @ApiModelProperty("对应某年的自增序列号") private String maxNumber; @ApiModelProperty("版本号") private Long version; @ApiModelProperty("创建人ID") private Long createUserId; @ApiModelProperty("创建时间") private LocalDateTime createTime; @ApiModelProperty("更新人ID") private Long updateUserId; @ApiModelProperty("更新时间") private LocalDateTime updateTime; @ApiModelProperty("是否删除:0-未删除 1-已删除") private String deleted; }

3、实现业务

@Component @AllArgsConstructor @Log4j2 public class TSequenceService { private final RedissonClient redissonClient; private final TSequenceMapper sequenceMapper; private static final String ORDER_SEQ_LOCK_KEY = "seq:lock:";//锁前缀 private static final int MAX_RETRY_TIMES = 10;//自旋次数 private static final long RETRY_INTERVAL = 200;//自旋一次等待时间 private static final long LOCK_EXPIRE_SECONDS = 30;//锁等待时间 /** * 生成唯一编码 * @param prefix :前缀 * @param typeKey :业务类型 * @param currentValue :自增数据 * @return */ @Transactional(rollbackFor = Exception.class) public String publicCreateNumber(String prefix,String typeKey,String currentValue) { String lockKey = ORDER_SEQ_LOCK_KEY + typeKey; int retryCount = 0; // 锁对象移到循环外,避免重复创建 RLock lock = redissonClient.getLock(lockKey); // 自旋获取锁 while (retryCount < MAX_RETRY_TIMES) { try { // 尝试获取锁(非阻塞) boolean locked = lock.tryLock(0, LOCK_EXPIRE_SECONDS, TimeUnit.SECONDS); if (locked) { try { // 加锁成功后执行核心逻辑 return doCreateNumber(prefix,typeKey,currentValue); } finally { // 释放锁(确保当前线程持有锁) if (lock.isHeldByCurrentThread()) { lock.unlock(); log.info("释放分布式锁成功,lockKey:{}", lockKey); } } } else { // 自旋等待 retryCount++; log.warn("第{}次获取锁失败,等待{}ms后重试,lockKey:{}", retryCount, RETRY_INTERVAL, lockKey); TimeUnit.MILLISECONDS.sleep(RETRY_INTERVAL); } } catch (InterruptedException e) { log.error("自旋等待时被中断", e); Thread.currentThread().interrupt(); throw new RuntimeException("生成编号失败:线程被中断"); } catch (Exception e) { log.error("获取/释放锁异常", e); throw new RuntimeException("生成编号失败:锁操作异常", e); } } log.error("生成编号失败:超出最大自旋重试次数({}次),lockKey:{}", MAX_RETRY_TIMES, lockKey); throw new RuntimeException("业务繁忙,请稍后尝试"); } /** * 加锁后的核心业务逻辑 * @param prefix :前缀 * @param typeKey :业务类型 * @param currentValue :自增数据 * @return */ public String doCreateNumber(String prefix,String typeKey,String currentValue) { String yearMonth = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMM"));//获取日期 long newVersion; int nextNumber; String number; //查询数据库序列数据,悲观锁查询,锁定行 TSequence tSequence = sequenceMapper.selectOne(new LambdaQueryWrapper<TSequence>() .eq(TSequence::getDeleted, DeletedEnum.DEFAULT.getValue()) .eq(TSequence::getTypeKey, typeKey) .eq(TSequence::getRuleDate, yearMonth) .last("FOR UPDATE")); if (ObjectUtil.isNull(tSequence)) { // 初始化编号 nextNumber = Integer.parseInt(currentValue)+ 1; // 使用 DecimalFormat 保持前导零 DecimalFormat df = new DecimalFormat(currentValue); String index = df.format(nextNumber); number = prefix+yearMonth+index;//拼接新编号 // 插入新记录 TSequence newSequence = new TSequence(); newSequence.setTypeKey(typeKey); newSequence.setMaxNumber(nextNumber); newSequence.setVersion(1L); newSequence.setRuleDate(yearMonth); newSequence.setCreateTime(LocalDateTime.now()); int insert = sequenceMapper.insert(newSequence); if (insert != 1) { throw new RuntimeException("初始化序列失败,enumValue:" + typeKey); } log.info("初始化序列成功,编号:{}", number); } else { // 已有序列,递增编号 String maxNumber = tSequence.getMaxNumber(); nextNumber = Integer.parseInt(tSequence.getMaxNumber())+ 1; // 使用 DecimalFormat 保持前导零 DecimalFormat df = new DecimalFormat(currentValue); String index = df.format(nextNumber); number = prefix+yearMonth+index;//拼接新编号 if (ObjectUtil.isEmpty(number)) { throw new RuntimeException("递增编号失败,当前最大编号:" + maxNumber); } // 乐观锁更新:校验旧版本号,设置新版本号 Long oldVersion = tSequence.getVersion(); newVersion = oldVersion + 1; LambdaUpdateWrapper<TSequence> updateWrapper = new LambdaUpdateWrapper<TSequence>() .eq(TSequence::getTypeKey, typeKey) .eq(TSequence::getRuleDate, yearMonth) .eq(TSequence::getMaxNumber, maxNumber) // 校验旧编号 .eq(TSequence::getVersion, oldVersion) // 校验旧版本号(核心修复) .set(TSequence::getVersion, newVersion) // 设置新版本号 .set(TSequence::getMaxNumber, nextNumber) .set(TSequence::getUpdateTime, LocalDateTime.now()); // 执行更新并校验结果 int update = sequenceMapper.update(null, updateWrapper); if (update != 1) { throw new RuntimeException("更新序列失败,并发冲突!enumValue:" + typeKey + ", 当前编号:" + maxNumber); } log.info("更新序列成功,旧编号:{},新编号:{}", maxNumber, number); } return number; } }
http://www.zskr.cn/news/111264.html

相关文章:

  • Dify AI 结合ZeroNews 实现公网快速访问
  • 10 个专科生论文写作工具,AI降重查重率推荐
  • Trae 和 cursor使用
  • 详细介绍:基于YOLOv5-AUX的棕熊目标检测与识别系统实现
  • 【金融风险分析必备技能】:掌握R语言相关性矩阵的5大核心应用
  • 生物信息分析高手私藏代码(R语言代谢组完整流程大公开)
  • IDEA/pycharm快捷键
  • 在算家云搭建Linly-Talker数字人语音模型
  • MySQL Shell 使用方法
  • Windows下TensorFlow-GPU 2.5.0环境搭建指南
  • 为什么你的量子模拟无法扩展?R语言多qubit架构陷阱全揭示
  • Cursor Agent 模式提示词
  • 嵌入式和软件系统中常见通信协议
  • Dify中Tesseract识别延迟高?工程师绝不外传的4种提速技巧
  • 如何将边缘Agent镜像缩小95%?,资深架构师亲授瘦身技巧
  • 【赵渝强老师】使用NetManager创建Oracle数据库的监听器
  • 错过再等一年!Dify私有化模型加载调优的7个核心参数配置
  • 【金融分析师必看】R语言实战:4类典型流动性指标建模精讲
  • LobeChat如何处理长上下文会话?上下文管理机制剖析
  • 大数据ETL中的数据质量提升工具与方法
  • Dify与Tesseract字体训练实战(从零搭建高精度OCR系统)
  • 适用于新手的软文营销“三步法”,精准锁定目标客户
  • Sprint Blog 2 (Dec 14-Dec 15) from“Pulse news stream”
  • 2025年广东保安公司服务能力深度评测报告 - 优质品牌商家
  • Dify依赖检查没人讲清楚?这篇万字长文彻底说透了
  • 2025全国优质保安公司推荐榜从需求场景看服务价值 - 优质品牌商家
  • 服务总在凌晨崩溃?,一文掌握Docker Compose健康检查精准配置
  • 【提升AI系统协同效率】:Docker-LangGraph多Agent通信优化的7大策略
  • 从零构建智能Agent文档系统:Dify配置与最佳实践全揭秘
  • 为什么你的Agent服务状态异常频发?根源竟在Docker数据卷挂载策略上