首页 > 编程语言 >mybatis-plus雪花算法增强:idworker

mybatis-plus雪花算法增强:idworker

时间:2022-12-07 17:35:30浏览次数:66  
标签:workerId idworker datacenterId plus mybatis 节点


文章目录

  • ​​前言​​
  • ​​一、官网​​
  • ​​二、默认实现的弊端​​
  • ​​三、mybatis-plus中datacenterId和workerId的默认生成规则​​
  • ​​四、idworker介绍​​
  • ​​五、idworker实战​​
  • ​​1、引入maven依赖​​
  • ​​2、添加zookeeper配置​​
  • ​​3、指定mybatis-plus的id生成器​​
  • ​​4、测试​​
  • ​​总结​​

前言

前面已经介绍了利用mybatis-plus中默认的雪花算法生成分布式唯一id,但是还是有一些弊端存在,今天聊聊在mybatis-plus中引入分布式ID生成框架​​idworker​​,进一步增强实现生成分布式唯一ID。


一、官网

官方文档:​​https://baomidou.com/​

Git地址:​​https://github.com/baomidou/mybatis-plus​

idworker官网:​​https://github.com/imadcn/idworker​

TIP⚠️:
推荐学习框架的时候,多研究下官网,获取第一手资料。

二、默认实现的弊端

在雪花算法的实现中,需要用户指定​​datacenterId​​​和​​workerId​​的值。

mybatis-plus雪花算法增强:idworker_mybatis


在分布式场景下,如果多台机器上的服务都指定相同的datacenterId和workerId,在高并发请求下,会出现Id重复的风险。

如下是一个雪花算法ID出现重复的案例:
​​​https://github.com/imadcn/idworker/issues/14​

三、mybatis-plus中datacenterId和workerId的默认生成规则

默认情况下,并不需要我们主动去配置datacenterId和workerId的值。​​mybatis-plus框架会根据应用所在服务器IP地址来生成datacenterId和workerId​​。

我们来看看DefaultIdentifierGenerator的构造方法:

//默认的无参构造方法
public DefaultIdentifierGenerator() {
this.sequence = new Sequence((InetAddress)null);
}

public DefaultIdentifierGenerator(InetAddress inetAddress) {
this.sequence = new Sequence(inetAddress);
}

#也可以主动指定datacenterId和workerId的值
public DefaultIdentifierGenerator(long workerId, long dataCenterId) {
this.sequence = new Sequence(workerId, dataCenterId);
}

根据ip地址初始化Sequence:

public Sequence(InetAddress inetAddress) {
this.inetAddress = inetAddress;
this.datacenterId = this.getDatacenterId(31L);
this.workerId = this.getMaxWorkerId(this.datacenterId, 31L);
}

根据ip地址生成datacenterId:

protected long getDatacenterId(long maxDatacenterId) {
long id = 0L;

try {
if (null == this.inetAddress) {
this.inetAddress = InetAddress.getLocalHost();
}

NetworkInterface network = NetworkInterface.getByInetAddress(this.inetAddress);
if (null == network) {
id = 1L;
} else {
byte[] mac = network.getHardwareAddress();
if (null != mac) {
id = (255L & (long)mac[mac.length - 2] | 65280L & (long)mac[mac.length - 1] << 8) >> 6;
id %= maxDatacenterId + 1L;
}
}
} catch (Exception var7) {
logger.warn(" getDatacenterId: " + var7.getMessage());
}

return id;
}

根据datacenterId生成workerId:

protected long getMaxWorkerId(long datacenterId, long maxWorkerId) {
StringBuilder mpid = new StringBuilder();
mpid.append(datacenterId);
String name = ManagementFactory.getRuntimeMXBean().getName();
if (StringUtils.isNotBlank(name)) {
mpid.append(name.split("@")[0]);
}

return (long)(mpid.toString().hashCode() & '\uffff') % (maxWorkerId + 1L);
}

小结:
无论是用户自己指定​​​datacenterId​​​和​​workerId​​​,还是根据IP地址自动生成​​datacenterId​​​和​​workerId​​​。显然在大规模的集群环境下都不利于集群的扩展和维护管理,而且容易出现​​datacenterId​​​和​​workerId​​相同而导致出现id重复的问题。

那么有没有方法自动管理​​datacenterId​​​和​​workerId​​的生成呢?

四、idworker介绍

idworker 是一个基于zookeeper和snowflake算法的分布式统一ID生成工具,通过zookeeper自动注册机器(最多1024台),​​无需手动指定workerId和dataCenterId​​。

在分布式集群中,可能需要部署的大量的机器节点。在节点少的受,可以人工维护。在量大的场景下,手动维护成本高,考虑到自动部署、运维等等问题,节点的命名,最好由系统自动维护。

节点的命名,主要是为节点进行唯一编号。主要的诉求是,不同节点的编号,是绝对的不能重复。一旦编号重复,就会导致有不同的节点碰撞,导致集群异常。

有以下两个方案,可供生成集群节点编号:
(1)使用数据库的自增ID特性,用数据表,存储机器的mac地址或者ip来维护。
(2)使用ZooKeeper持久顺序节点的次序特性,来维护节点的编号。

这里,我们采用第二种,通过ZooKeeper持久顺序节点特性,来配置维护节点的编号NODEID。
集群节点命名服务的基本流程是:
(1)启动节点服务,连接ZooKeeper, 检查命名服务根节点根节点是否存在,如果不存在就创建系统根节点。
(2)在根节点下创建一个临时顺序节点,取回顺序号做节点的NODEID。如何临时节点太多,可以根据需要,删除临时节点。

由于是采用zookeeper顺序节点的特性生成​​datacenterId​​​和​​workerId​​​,可以天然的保证​​datacenterId​​​和​​workerId​​的唯一性,减少了人工维护的弊端。

五、idworker实战

其中mybatis-plus内置的​​ImadcnIdentifierGenerator​​方法,就已经提供了对idworker框架的支持。

​对,你没看错,又又又是内置的,可是你却还不会用。​​不得不佩服mybatis-plus框架的开发者,太牛了。

mybatis-plus雪花算法增强:idworker_分布式Id_02


查看ImadcnIdentifierGenerator的源码,可以发现里面就是通过idworker实现的。

mybatis-plus雪花算法增强:idworker_zookeeper_03

1、引入maven依赖

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>

<dependency>
<groupId>com.imadcn.framework</groupId>
<artifactId>idworker</artifactId>
<version>1.5.0</version>
</dependency>

2、添加zookeeper配置

mybatis-plus.zookeeper.serverLists=127.0.0.1:2181

3、指定mybatis-plus的id生成器

@Configuration
public class IdAutoConfig {
@Value("${mybatis-plus.zookeeper.serverLists}")
private String zkServerLists;

@Bean
public IdentifierGenerator idGenerator() {
return new ImadcnIdentifierGenerator(zkServerLists);
}
}

4、测试

执行单元测试:

@Test
public void testInsert() {
System.out.println(("----- insert method test ------"));
User user = new User();
user.setName("test");
user.setAge(13);
user.setEmail("[email protected]");
userMapper.insert(user);
System.out.println(user.toString());
}

执行结果:

mybatis-plus雪花算法增强:idworker_mybatis-plus_04

Preparing: INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
Parameters: 728706665213329499(Long), test(String), 13(Integer), [email protected](String)
Updates: 1
User(id=728706665213329499, name=test, age=13, [email protected])

总结

本文主要介绍如何在mybatis-plus中引入idworker框架,通过zookeeper管理snowflake算法中workerId和dataCenterId`的生成,保证其唯一性,避免出现id重复的情况。


标签:workerId,idworker,datacenterId,plus,mybatis,节点
From: https://blog.51cto.com/u_15905482/5919837

相关文章