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

相关文章

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

mybatis映射器配置小结

《mybatis映射器配置小结》本文详解MyBatis映射器配置,重点讲解字段映射的三种解决方案(别名、自动驼峰映射、resultMap),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定... 目录select中字段的映射问题使用SQL语句中的别名功能使用mapUnderscoreToCame

mybatis-plus如何根据任意字段saveOrUpdateBatch

《mybatis-plus如何根据任意字段saveOrUpdateBatch》MyBatisPlussaveOrUpdateBatch默认按主键判断操作类型,若需按其他唯一字段(如agentId、pe... 目录使用场景方法源码方法改造首先在service层定义接口service层接口实现总结使用场景my

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

MyBatis ParameterHandler的具体使用

《MyBatisParameterHandler的具体使用》本文主要介绍了MyBatisParameterHandler的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一、概述二、源码1 关键属性2.setParameters3.TypeHandler1.TypeHa

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

504 Gateway Timeout网关超时的根源及完美解决方法

《504GatewayTimeout网关超时的根源及完美解决方法》在日常开发和运维过程中,504GatewayTimeout错误是常见的网络问题之一,尤其是在使用反向代理(如Nginx)或... 目录引言为什么会出现 504 错误?1. 探索 504 Gateway Timeout 错误的根源 1.1 后端