雪花算法简单描述:
最高位是符号位,始终为0,不可用。
41位的时间序列,精确到毫秒级,41位的长度可以使用69年。时间位还有一个很重要的作用是可以根据时间进行排序。
10位的机器标识,10位的长度最多支持部署1024个节点。
12位的计数序列号,序列号即一系列的自增id,可以支持同一节点同一毫秒生成多个ID序号,12位的计数序列号支持每个节点每毫秒产生4096个ID序号。
看的出来,这个算法很简洁也很简单,但依旧是一个很好的ID生成策略。其中,10位器标识符一般是5位IDC+5位machine编号,唯一确定一台机器。
<?php
class lib_snowflake
{
const TWEPOCH = 1488834974657; // 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动)
const WORKER_ID_BITS = 5; // 机器标识位数
const DATACENTER_ID_BITS = 5; // 数据中心标识位数
const SEQUENCE_BITS = 11; // 毫秒内自增位
private $workerId; // 工作机器ID(0~31)
private $datacenterId; // 数据中心ID(0~31)
private $sequence; // 毫秒内序列(0~4095)
private $maxWorkerId = -1 ^ (-1 << self::WORKER_ID_BITS); // 机器ID最大值31
private $maxDatacenterId = -1 ^ (-1 << self::DATACENTER_ID_BITS); // 数据中心ID最大值31
private $workerIdShift = self::SEQUENCE_BITS; // 机器ID偏左移11位
private $datacenterIdShift = self::SEQUENCE_BITS + self::WORKER_ID_BITS; // 数据中心ID左移16位
private $timestampLeftShift = self::SEQUENCE_BITS + self::WORKER_ID_BITS + self::DATACENTER_ID_BITS; // 时间毫秒左移21位
private $sequenceMask = -1 ^ (-1 << self::SEQUENCE_BITS); // 生成序列的掩码4095
private $lastTimestamp = -1; // 上次生产id时间戳
public function __construct($workerId, $datacenterId, $sequence = 0)
{
if ($workerId > $this->maxWorkerId || $workerId < 0) {
throw new Exception("worker Id can't be greater than {$this->maxWorkerId} or less than 0");
}
if ($datacenterId > $this->maxDatacenterId || $datacenterId < 0) {
throw new Exception("datacenter Id can't be greater than {$this->maxDatacenterId} or less than 0");
}
$this->workerId = $workerId;
$this->datacenterId = $datacenterId;
$this->sequence = $sequence;
}
public function nextId()
{
$timestamp = $this->timeGen();
if ($timestamp < $this->lastTimestamp) {
$diffTimestamp = bcsub($this->lastTimestamp, $timestamp);
throw new Exception("Clock moved backwards. Refusing to generate id for {$diffTimestamp} milliseconds");
}
if ($this->lastTimestamp == $timestamp) {
$this->sequence = ($this->sequence + 1) & $this->sequenceMask;
if (0 == $this->sequence) {
$timestamp = $this->tilNextMillis($this->lastTimestamp);
}
} else {
$this->sequence = 0;
}
$this->lastTimestamp = $timestamp;
$gmpTimestamp = gmp_init($this->leftShift(bcsub($timestamp, self::TWEPOCH), $this->timestampLeftShift));
$gmpDatacenterId = gmp_init($this->leftShift($this->datacenterId, $this->datacenterIdShift));
$gmpWorkerId = gmp_init($this->leftShift($this->workerId, $this->workerIdShift));
$gmpSequence = gmp_init($this->sequence);
return gmp_strval(gmp_or(gmp_or(gmp_or($gmpTimestamp, $gmpDatacenterId), $gmpWorkerId), $gmpSequence));
}
protected function tilNextMillis($lastTimestamp)
{
$timestamp = $this->timeGen();
while ($timestamp <= $lastTimestamp) {
$timestamp = $this->timeGen();
}
return $timestamp;
}
protected function timeGen()
{
return floor(microtime(true) * 1000);
}
// 左移 <<
protected function leftShift($a, $b)
{
return bcmul($a, bcpow(2, $b));
}
}
第二种写法:
<?php
/**
* 分布式 id 生成类 组成: <毫秒级时间戳+机器id+序列号>
* 默认情况下41bit的时间戳可以支持该算法使用到2082年,10bit的工作机器id可以支持1023台机器,序列号支持1毫秒产生4095个自增序列id
* @author zhangqi
*/
class IdCreate
{
const EPOCH = 1479533469598; //开始时间,固定一个小于当前时间的毫秒数
const max12bit = 4095;
const max41bit = 1099511627775;
static $machineId = null; // 机器id
public static function machineId($mId = 0)
{
self::$machineId = $mId;
}
public static function createOnlyId()
{
// 时间戳 42字节
$time = floor(microtime(true) * 1000);
// 当前时间 与 开始时间 差值
$time -= self::EPOCH;
// 二进制的 毫秒级时间戳
$base = decbin(self::max41bit + $time);
// 机器id 10 字节
if(!self::$machineId)
{
$machineid = self::$machineId;
}
else
{
$machineid = str_pad(decbin(self::$machineId), 10, "0", STR_PAD_LEFT);
}
// 序列数 12字节
$random = str_pad(decbin(mt_rand(0, self::max12bit)), 12, "0", STR_PAD_LEFT);
// 拼接
$base = $base.$machineid.$random;
// 转化为 十进制 返回
return bindec($base);
}
}
IdCreate::machineId("1");//机器编号
echo IdCreate::createOnlyId();//分布式id