分布式id的生成方式——雪花算法
雪花算法是twitter开源的一个算法。
由64位0或1组成,其中41位是时间戳,10位工作机器id,12位序列号,该类通过方法nextID()实现id的生成,用Long数据类型去存储。
我们使用idworker不建议每次都通过new的方式使用,如果在Spring中,可以通过如下方式将该bean注入到Spring容器中
- <bean id="idWorker" class="utils.IdWorker">
- <!-- 工作机器ID:值范围是0-31 数据中心ID:值范围是0-31,两个参数可以不写 -->
- <constructor-arg index="0" value="0"></constructor-arg>
- <constructor-arg index="1" value="0"></constructor-arg>
- </bean>
工具类IdWorker 的完整代码如下
- import java.lang.management.ManagementFactory;
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- /**
- * <p>名称:IdWorker.java</p>
- * <p>描述:分布式自增长ID</p>
- * <pre>
- * Twitter的 Snowflake JAVA实现方案
- * </pre>
- * 核心代码为其IdWorker这个类实现,其原理结构如下,我分别用一个0表示一位,用—分割开部分的作用:
- * 1||0---0000000000 0000000000 0000000000 0000000000 0 --- 00000 ---00000 ---000000000000
- * 在上面的字符串中,第一位为未使用(实际上也可作为long的符号位),接下来的41位为毫秒级时间,
- * 然后5位datacenter标识位,5位机器ID(并不算标识符,实际是为线程标识),
- * 然后12位该毫秒内的当前毫秒内的计数,加起来刚好64位,为一个Long型。
- * 这样的好处是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由datacenter和机器ID作区分),
- * 并且效率较高,经测试,snowflake每秒能够产生26万ID左右,完全满足需要。
- * <p>
- * 64位ID (42(毫秒)+5(机器ID)+5(业务编码)+12(重复累加))
- *
- * @author Polim
- */
- public class IdWorker {
- // 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动)
- private final static long twepoch = 1288834974657L;
- // 机器标识位数
- private final static long workerIdBits = 5L;
- // 数据中心标识位数
- private final static long datacenterIdBits = 5L;
- // 机器ID最大值
- private final static long maxWorkerId = -1L ^ (-1L << workerIdBits);
- // 数据中心ID最大值
- private final static long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
- // 毫秒内自增位
- private final static long sequenceBits = 12L;
- // 机器ID偏左移12位
- private final static long workerIdShift = sequenceBits;
- // 数据中心ID左移17位
- private final static long datacenterIdShift = sequenceBits + workerIdBits;
- // 时间毫秒左移22位
- private final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
- private final static long sequenceMask = -1L ^ (-1L << sequenceBits);
- /* 上次生产id时间戳 */
- private static long lastTimestamp = -1L;
- // 0,并发控制
- private long sequence = 0L;
- private final long workerId;
- // 数据标识id部分
- private final long datacenterId;
- public IdWorker(){
- this.datacenterId = getDatacenterId(maxDatacenterId);
- this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
- }
- /**
- * @param workerId
- * 工作机器ID
- * @param datacenterId
- * 序列号
- */
- public IdWorker(long workerId, long datacenterId) {
- if (workerId > maxWorkerId || workerId < 0) {
- throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
- }
- if (datacenterId > maxDatacenterId || datacenterId < 0) {
- throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
- }
- this.workerId = workerId;
- this.datacenterId = datacenterId;
- }
- /**
- * 获取下一个ID
- *
- * @return
- */
- public synchronized long nextId() {
- long timestamp = timeGen();
- if (timestamp < lastTimestamp) {
- throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
- }
- if (lastTimestamp == timestamp) {
- // 当前毫秒内,则+1
- sequence = (sequence + 1) & sequenceMask;
- if (sequence == 0) {
- // 当前毫秒内计数满了,则等待下一秒
- timestamp = tilNextMillis(lastTimestamp);
- }
- } else {
- sequence = 0L;
- }
- lastTimestamp = timestamp;
- // ID偏移组合生成最终的ID,并返回ID
- long nextId = ((timestamp - twepoch) << timestampLeftShift)
- | (datacenterId << datacenterIdShift)
- | (workerId << workerIdShift) | sequence;
- return nextId;
- }
- private long tilNextMillis(final long lastTimestamp) {
- long timestamp = this.timeGen();
- while (timestamp <= lastTimestamp) {
- timestamp = this.timeGen();
- }
- return timestamp;
- }
- private long timeGen() {
- return System.currentTimeMillis();
- }
- /**
- * <p>
- * 获取 maxWorkerId
- * </p>
- */
- protected static long getMaxWorkerId(long datacenterId, long maxWorkerId) {
- StringBuffer mpid = new StringBuffer();
- mpid.append(datacenterId);
- String name = ManagementFactory.getRuntimeMXBean().getName();
- if (!name.isEmpty()) {
- /*
- * GET jvmPid
- */
- mpid.append(name.split("@")[0]);
- }
- /*
- * MAC + PID 的 hashcode 获取16个低位
- */
- return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
- }
- /**
- * <p>
- * 数据标识id部分
- * </p>
- */
- protected static long getDatacenterId(long maxDatacenterId) {
- long id = 0L;
- try {
- InetAddress ip = InetAddress.getLocalHost();
- NetworkInterface network = NetworkInterface.getByInetAddress(ip);
- if (network == null) {
- id = 1L;
- } else {
- byte[] mac = network.getHardwareAddress();
- id = ((0x000000FF & (long) mac[mac.length - 1])
- | (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6;
- id = id % (maxDatacenterId + 1);
- }
- } catch (Exception e) {
- System.out.println(" getDatacenterId: " + e.getMessage());
- }
- return id;
- }
- public static void main(String[] args) {
- IdWorker idWorker = new IdWorker(0,1);
- for (int i = 0; i < 50; i++) {
- long nextId = idWorker.nextId();
- System.out.println(nextId);
- }
- }
- }
除了使用雪花算法之外还可以采用
redis生成唯一key值、数据库单键一张表生成id、UUID(缺点无序)
分布式id的生成方式——雪花算法的更多相关文章
- 分布式ID生成器 snowflake(雪花)算法
在springboot的启动类中引入 @Bean public IdWorker idWorkker(){ return new IdWorker(1, 1); } 在代码中调用 @Autowired ...
- 分布式id生成器,雪花算法IdWorker
/** * <p>名称:IdWorker.java</p> * <p>描述:分布式自增长ID</p> * <pre> * Twitter的 ...
- 全局唯一iD的生成 雪花算法详解及其他用法
一.介绍 雪花算法的原始版本是scala版,用于生成分布式ID(纯数字,时间顺序),订单编号等. 自增ID:对于数据敏感场景不宜使用,且不适合于分布式场景.GUID:采用无意义字符串,数据量增大时造成 ...
- 分布式ID方案SnowFlake雪花算法分析
1.算法 SnowFlake算法生成的数据组成结构如下: 在java中用long类型标识,共64位(每部分用-分开): 0 - 0000000000 0000000000 0000000000 000 ...
- 雪花算法【分布式ID问题】【刘新宇】
分布式ID 1 方案选择 UUID UUID是通用唯一识别码(Universally Unique Identifier)的缩写,开放软件基金会(OSF)规范定义了包括网卡MAC地址.时间戳.名字空间 ...
- 使用雪花算法为分布式下全局ID、订单号等简单解决方案考虑到时钟回拨
1.snowflake简介 互联网快速发展的今天,分布式应用系统已经见怪不怪,在分布式系统中,我们需要各种各样的ID,既然是ID那么必然是要保证全局唯一,除此之外,不同当业务还需要不同 ...
- 分布式ID系列(5)——Twitter的雪法算法Snowflake适合做分布式ID吗
介绍Snowflake算法 SnowFlake算法是国际大公司Twitter的采用的一种生成分布式自增id的策略,这个算法产生的分布式id是足够我们我们中小公司在日常里面的使用了.我也是比较推荐这一种 ...
- 如何设计一个分布式 ID 发号器?
大家好,我是树哥. 在复杂的分布式系统中,往往需要对大量的数据和消息进行唯一标识,例如:分库分表的 ID 主键.分布式追踪的请求 ID 等等.于是,设计「分布式 ID 发号器」就成为了一个非常常见的系 ...
- 雪花算法(snowflake)的JAVA实现
snowflake算法由twitter公司出品,原始版本是scala版,用于生成分布式ID,结构图: 算法描述: 最高位是符号位,始终为0,不可用. 41位的时间序列,精确到毫秒级,41位的长度可以使 ...
随机推荐
- 笔记:Java Language Specification - 章节17- 线程和锁
回答一个问题:多线程场景下,有时一个线程对shared variable的修改可能对另一个线程不可见.那么,何时一个线程对内存的修改才会对另一个线程可见呢? 基本的原则: 如果 读线程 和 写线程 不 ...
- Mybatis源码解析(三) —— Mapper代理类的生成
Mybatis源码解析(三) -- Mapper代理类的生成 在本系列第一篇文章已经讲述过在Mybatis-Spring项目中,是通过 MapperFactoryBean 的 getObject( ...
- 【开发笔记】-通过js控制input禁止输入空格
<input type="text" id="fname" onkeyup="myFunction(id)"> <scri ...
- CentOS 7 - 安装Python 3
Enable Software Collections (SCL) Software Collections, also known as SCL is a community project tha ...
- .Net core 使用swagger进行Api管理
上次我们讲过如何在swagger上隐藏接口,众所周知,swagger是一个强大的api文档工具,可以帮助我们记录文档并且测试接口,也是一个可视化操作接口的工具. 那么如果我们写的接口非常多的时候怎么办 ...
- ubuntu用samba来实现和虚拟机的文件共享
1.安装samba sudo apt install -y samba sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak 2.创建文件夹并修改权限 ...
- U盘启动安装CentOS 7出现 -dracut initqueue timeout
使用U盘启动安装CentOS7出现 Warning: dracut-initqueue timeout - starting timeout scripts 的解决办法 原因: ISO下,在/isol ...
- linux启动脚本
1. linux启动脚本 : /etc/init.d/脚本例如:/etc/init.d/iptables start init.d/ 下面的每一个文件就是一个启动脚本 2. 以上的/etc/init ...
- chrome开发者工具--使用 Network 面板测量您的网站网络性能。
转自:Tools for Web Developers Network 面板记录页面上每个网络操作的相关信息,包括详细的耗时数据.HTTP 请求与响应标头和 Cookie,等等. TL;DR 使用 ...
- Unity历史版本的文档
前言 在我们的开发过程中,如果要查找Unity文档,通常会有以下两种方式: 1. 打开Unity的官网,查找文档 2. 查找本地安装的Unity文档 但是Unity官网上的文档,总是当前最新版本的文档 ...