`ImadcnIdentifierGenerator` 深度解析

ImadcnIdentifierGenerator 深度解析

这是一个基于Zookeeper的分布式ID生成器实现,结合了雪花算法(Snowflake)和压缩UUID生成能力。下面我将从多个维度详细分析这个实现类。

java 复制代码
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.baomidou.mybatisplus.core.incrementer;

import com.imadcn.framework.idworker.config.ApplicationConfiguration;
import com.imadcn.framework.idworker.config.ZookeeperConfiguration;
import com.imadcn.framework.idworker.generator.CompressUUIDGenerator;
import com.imadcn.framework.idworker.generator.SnowflakeGenerator;
import com.imadcn.framework.idworker.register.zookeeper.ZookeeperWorkerRegister;
import com.imadcn.framework.idworker.registry.zookeeper.ZookeeperRegistryCenter;
import java.io.Closeable;
import java.io.IOException;

public class ImadcnIdentifierGenerator implements IdentifierGenerator, Closeable {
    private final SnowflakeGenerator idGenerator;
    private final CompressUUIDGenerator uuidGenerator;

    public ImadcnIdentifierGenerator(String serverLists) {
        this(configuration(serverLists));
    }

    public ImadcnIdentifierGenerator(ZookeeperConfiguration zookeeperConfiguration) {
        this(zookeeperConfiguration, new ApplicationConfiguration());
    }

    public ImadcnIdentifierGenerator(ZookeeperConfiguration zookeeperConfiguration, ApplicationConfiguration applicationConfiguration) {
        this.uuidGenerator = new CompressUUIDGenerator();
        ZookeeperRegistryCenter center = new ZookeeperRegistryCenter(zookeeperConfiguration);
        ZookeeperWorkerRegister register = new ZookeeperWorkerRegister(center, applicationConfiguration);
        this.idGenerator = new SnowflakeGenerator(register);
        this.idGenerator.init();
    }

    private static ZookeeperConfiguration configuration(String serverLists) {
        ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration();
        zookeeperConfiguration.setServerLists(serverLists);
        return zookeeperConfiguration;
    }

    public Number nextId(Object entity) {
        return this.idGenerator.nextId();
    }

    public String nextUUID(Object entity) {
        return this.uuidGenerator.nextStringId();
    }

    public void close() throws IOException {
        this.idGenerator.close();
    }
}

类继承与实现关系

复制代码
ImadcnIdentifierGenerator
├─ implements IdentifierGenerator (MyBatis-Plus接口)
├─ implements Closeable (资源关闭接口)

核心组件

1. ID生成器成员

java 复制代码
private final SnowflakeGenerator idGenerator;  // 雪花算法ID生成器
private final CompressUUIDGenerator uuidGenerator;  // 压缩UUID生成器

2. 构造方法链

提供了三种构造方式:

java 复制代码
// 1. 最简单构造(仅需Zookeeper服务器地址)
public ImadcnIdentifierGenerator(String serverLists)

// 2. 使用Zookeeper配置
public ImadcnIdentifierGenerator(ZookeeperConfiguration zookeeperConfiguration)

// 3. 完整配置(Zookeeper+应用配置)
public ImadcnIdentifierGenerator(ZookeeperConfiguration zookeeperConfiguration, 
                               ApplicationConfiguration applicationConfiguration)

核心方法实现

1. ID生成方法

java 复制代码
@Override
public Number nextId(Object entity) {
    return this.idGenerator.nextId(); // 使用雪花算法生成数字ID
}

@Override
public String nextUUID(Object entity) {
    return this.uuidGenerator.nextStringId(); // 生成压缩格式UUID
}

2. 资源清理

java 复制代码
@Override
public void close() throws IOException {
    this.idGenerator.close(); // 关闭雪花算法生成器
}

技术实现细节

雪花算法初始化流程

  1. 创建Zookeeper注册中心

  2. 创建Worker注册器

  3. 初始化雪花ID生成器

    java 复制代码
    ZookeeperRegistryCenter center = new ZookeeperRegistryCenter(zookeeperConfiguration);
    ZookeeperWorkerRegister register = new ZookeeperWorkerRegister(center, applicationConfiguration);
    this.idGenerator = new SnowflakeGenerator(register);
    this.idGenerator.init();

配置处理

java 复制代码
private static ZookeeperConfiguration configuration(String serverLists) {
    ZookeeperConfiguration config = new ZookeeperConfiguration();
    config.setServerLists(serverLists); // 设置Zookeeper集群地址
    return config;
}

设计亮点

  1. 分布式支持:通过Zookeeper协调Worker ID分配,避免集群环境下ID冲突
  2. 双ID策略:同时提供有序数字ID和高性能UUID生成能力
  3. 资源管理:实现Closeable接口确保正确释放Zookeeper连接
  4. 配置灵活:提供多种构造方法适应不同使用场景

典型使用场景

1. MyBatis-Plus集成

java 复制代码
@Bean
public IdentifierGenerator idGenerator() {
    return new ImadcnIdentifierGenerator("zk1:2181,zk2:2181");
}

2. 直接调用生成ID

java 复制代码
ImadcnIdentifierGenerator generator = new ImadcnIdentifierGenerator(config);

// 生成订单ID
Long orderId = generator.nextId(orderEntity).longValue();

// 生成业务流水号
String traceId = generator.nextUUID(null);

性能考量

  1. 雪花算法优势

    • 64位ID结构(时间戳+WorkerID+序列号)
    • 单机每秒可生成400万+ ID
    • 趋势递增,适合数据库索引
  2. 压缩UUID优化

    • 相比标准UUID更节省存储空间
    • 仍保持全局唯一性

扩展建议

  1. 故障转移:增加Zookeeper连接失败的备用方案
  2. 监控集成:添加ID生成速率等监控指标
  3. 自定义配置:支持更多雪花算法参数调整
  4. ID解析:添加从ID反解生成时间、WorkerID等信息的方法

与其他组件的对比

特性 ImadcnIdentifierGenerator 数据库序列 Redis自增 UUID
分布式支持
有序性
性能 极高 中等
依赖外部服务 Zookeeper 数据库 Redis
ID长度 64位数字 数字 数字 32位字符串

这个实现非常适合需要高吞吐量、分布式部署的场景,特别是在微服务架构中为各类业务实体生成唯一标识符。

相关推荐
程序媛学姐7 分钟前
SpringBoot Actuator健康检查:自定义HealthIndicator
java·spring boot·后端
白鸽(二般)11 分钟前
eclipse常用快捷键
java·ide·eclipse
猿java14 分钟前
10种常见的架构风格,你用过几种?
java·分布式·架构
virtuousOne16 分钟前
spring Ai---向量知识库(一)
java
刘大猫2619 分钟前
Arthas stack (输出当前方法被调用的调用路径)
java·人工智能·数据分析
网络技术 notebook23 分钟前
java输出、输入语句
java·开发语言
LiuYaoheng27 分钟前
深入理解Java包装类:自动装箱拆箱与缓存池机制
java·缓存
欲儿29 分钟前
RabbitMQ原理及代码示例
java·spring boot·后端·rabbitmq
寒也1 小时前
识别法院PDF文件特定字段并插入数据库【正则表达式+本地化部署】
java·数据库·正则表达式·eclipse·pdf·达梦·ruoyi
中国lanwp1 小时前
Spring Boot 版本与对应 JDK 版本兼容性
java·开发语言·spring boot