self::MAX_DATA_CENTER_ID) { throw new \Exception("数据中心ID超出范围"); } if ($machineId < 0 || $machineId > self::MAX_MACHINE_ID) { throw new \Exception("机器ID超出范围"); } $this->datacenterId = $datacenterId; $this->machineId = $machineId; $this->epoch = 1609430400000; // 自定义起始时间(例如2021年1月1日) } public function nextId(): int { $timestamp = $this->currentTimeMillis(); if ($timestamp < $this->lastTimestamp) { throw new \Exception("错误:系统时钟发生回拨"); } if ($this->lastTimestamp === $timestamp) { $this->sequence = ($this->sequence + 1) & ((1 << self::SEQUENCE_BITS) - 1); if ($this->sequence === 0) { $timestamp = $this->waitNextMillis($timestamp); } } else { $this->sequence = 0; } $this->lastTimestamp = $timestamp; return (($timestamp - $this->epoch) << self::TIMESTAMP_LEFT_SHIFT) | ($this->datacenterId << self::SEQUENCE_LEFT_SHIFT) | ($this->machineId << self::SEQUENCE_BITS) | $this->sequence; } private function waitNextMillis(int $lastTimestamp): int { $timestamp = $this->currentTimeMillis(); while ($timestamp <= $lastTimestamp) { $timestamp = $this->currentTimeMillis(); } return $timestamp; } private function currentTimeMillis(): int { return (int)(microtime(true) * 1000); } }