news 2026/7/1 20:36:51

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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; } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/28 21:17:41

LobeChat能否预订门票?智能服务再升级

LobeChat能否预订门票&#xff1f;智能服务再升级 在AI助手逐渐渗透日常生活的今天&#xff0c;我们已经不再满足于“问一句答一句”的简单交互。当用户对聊天机器人说“帮我订张周杰伦演唱会的票”&#xff0c;期望得到的不再是“我无法执行此操作”的冰冷回复&#xff0c;而是…

作者头像 李华
网站建设 2026/6/30 12:41:00

火山引擎AI大模型图像描述生成后交由Anything-LLM组织报告

火山引擎AI大模型图像描述生成后交由Anything-LLM组织报告 在企业智能化转型的浪潮中&#xff0c;一个日益突出的问题浮出水面&#xff1a;AI看得见世界&#xff0c;却记不住它说过的话。 当视觉模型从一张会议照片中精准识别出“两人正在审阅合同”&#xff0c;几天后用户再问…

作者头像 李华
网站建设 2026/6/30 9:34:44

权限设计陷进频发?,一文读懂 Dify 混合检索场景下的安全边界控制

第一章&#xff1a;权限设计陷进频发&#xff1f;一文读懂 Dify 混合检索场景下的安全边界控制在构建基于 Dify 的混合检索系统时&#xff0c;权限边界模糊常引发数据越权访问问题。尤其是在多租户、多角色协作的场景下&#xff0c;若未对检索请求链路实施细粒度控制&#xff0…

作者头像 李华
网站建设 2026/6/30 22:07:40

Dify工作流并行执行陷阱:90%开发者忽略的3个性能瓶颈

第一章&#xff1a;Dify工作流并行执行的核心机制Dify 工作流引擎通过任务图&#xff08;Task Graph&#xff09;与运行时调度器的协同&#xff0c;实现了高效的并行执行能力。其核心在于将工作流中的各个节点解析为可独立运行的任务单元&#xff0c;并依据依赖关系动态调度执行…

作者头像 李华
网站建设 2026/6/30 22:06:20

LobeChat能否支持虚拟试衣?服装搭配AI推荐引擎

LobeChat能否支持虚拟试衣&#xff1f;服装搭配AI推荐引擎 在电商直播和社交种草盛行的今天&#xff0c;用户已经不再满足于“看看图、点点购”的购物方式。他们更希望获得一种接近线下门店的沉浸式体验——比如上传一张自己的上衣照片&#xff0c;立刻得到&#xff1a;“这件…

作者头像 李华