Giter VIP home page Giter VIP logo

idworker's Introduction

idworker - 基于zookeeper,snowflake的分布式统一ID生成工具

Build Status Coverage Status Maven Central License

是什么

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

怎么用

Maven

<dependency>
  <groupId>com.imadcn.framework</groupId>
  <artifactId>idworker</artifactId>
  <version>${latest.version}</version>
</dependency>

XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:idworker="http://code.imadcn.com/schema/idworker"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.imadcn.com/schema/idworker
        http://code.imadcn.com/schema/idworker/idworker.xsd">
        
    <idworker:registry id="zkRegistryCenter" server-lists="host1:port1,host2:port2"/>
    <idworker:generator id="snowflakeGenerator" registry-center-ref="zkRegistryCenter" />
</beans>

API

@Autowired
public IdGenerator generator;

public void id() {
    long id = generator.nextId(); // Long型ID(64进制UUID不支持),随着时间推移,ID长度为7-19位
    long[] ids = generator.nextId(100_000); // 批量Long型ID(64进制UUID不支持),最多10w个
	
    String strId = generator.nextStringId(); // 字符串格式ID
    String fixedId = generator.nextFixedStringId(); // 固定19位长度字符串Id
}

配置参考

<idworker:registry /> 注册中心配置,如zookeeper(64进制UUID策略可不配置注册中心)

属性 类型 必填 缺省值 兼容版本 描述
id String 1.0.0+ Spring容器中的ID
server-lists String 1.0.0+ 连接Zookeeper服务器的列表
包括IP地址和端口号
多个地址用逗号分隔
如: host1:2181,host2:2181
namespace String idworker 1.0.0+ Zookeeper的命名空间
base-sleep-time-milliseconds int 1000 1.0.0+ 等待重试的间隔时间的初始值
单位:毫秒
max-sleep-time-milliseconds int 3000 1.0.0+ 等待重试的间隔时间的最大值
单位:毫秒
max-retries int 3 1.0.0+ 最大重试次数
session-timeout-milliseconds int 60000 1.0.0+ 会话超时时间
单位:毫秒
connection-timeout-milliseconds int 15000 1.0.0+ 连接超时时间
单位:毫秒
digest String 1.0.0+ 连接Zookeeper的权限令牌
缺省为不需要权限验证

<idworker:generator /> ID生成策略配置

属性 类型 必填 缺省值 兼容版本 描述
id String 1.0.0+ Spring容器中的ID
strategy String snowflake 1.2.0+ ID生成[snowflake, compress_uuid],当策略为64进制uuid时,registry-center-ref可不用配置
registry-center-ref String 1.0.0+ 注册中心SpringBeanRef,当生成策略为snowflake时,必填
group String default 1.0.0+ 分组名,可以为不同业务分配分组,独立注册
registry-file String ./tmp/idworker/GROUPNAME.cache 1.3.0+ 注册信息缓存文件地址,默认在程序所在目录 ./tmp/idworker/GROUPNAME.cache
durable String false 1.4.0+ ID注册信息是否持久化存储(持久化存储会依赖于本地缓存文件,容器环境建议使用非持久化)

<generator:snowflake /> 生成策略 : snowflake模式

属性 类型 必填 缺省值 兼容版本 描述
id String 1.2.0+ Spring容器中的ID
registry-center-ref String 1.2.0+ 注册中心SpringBeanRef
group String default 1.2.0+ 分组名,可以为不同业务分配分组,独立注册
registry-file String ./tmp/idworker/GROUPNAME.cache 1.3.0+ 注册信息缓存文件地址,默认在程序所在目录 ./tmp/idworker/GROUPNAME.cache
durable String false 1.4.0+ ID注册信息是否持久化存储(持久化存储会依赖于本地缓存文件,容器环境建议使用非持久化)

<generator:compress-uuid /> 生成策略 : 64进制UUID模式

属性 类型 必填 缺省值 兼容版本 描述
id String 1.2.0+ Spring容器中的ID

idworker's People

Contributors

caihj avatar dependabot[bot] avatar imadcn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.