mybatis-plus 雪花算法id冲突问题解决、雪花算法id冲突、雪花算法、id冲突解决、id、id冲突、主键冲突

2024-09-05 10:36

本文主要是介绍mybatis-plus 雪花算法id冲突问题解决、雪花算法id冲突、雪花算法、id冲突解决、id、id冲突、主键冲突,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

mybatis-plus 雪花算法id冲突问题解决、雪花算法id冲突、雪花算法、id冲突解决、id、id冲突、主键冲突

问题:
k8s里起了多个pod,发现mybatis-plus的雪花算法不同pod之前生成了相同的id

问题原因:
mybatis-plus默认id生成器生成datacenterId时是读取的机器网卡mac地址后两个字节,生成一个0~31的数字,这里有很大机率生成相同的datacenterId值
mybatis-plus默认id生成器生成workerId时是读取的jvm pid,但是k8s里pod的jvm pid不作处理时默认都是1,导致datacenterId + workerId有很大机率不同的pod拥有相同的值

分析日志:
ee:dc:39:3f:1a:53
531a >> 6 = 0101001100
0101001100 % 32 = 01100

86:65:49:0a:d1:f4
f4d1 >> 6 = 1111010011
1111010011 % 32 = 10011

e2:71:3a:cc:31:c3
c331 >> 6 = 1100001100
1100001100 % 32 = 01100

解决思路:
不同的pod之间分配唯一的datacenterId、workerId,使生成的雪花算法id不会产生冲突

解决方案(已实际使用并已通过测试):

//程序配置
application.yml:mybatis-plus-ext:snowflake:redis-key: smarthome.mybatis-plus-ext.snowflake.redis-key #mybatis-plus 雪花算法生成器配置。解决多个pod情况下生成的 数据中心id+workerId 重复的问题的redis键值


 

//配置类
MybatisPlusSnowflakeConfig.java:package com.onbright.smarthome.config;import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.MutablePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;/*** <p>* mybatis-plus 雪花算法生成器配置。解决多个pod情况下生成的 数据中心id+workerId 重复的问题* </p>*/
@Slf4j
@Configuration
public class MybatisPlusSnowflakeConfig {@Autowiredprivate RedisTemplate redisTemplate;@Value("${mybatis-plus-ext.snowflake.redis-key}")private String redisKey;@Beanpublic IdentifierGenerator identifierGenerator(){//testedLong redisIncrementValue = redisTemplate.opsForValue().increment(redisKey, 1L);if (redisIncrementValue == null || redisIncrementValue < 0L){throw new RuntimeException(String.format("程序启动失败, 使用redis获取到的雪花算法索引值异常, %s", redisIncrementValue));}//tested//从redis索引值中取出dataCenterId、workerIdMutablePair<Long, Long> longLongMutablePair = generateDatacenterIdAndWorkerIdFromIndex(redisIncrementValue);Long dataCenterId = longLongMutablePair.getLeft();Long workerId = longLongMutablePair.getRight();//tested//再次检查dataCenterId、workerId值if (dataCenterId == null || dataCenterId < 0L || dataCenterId > 31L|| workerId == null || workerId < 0L || workerId > 31L){throw new RuntimeException(String.format("程序启动失败, 使用雪花算法索引值算出的dataCenterId,workerId异常, %s, %s, %s", redisIncrementValue, dataCenterId, workerId));}log.info("mybatis-plus雪花算法datacenterId,workerId生成, redisIncrementValue: {}, dataCenterId: {}, workerId: {}", redisIncrementValue, dataCenterId, workerId);return new DefaultIdentifierGenerator(workerId, dataCenterId);}/*** 从index值中生成雪花算法的datacenterId(5bit)、workerId(5bit)** @param index 索引值,如:1。必须为大于等于0的值* @return 返回生成好的用MutablePair包装的datacenterId(left)、workerId(right),其中datacenterId值范围为0~31,workerId值范围也为0~31,如:{0,0}、{1、1}、{0,1}、{0,31}、{31,31}* @throws IllegalArgumentException 当参数索引值小于0时会抛出异常*/public static MutablePair<Long, Long> generateDatacenterIdAndWorkerIdFromIndex(long index) {if (index < 0) {throw new IllegalArgumentException("索引值不能为负数");}long indexBucket = index % 1024L;long dataCenterId = indexBucket / 32L;long workerId = indexBucket % 32L;return MutablePair.of(dataCenterId, workerId);}}//测试核心函数
MybatisPlusSnowflakeConfigTest.java:package com.onbright.smarthome.config;import org.apache.commons.lang3.tuple.MutablePair;
import org.junit.Assert;
import org.junit.Test;public class MybatisPlusSnowflakeConfigTest {@Testpublic void generateDatacenterIdAndWorkerIdFromIndex() {//legal{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(0L);Assert.assertEquals(0L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(0L, longLongMutablePair.getRight().longValue());}{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(1L);Assert.assertEquals(0L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(1L, longLongMutablePair.getRight().longValue());}{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(2L);Assert.assertEquals(0L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(2L, longLongMutablePair.getRight().longValue());}{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(31L);Assert.assertEquals(0L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(31L, longLongMutablePair.getRight().longValue());}{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(32L);Assert.assertEquals(1L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(0L, longLongMutablePair.getRight().longValue());}{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(33L);Assert.assertEquals(1L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(1L, longLongMutablePair.getRight().longValue());}{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(63L);Assert.assertEquals(1L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(31L, longLongMutablePair.getRight().longValue());}{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(64L);Assert.assertEquals(2L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(0L, longLongMutablePair.getRight().longValue());}{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(991L);Assert.assertEquals(30L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(31L, longLongMutablePair.getRight().longValue());}{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(992L);Assert.assertEquals(31L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(0L, longLongMutablePair.getRight().longValue());}{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(993L);Assert.assertEquals(31L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(1L, longLongMutablePair.getRight().longValue());}{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(1023L);Assert.assertEquals(31L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(31L, longLongMutablePair.getRight().longValue());}{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(1024L);Assert.assertEquals(0L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(0L, longLongMutablePair.getRight().longValue());}//integer.max{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(2147483647L);Assert.assertEquals(31L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(31L, longLongMutablePair.getRight().longValue());}//long.max{MutablePair<Long, Long> longLongMutablePair = MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(9223372036854775807L);Assert.assertEquals(31L, longLongMutablePair.getLeft().longValue());Assert.assertEquals(31L, longLongMutablePair.getRight().longValue());}//illegal{Assert.assertEquals("索引值不能为负数", Assert.assertThrows(IllegalArgumentException.class, () -> MybatisPlusSnowflakeConfig.generateDatacenterIdAndWorkerIdFromIndex(-1)).getMessage());}}
}

这篇关于mybatis-plus 雪花算法id冲突问题解决、雪花算法id冲突、雪花算法、id冲突解决、id、id冲突、主键冲突的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1138715

相关文章

解决IDEA报错:编码GBK的不可映射字符问题

《解决IDEA报错:编码GBK的不可映射字符问题》:本文主要介绍解决IDEA报错:编码GBK的不可映射字符问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录IDEA报错:编码GBK的不可映射字符终端软件问题描述原因分析解决方案方法1:将命令改为方法2:右下jav

MyBatis模糊查询报错:ParserException: not supported.pos 问题解决

《MyBatis模糊查询报错:ParserException:notsupported.pos问题解决》本文主要介绍了MyBatis模糊查询报错:ParserException:notsuppo... 目录问题描述问题根源错误SQL解析逻辑深层原因分析三种解决方案方案一:使用CONCAT函数(推荐)方案二:

Redis 热 key 和大 key 问题小结

《Redis热key和大key问题小结》:本文主要介绍Redis热key和大key问题小结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、什么是 Redis 热 key?热 key(Hot Key)定义: 热 key 常见表现:热 key 的风险:二、

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Spring Boot中JSON数值溢出问题从报错到优雅解决办法

《SpringBoot中JSON数值溢出问题从报错到优雅解决办法》:本文主要介绍SpringBoot中JSON数值溢出问题从报错到优雅的解决办法,通过修改字段类型为Long、添加全局异常处理和... 目录一、问题背景:为什么我的接口突然报错了?二、为什么会发生这个错误?1. Java 数据类型的“容量”限制

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基