• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

id-generator: 基于Twitter的SnowFlake算法实现的高性能分布式ID发号器。支持手动或通 ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

id-generator

开源软件地址:

https://gitee.com/simpleweb/id-generator

开源软件介绍:

id-generator

项目介绍

基于Twitter的SnowFlake算法实现的分布式ID发号器。支持手动或通过Zookeeper分配workerId。配置简单,操作简易。生成的id具备全局唯一,粗略有序,可反向解码等特性。

数据结构

毫秒级
 时间位数据中心工作机器序号位
位数415512
  • 时间41位,可以使用2^41/1000/60/60/24/365=69.73, 约可使用69年
  • 数据中心5位, 可部署2^5=32个数据中心
  • 工作机器5位,同一个数据中心可部署2^5=32个服务
  • 序号12位,表示同一机器同一时间(毫秒)内理论上可以产生最多2^12-1=4095个ID序号
秒级
 时间位数据中心工作机器序号位
位数315522
  • 时间31位,可以使用2^31/60/60/24/365=68, 约可使用68年
  • 数据中心5位, 可部署2^5=32个数据中心
  • 工作机器5位,同一个数据中心可部署2^5=32个服务
  • 序号22位,表示同一机器同一时间(秒)内理论上可以产生最多2^22-1=4194303个ID序号

安装

  1. 在pom.xml文件中增加以下仓库
<repositories>    <repository>        <id>iwanttomakemoney_admin</id>        <url>https://gitee.com/iwanttomakemoney_admin/maven/raw/master/repository</url>    </repository></repositories>

引入以下依赖

<dependencies>    <dependency>		<groupId>com.lxm</groupId>		<artifactId>id-generator-interface</artifactId>		<version>2.3</version>	</dependency>	<dependency>		<groupId>com.lxm</groupId>		<artifactId>id-generator-core</artifactId>		<version>2.3</version>	</dependency></dependencies>

配置

基于spring boot的项目
  1. 在yml或property配置文件中设置所需的参数.(此步骤非必需,若跳过此步骤将生效默认配置)
  2. 在启动类上增加@EnableIdGenerator注解即可. 如
@SpringBootApplication@EnableIdGeneratorpublic class DemoApplication {	public static void main(String[] args) {		SpringApplication.run(DemoApplication.class, args);	}}
普通的spring项目
  1. 在类路径下新增一个property配置文件, 设置所需参数(此步骤非必需,若跳过此步骤将生效默认配置)
  2. 在xxxContext.xml文件中增加如下配置以便识别上一步设置的参数
<bean id="appProperty"          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="locations">        <array>            <value>classpath:application.properties</value>        </array>    </property></bean>
  1. 新增一个配置类,在其上增加@EnableIdGenerator注解即可.如
@Configuration@EnableIdGeneratorpublic class WebConfigure {    // other configure }
非spring项目

在项目中增加一个单例类工具类, 如

public class IdUtil {    private static IdUtil _instance = new IdUtil();        private static IdService idService;    public static IdService service() {        return idService;    }    private IdUtil() {        // 使用提供的工厂类生成idService        idService = IdServiceFactoryBean.idService(new AutoConfiguration());    }}

使用

在需要的地方注入服务即可使用,如:

@Serviceclass DemoService {    @Autowired    private IdService idService;    public void test() {        // 通过自动装配提供的服务        long id1 = idService.genId();        // 通过单例的形式提供的服务        long id2 = IdUtil.service().genId();        ...    }}

API

// 生成idlong idService.genId();// 批量生成idlong[] batchGenId(int count);// 解析idId decode(long id);// 手动生成idlong encode(long time, long dataCenterId, long workerId, long seq);// 解析id中的时间戳Date transTime(long time);

参数

属性类型缺省值描述
id.zookeeper.enableBooleanfalse是否启用Zookeeper分配workerId。 默认为false表示使用手动分配workerId;若为true则需预先准备至少一个的Zookeeper服务
id.zookeeper.serverListsStringnull连接Zookeeper服务器的列表 包括IP地址和端口号 多个地址用逗号分隔如:host1:2181,host2:2181
id.zookeeper.digestStringnull连接Zookeeper的权限令牌 缺省为不需要权限验证
id.zookeeper.namespaceString"id-generator"Zookeeper的命名空间
id.zookeeper.baseSleepTimeInteger1000等待重试的间隔时间的初始值 单位:毫秒
id.zookeeper.maxSleepTimeInteger3000等待重试的间隔时间的最大值 单位:毫秒
id.zookeeper.maxRetriesInteger3最大重试次数
id.zookeeper.sessionTimeoutInteger60000会话超时时间 单位:毫秒
id.zookeeper.connectionTimeoutInteger15000连接超时时间 单位:毫秒
id.type.second=falseBooleanfalsetrue-秒级别 false-毫秒级别
id.workerIdInteger0手动指定工作机器号,当id.zookeeper.enable=false有效
id.datacenterIdInteger-1手动指定数据中心, 若不指定则将根据mac地址自动分配一个固定的编号

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap