PHP源码实现案例雪花id SnowFlake
demo1<?php
/**
* 雪花算法类
* @package app\helpers
*/
class SnowFlake
{
const EPOCH = 1479533469598;
const max12bit = 4095;
const max41bit = 1099511627775;
static $machineId = null;
public static function machineId($mId = 0) {
self::$machineId = $mId;
}
public static function generateParticle() {
/*
* Time - 42 bits
*/
$time = floor(microtime(true) * 1000);
/*
* Substract custom epoch from current time
*/
$time -= self::EPOCH;
/*
* Create a base and add time to it
*/
$base = decbin(self::max41bit + $time);
/*
* Configured machine id - 10 bits - up to 1024 machines
*/
if(!self::$machineId) {
$machineid = self::$machineId;
} else {
$machineid = str_pad(decbin(self::$machineId), 10, "0", STR_PAD_LEFT);
}
/*
* sequence number - 12 bits - up to 4096 random numbers per machine
*/
$random = str_pad(decbin(mt_rand(0, self::max12bit)), 12, "0", STR_PAD_LEFT);
/*
* Pack
*/
$base = $base.$machineid.$random;
/*
* Return unique time id no
*/
return bindec($base);
}
public static function timeFromParticle($particle) {
/*
* Return time
*/
return bindec(substr(decbin($particle),0,41)) - self::max41bit + self::EPOCH;
}
}
demo2
<?php
public function createID(){
//假设一个机器id
$machineId = 1234567890;
//41bit timestamp(毫秒)
$time = floor(microtime(true) * 1000);
//0bit 未使用
$suffix = 0;
//datacenterId 添加数据的时间
$base = decbin(pow(2,40) - 1 + $time);
//workerId 机器ID
$machineid = decbin(pow(2,9) - 1 + $machineId);
//毫秒类的计数
$random = mt_rand(1, pow(2,11)-1);
$random = decbin(pow(2,11)-1 + $random);
//拼装所有数据
$base64 = $suffix.$base.$machineid.$random;
//将二进制转换int
$base64 = bindec($base64);
$id = sprintf('%.0f', $base64);
return $id;
}
SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),效率较高。但是依赖与系统时间的一致性,如果系统时间被回调,或者改变,可能会造成id冲突或者重复。实际中我们的机房并没有那么多,我们可以改进改算法,将10bit的机器id优化,成业务表或者和我们系统相关的业务。