分布式环境下生成workerId问题

生成的雪花Id,这里面时间位是41位差不多69年,机器位是10位使用机器的mac值后两位做的,最后12位是单节点同一时间最多会出现4095个数,理论上来说都是够用的。但是在分布式场景下会出现生成的workerId相同,我目前的解决思路是通过redis操作,因为我最多机器也就6台同时运行,生成的值通过incr操作然后进行1023取余是不会出现重复的;

/**
     * 从redis获取到workerId
     * @return
     */
    public static long initWorkerIdByRedis() {
        RedisUtil redisUtil = (RedisUtil)SpringContextUtils.getBean("redisUtil");
        long workerId = redisUtil.incr("workerId", 1);
        workerId = workerId % maxWorkerId;
        return workerId;
    }

下面这个代码是seata里面生成的id的代码

/*
 *  Copyright 1999-2019 Seata.io Group.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package io.seata.common.util;

import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author funkye
 * @author selfishlover
 */
public class IdWorker {

    /**
     * Start time cut (2020-05-03)
     */
    private final long twepoch = 1588435200000L;

    /**
     * The number of bits occupied by workerId
     */
    private final int workerIdBits = 10;

    /**
     * The number of bits occupied by timestamp
     */
    private final int timestampBits = 41;

    /**
     * The number of bits occupied by sequence
     */
    private final int sequenceBits = 12;

    /**
     * Maximum supported machine id, the result is 1023
     */
    private final int maxWorkerId = ~(-1 << workerIdBits);

    /**
     * business meaning: machine ID (0 ~ 1023)
     * actual layout in memory:
     * highest 1 bit: 0
     * middle 10 bit: workerId
     * lowest 53 bit: all 0
     */
    private long workerId;

    /**
     * timestamp and sequence mix in one Long
     * highest 11 bit: not used
     * middle  41 bit: timestamp
     * lowest  12 bit: sequence
     */
    private AtomicLong timestampAndSequence;

    /**
     * mask that help to extract timestamp and sequence from a long
     */
    private final long timestampAndSequenceMask = ~(-1L << (timestampBits + sequenceBits));

    /**
     * instantiate an IdWorker using given workerId
     * @param workerId if null, then will auto assign one
     */
    public IdWorker(Long workerId) {
        initTimestampAndSequence();
        initWorkerId(workerId);
    }

    /**
     * init first timestamp and sequence immediately
     */
    private void initTimestampAndSequence() {
        long timestamp = getNewestTimestamp();
        long timestampWithSequence = timestamp << sequenceBits;
        this.timestampAndSequence = new AtomicLong(timestampWithSequence);
    }

    /**
     * init workerId
     * @param workerId if null, then auto generate one
     */
    private void initWorkerId(Long workerId) {
        if (workerId == null) {
            workerId = generateWorkerId();
        }
        if (workerId > maxWorkerId || workerId < 0) {
            String message = String.format("worker Id can't be greater than %d or less than 0", maxWorkerId);
            throw new IllegalArgumentException(message);
        }
        this.workerId = workerId << (timestampBits + sequenceBits);
    }

    /**
     * get next UUID(base on snowflake algorithm), which look like:
     * highest 1 bit: always 0
     * next   10 bit: workerId
     * next   41 bit: timestamp
     * lowest 12 bit: sequence
     * @return UUID
     */
    public long nextId() {
        waitIfNecessary();
        long next = timestampAndSequence.incrementAndGet();
        long timestampWithSequence = next & timestampAndSequenceMask;
        return workerId | timestampWithSequence;
    }

    /**
     * block current thread if the QPS of acquiring UUID is too high
     * that current sequence space is exhausted
     */
    private void waitIfNecessary() {
        long currentWithSequence = timestampAndSequence.get();
        long current = currentWithSequence >>> sequenceBits;
        long newest = getNewestTimestamp();
        if (current >= newest) {
            try {
                Thread.sleep(5);
            } catch (InterruptedException ignore) {
                // don't care
            }
        }
    }

    /**
     * get newest timestamp relative to twepoch
     */
    private long getNewestTimestamp() {
        return System.currentTimeMillis() - twepoch;
    }

    /**
     * auto generate workerId, try using mac first, if failed, then randomly generate one
     * @return workerId
     */
    private long generateWorkerId() {
        try {
            return generateWorkerIdBaseOnMac();
        } catch (Exception e) {
            return generateRandomWorkerId();
        }
    }

    /**
     * use lowest 10 bit of available MAC as workerId
     * @return workerId
     * @throws Exception when there is no available mac found
     */
    private long generateWorkerIdBaseOnMac() throws Exception {
        Enumeration<NetworkInterface> all = NetworkInterface.getNetworkInterfaces();
        while (all.hasMoreElements()) {
            NetworkInterface networkInterface = all.nextElement();
            boolean isLoopback = networkInterface.isLoopback();
            boolean isVirtual = networkInterface.isVirtual();
            if (isLoopback || isVirtual) {
                continue;
            }
            byte[] mac = networkInterface.getHardwareAddress();
            return ((mac[4] & 0B11) << 8) | (mac[5] & 0xFF);
        }
        throw new RuntimeException("no available mac found");
    }

    /**
     * randomly generate one as workerId
     * @return workerId
     */
    private long generateRandomWorkerId() {
        return new Random().nextInt(maxWorkerId + 1);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 分布式ID生成算法是一种特殊的哈希算法,它可以使用Java代码来生成一个唯一的、全局递增的ID,这样可以避免ID发生重复。下面是一段可以用来生成分布式IDJava代码: public static long generateId() { long currentTime = System.currentTimeMillis(); long nextId = (currentTime << 8) + getRandomNumber(0, 255); return nextId; }private static int getRandomNumber(int min, int max) { Random random = new Random(); return random.nextInt((max - min) + 1) + min; } ### 回答2: 分布式ID生成算法在分布式系统中非常重要,用于生成唯一的标识符。Java语言提供了很多方法来实现这一目标,下面是一个使用Snowflake算法生成分布式ID的示例代码: ```java public class DistributedIdGenerator { private static final long EPOCH = 1609459200000L; // 自定义起始时间点,用于减小ID长度 private long workerId; private long sequence = 0L; private long lastTimestamp = -1L; public DistributedIdGenerator(long workerId) { if (workerId < 0L || workerId > 1023L) { throw new IllegalArgumentException("Worker ID must be between 0 and 1023."); } this.workerId = workerId; } public synchronized long generateId() { long currentTimestamp = System.currentTimeMillis(); if (currentTimestamp < lastTimestamp) { throw new RuntimeException("Clock moved backwards. Refusing to generate ID."); } if (currentTimestamp == lastTimestamp) { sequence = (sequence + 1) & 4095; // 用位运算保证sequence不超过12位 if (sequence == 0) { currentTimestamp = nextMillis(lastTimestamp); } } else { sequence = 0L; } lastTimestamp = currentTimestamp; return ((currentTimestamp - EPOCH) << 22) | (workerId << 12) | sequence; } private long nextMillis(long lastTimestamp) { long timestamp = System.currentTimeMillis(); while (timestamp <= lastTimestamp) { timestamp = System.currentTimeMillis(); } return timestamp; } } ``` 上述代码中,使用Snowflake算法生成分布式ID。在构造方法中,传入一个workerId用于区分不同的分布式节点。generateId()方法加入了线程同步,在不同线程调用时可以保证生成ID唯一性。生成ID由三部分组成,即时间戳、worker ID和序列号。使用位运算进行位移和操作,保证ID的长度以及每个部分的取值范围。在运行过程中,如果发现系统的时间比上一次生成ID时的时间回退了,会抛出异常。如果当前时间戳与上一次相等,则增加序列号,否则重置序列号为0。返回的ID可以用于唯一标识分布式系统中的不同实体。 ### 回答3: 分布式ID生成算法是为了解决多个节点之间生成唯一ID问题。一个常见的分布式ID生成算法是Snowflake算法。 Snowflake算法是Twitter提出的一种算法,通过结合时间戳、机器ID和序列号来生成唯一的ID。下面是一个使用Java语言实现的简单示例代码: public class SnowflakeIdGenerator { private final long startTimeStamp = 1566769200000L; // 设置起始时间戳,例如2019-08-26 00:00:00 private final long machineIdBits = 5L; // 机器ID所占位数 private final long maxMachineId = -1L ^ (-1L << machineIdBits); // 最大机器ID private final long sequenceBits = 12L; // 序列号所占位数 private final long machineIdShift = sequenceBits; // 机器ID左移位数 private final long timestampShift = sequenceBits + machineIdBits; // 时间戳左移位数 private final long sequenceMask = -1L ^ (-1L << sequenceBits); // 序列号掩码 private long lastTimeStamp = -1L; private long sequenceId = 0L; private long machineId; public SnowflakeIdGenerator(long machineId) { if (machineId < 0 || machineId > maxMachineId) { throw new IllegalArgumentException("Invalid machineId. It must be between 0 and " + maxMachineId); } this.machineId = machineId; } public synchronized long generateId() { long currentTimeStamp = System.currentTimeMillis(); if (currentTimeStamp < lastTimeStamp) { throw new RuntimeException("Invalid system clock. Current timestamp is less than last timestamp."); } if (currentTimeStamp == lastTimeStamp) { sequenceId = (sequenceId + 1) & sequenceMask; if (sequenceId == 0) { currentTimeStamp = getNextTimeStamp(); } } else { sequenceId = 0L; } lastTimeStamp = currentTimeStamp; return ((currentTimeStamp - startTimeStamp) << timestampShift) | (machineId << machineIdShift) | sequenceId; } private long getNextTimeStamp() { long timeStamp = System.currentTimeMillis(); while (timeStamp <= lastTimeStamp) { timeStamp = System.currentTimeMillis(); } return timeStamp; } } 在使用时,可以创建一个SnowflakeIdGenerator对象,并传入机器ID,然后调用generateId()方法即可生成一个唯一的ID。例如: public class Main { public static void main(String[] args) { SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1); // 创建一个机器ID为1的ID生成器 long id = idGenerator.generateId(); // 生成唯一ID System.out.println(id); } } 这样就可以得到一个分布式环境下唯一的ID了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值