- 浏览: 155385 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (118)
- rest (2)
- spring (8)
- java proxy (1)
- linux (9)
- nginx (1)
- 加密算法 (2)
- jquery (3)
- hibernate (9)
- bootstrap (0)
- mysql (15)
- java (6)
- 应用服务器 (2)
- jdbc (3)
- js (3)
- springMVC (3)
- JAVA基础分类 (2)
- mycat (5)
- mybatis (0)
- drools规则引擎 (0)
- 压力测试工具 (1)
- 日志管理 (3)
- maven (3)
- 数据源 (1)
- kryo 序列化 (1)
- dubbo (3)
- com.google.common.collect 工具类 (2)
- memcache (2)
- jdk (1)
- 正则 (2)
- amoeba (1)
- 分布式事务 (2)
- html5 (1)
- spring-data-elasticSearch (2)
- shell脚本 (1)
- Elasticsearch (9)
- 设计模式 (2)
- NOSQL (1)
- hash算法 (4)
- 多线程 (0)
- 电商 (1)
- pinpoint (0)
最新评论
JAVA 实现:
PHP实现:
压力测试结果,8g内存的笔记本,10w次请求1000线程
压力测试代码:
package io.github.id; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 第一位为未使用(实际上也可作为long的符号位),接下来的41位为毫秒级时间,然后5位datacenter标识位,</br> * 5位机器ID(并不算标识符,实际是为线程标识),然后12位该毫秒内的当前毫秒内的计数,加起来刚好64位,为一个Long型。 * 0---0000000000 0000000000 0000000000 0000000000 0 --- 00000 ---00000 ---000000000000 * @author cailin * */ public class IdWorker { protected static final Logger LOG = LoggerFactory.getLogger(IdWorker.class); //机器id private long workerId; //数据中心id private long datacenterId; private long sequence = 0L; private long twepoch = 1288834974657L; //机器标识位数 private long workerIdBits = 5L; //数据中心标识位数 private long datacenterIdBits = 5L; //机器ID最大值 private long maxWorkerId = -1L ^ (-1L << workerIdBits); //数据中心ID最大值 private long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); //毫秒内自增位 private long sequenceBits = 12L; //机器ID偏左移12位 private long workerIdShift = sequenceBits; //数据中心ID左移17位 private long datacenterIdShift = sequenceBits + workerIdBits; //时间毫秒左移22位 private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; private long sequenceMask = -1L ^ (-1L << sequenceBits); private long lastTimestamp = -1L; /** * @param workerId 机器id * @param datacenterId 数据中心id */ public IdWorker(long workerId, long datacenterId) { // sanity check for workerId 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; LOG.info(String.format("worker starting. timestamp left shift %d, datacenter id bits %d, worker id bits %d, sequence bits %d, workerid %d", timestampLeftShift, datacenterIdBits, workerIdBits, sequenceBits, workerId)); } public synchronized long nextId() { long timestamp = timeGen(); if (timestamp < lastTimestamp) { LOG.error(String.format("clock is moving backwards. Rejecting requests until %d.", 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 return ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift) | (workerId << workerIdShift) | sequence; } protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } protected long timeGen() { return System.currentTimeMillis(); } public static void main(String[] args) { IdWorker worker = new IdWorker(1, 1); worker.nextId(); } }
PHP实现:
<?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 5 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at the following url: | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Original Author <author@example.com> | // | Your Name <you@example.com> | // +----------------------------------------------------------------------+ // // $Id:$ class Idwork { const debug = 1; static $workerId; static $twepoch = 1361775855078; static $sequence = 0; const workerIdBits = 4; static $maxWorkerId = 15; const sequenceBits = 10; static $workerIdShift = 10; static $timestampLeftShift = 14; static $sequenceMask = 1023; private static $lastTimestamp = - 1; function __construct($workId) { if ($workId > self::$maxWorkerId || $workId < 0) { throw new Exception("worker Id can't be greater than 15 or less than 0"); } self::$workerId = $workId; echo 'logdebug->__construct()->self::$workerId:' . self::$workerId; echo '</br>'; } function timeGen() { //获得当前时间戳 $time = explode(' ', microtime()); $time2 = substr($time[0], 2, 3); $timestramp = $time[1] . $time2; echo 'logdebug->timeGen()->$timestramp:' . $time[1] . $time2; echo '</br>'; return $time[1] . $time2; } function tilNextMillis($lastTimestamp) { $timestamp = $this->timeGen(); while ($timestamp <= $lastTimestamp) { $timestamp = $this->timeGen(); } echo 'logdebug->tilNextMillis()->$timestamp:' . $timestamp; echo '</br>'; return $timestamp; } function nextId() { $timestamp = $this->timeGen(); echo 'logdebug->nextId()->self::$lastTimestamp1:' . self::$lastTimestamp; echo '</br>'; if (self::$lastTimestamp == $timestamp) { self::$sequence = (self::$sequence + 1) & self::$sequenceMask; if (self::$sequence == 0) { echo "###########" . self::$sequenceMask; $timestamp = $this->tilNextMillis(self::$lastTimestamp); echo 'logdebug->nextId()->self::$lastTimestamp2:' . self::$lastTimestamp; echo '</br>'; } } else { self::$sequence = 0; echo 'logdebug->nextId()->self::$sequence:' . self::$sequence; echo '</br>'; } if ($timestamp < self::$lastTimestamp) { throw new Excwption("Clock moved backwards. Refusing to generate id for " . (self::$lastTimestamp - $timestamp) . " milliseconds"); } self::$lastTimestamp = $timestamp; echo 'logdebug->nextId()->self::$lastTimestamp3:' . self::$lastTimestamp; echo '</br>'; echo 'logdebug->nextId()->(($timestamp - self::$twepoch << self::$timestampLeftShift )):' . ((sprintf('%.0f', $timestamp) - sprintf('%.0f', self::$twepoch))); echo '</br>'; $nextId = ((sprintf('%.0f', $timestamp) - sprintf('%.0f', self::$twepoch))) | (self::$workerId << self::$workerIdShift) | self::$sequence; echo 'timestamp:' . $timestamp . '-----'; echo 'twepoch:' . sprintf('%.0f', self::$twepoch) . '-----'; echo 'timestampLeftShift =' . self::$timestampLeftShift . '-----'; echo 'nextId:' . $nextId . '----'; echo 'workId:' . self::$workerId . '-----'; echo 'workerIdShift:' . self::$workerIdShift . '-----'; return $nextId; } } $Idwork = new Idwork(1); $a = $Idwork->nextId(); $Idwork = new Idwork(2); $a = $Idwork->nextId(); ?>
压力测试结果,8g内存的笔记本,10w次请求1000线程
time:0:00:49.145 speed:4069589.9888086272
压力测试代码:
package io.github.id; import java.util.HashSet; import java.util.Set; import java.util.concurrent.CountDownLatch; import org.apache.commons.lang3.time.StopWatch; public class Benchmark { public static void main(String[] args) throws InterruptedException { Benchmark benchmark = new Benchmark(); benchmark.test(); } public void test() throws InterruptedException { int threadCount = 2000; final int genCount = 100000; StopWatch watch = new StopWatch(); final IdWorker idWorker = new IdWorker(0, 0); final CountDownLatch latch = new CountDownLatch(threadCount); final Set<Long> set = new HashSet(); watch.start(); for (int i = 0; i < threadCount; ++i) { Thread thread = new Thread() { public void run() { for (int j = 0; j < genCount; ++j) { long id = idWorker.nextId(); } latch.countDown(); } }; thread.start(); } latch.await(); watch.stop(); System.err.println("time:" + watch); System.err.println("speed:" + genCount * threadCount / (watch.getTime() / 1000.0)); } }
相关推荐
We have retired the initial release...Source code is still in the repository and is reachable from snowflake-2010 tag. We won't be accepting pull requests or responding to issues for the retired release.
Twitter的分布式自增ID算法snowflake (Java版)
Twitter的分布式自增ID雪花算法snowflake (Java版)
分布式ID生成,主键生成,Java实现的snowflake算法
主要介绍了Java实现Twitter的分布式自增ID算法snowflake,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Twitter Snowflake算法,php版代码; 请见博客: http://blog.csdn.net/envon123/article/details/52953872
分布式ID生成,主键生成策略,snowflake算法,采用java实现。
迄今为止最全面的分布式主键ID生成器。 优化的雪花算法(SnowFlake)——雪花漂移算法,在缩短ID长度的同时,具备极高瞬时并发处理能力(50W/0.1s)。 原生支持 C#/Java/Go/Rust/C/SQL 等多语言,且提供 PHP 扩展及 ...
而twitter的snowflake解决了这种需求,最初是Twitter把存储系统从MySQL迁移到Cassandra,因为Cassandra没有顺序的ID生成机制,所以开发了这样一套唯一的ID生成服务。结构雪花的结构如下(每部分用-分开): 0 - ...
nodejs-snowflake是一种快速可靠的方法,可以为分布式系统生成时间可排序的64位ID。 主要的ID生成功能是使用N-API用C ++编写的,这使得ID生成的过程非常快。 使用C ++进行id生成还可以确保生成的数字大小为64位。 ...
迄今为止最全面的分布式主键ID生成器。优化的雪花算法(SnowFlake)——雪花漂移算法,在缩短ID长度的同时,具备极高瞬时并发处理能力(50W/0.1s)。 原生支持 C#/Java/Go/Rust/C/SQL 等多语言,且提供 PHP 扩展及 ...
迄今为止最全面的分布式主键ID生成器。 优化的雪花算法(SnowFlake)——雪花漂移算法,在缩短ID长度的同时,具备极高瞬时并发处理能力(50W/0.1s)。 原生支持 C#/Java/Go/Rust/C/SQL 等多语言,且提供 PHP 扩展及 ...
idworker-基于zookeeper,snowflake的分布式统一ID生成工具 是什么 idworker是一个基于zookeeper和snowflake算法的分布式统一ID生成工具,通过zookeeper自动注册机器(最多1024台),无需手动指定workerId和...
本分布式主键ID生成器基于多语言优化,包含204个文件,包括Markdown文档、Dockerfile、Go源代码、Java源代码、GIT忽略文件、Header文件、C#源代码、C源代码、Pascal源代码、Rust源代码。系统采用优化的雪花算法...
最全面的分布式主键ID生成器。 优化的雪花算法(SnowFlake)——雪花漂移算法,在缩短ID长度的同时,具备极高瞬时并发处理能力(50W/0.1s)。 原生支持 C#/Java/Go/Rust/C/SQL 等多语言,且提供 PHP 扩展及 Python、...
Laravel开发-laravel-snowflake 为拉拉维尔雪花。
各种金融、电商、支付、等系统中产生的数据越来越多,对数据库进行分库分表是比较常见的,而分库后则需要有一个唯一ID来标识一条数据或消息,单个数据库的自增ID显然不能满足需求,此时也会需要一个能够生成全局唯一...
SnowFlake with Zookeeper in Kotlin 使用Zookeeper管理SnowFlake集群的workId 安装 下载并解压 进入解压目录并执行 ./SnowFlakeWithZK.jar start API GET http(s)://[host]:[port]/api/next/long 以长整型返回 GET...