GithubHelp home page GithubHelp logo

izern / sequence Goto Github PK

View Code? Open in Web Editor NEW
37.0 3.0 11.0 216 KB

高效GUID产生算法(sequence),基于Snowflake实现64位自增ID算法.

License: Apache License 2.0

Java 100.00%
sequence hibernate idgenerator

sequence's Introduction

分布式高效唯一ID生成器(sequence)

基于开源项目sequence

简介

高效GUID产生算法(sequence),基于Snowflake实现64位自增ID算法。

Twitter-Snowflake算法产生的背景相当简单,为了满足Twitter每秒上万条消息的请求,每条消息都必须分配一条唯一的id,这些id还需要一些大致的顺序(方便客户端排序),并且在分布式系统中不同机器产生的id必须不同。

性能测试数据:

性能测试结果

Snowflake算法核心

把时间戳,工作机器id,序列号组合在一起。

Snowflake算法核心

除了最高位bit标记为不可用以外,其余三组bit占位均可浮动,看具体的业务需求而定。默认情况下41bit的时间戳可以支持该算法使用到2082年,10bit的工作机器id可以支持1023台机器,序列号支持1毫秒产生4095个自增序列id。下文会具体分析。

Snowflake – 时间戳

这里时间戳的细度是毫秒级,具体代码如下,建议使用64位linux系统机器,因为有vdso,gettimeofday()在用户态就可以完成操作,减少了进入内核态的损耗。

Snowflake – 工作机器id

严格意义上来说这个bit段的使用可以是进程级,机器级的话你可以使用MAC地址来唯一标示工作机器,工作进程级可以使用IP+Path来区分工作进程。如果工作机器比较少,可以使用配置文件来设置这个id是一个不错的选择,如果机器过多配置文件的维护是一个灾难性的事情。

Snowflake – 序列号

序列号就是一系列的自增id(多线程建议使用atomic),为了处理在同一毫秒内需要给多条消息分配id,若同一毫秒把序列号用完了,则“等待至下一毫秒”。

获取

<dependency>
	<groupId>cn.izern</groupId>
	<artifactId>sequence</artifactId>
	<version>${version}</version>
</dependency>

使用

import cn.izern.sequence.Sequence;

Sequence sequence = new Sequence();
sequence.nextId();

线程安全,生成唯一序列ID

hibernate/jpa 使用Sequence作为ID生成方式

private Long id;
	
// other 

@Id
@GeneratedValue(generator = "idGenerator")
@GenericGenerator(name = "idGenerator", strategy = "cn.izern.hibernate.id.IDSequenceGenerator")
public Long getId() {
	return id;
}

public void setId(Long id) {
	this.id = id;
}

sequence's People

Contributors

izern avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sequence's Issues

关于生成id 的问题

你好,我使用了下,这个是生成后18位数字是么?之前用的其他的是19位
另外我想问下,这个18位数字的话作为用户id或者文章id或者回复帖子的id长度会不会有点太长,如果想要6-12位的话怎么处理呢,谢谢

datacenterID与workerid问题

对于Sequence 的无参构造方法,会自动生成datacenterID和workerid,分布式环境下,如果保证几百台机器,获取到的不重复?

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.