首頁 > 軟體

MyBatis使用雪花ID的實現

2022-04-07 13:00:03

一、實現MyBatis ID構建介面

@Slf4j
@Component
public class CustomIdGenerator implements IdentifierGenerator {

    @Override
    public Long nextId(Object entity) {
        //生成ID
        long id = SnowFlakeUtils.nextId();
        log.info("生成ID: " + id);
        return id;
    }
}

二、雪花ID生成工具類

@Slf4j
public class SnowFlakeUtils {

    /** 初始偏移時間戳 */
    private static final long OFFSET = 1546300800L;

    /** 機器id (0~15 保留 16~31作為備份機器) */
    private static final long WORKER_ID;
    /** 機器id所佔位數 (5bit, 支援最大機器數 2^5 = 32)*/
    private static final long WORKER_ID_BITS = 5L;
    /** 自增序列所佔位數 (16bit, 支援最大每秒生成 2^16 = 65536) */
    private static final long SEQUENCE_ID_BITS = 16L;
    /** 機器id偏移位數 */
    private static final long WORKER_SHIFT_BITS = SEQUENCE_ID_BITS;
    /** 自增序列偏移位數 */
    private static final long OFFSET_SHIFT_BITS = SEQUENCE_ID_BITS + WORKER_ID_BITS;
    /** 機器標識最大值 (2^5 / 2 - 1 = 15) */
    private static final long WORKER_ID_MAX = ((1 << WORKER_ID_BITS) - 1) >> 1;
    /** 備份機器ID開始位置 (2^5 / 2 = 16) */
    private static final long BACK_WORKER_ID_BEGIN = (1 << WORKER_ID_BITS) >> 1;
    /** 自增序列最大值 (2^16 - 1 = 65535) */
    private static final long SEQUENCE_MAX = (1 << SEQUENCE_ID_BITS) - 1;
    /** 發生時間回撥時容忍的最大回撥時間 (秒) */
    private static final long BACK_TIME_MAX = 1000L;

    /** 上次生成ID的時間戳 (秒) */
    private static long lastTimestamp = 0L;
    /** 當前秒內序列 (2^16)*/
    private static long sequence = 0L;
    /** 備份機器上次生成ID的時間戳 (秒) */
    private static long lastTimestampBak = 0L;
    /** 備份機器當前秒內序列 (2^16)*/
    private static long sequenceBak = 0L;

    static {
        // 初始化機器ID
        long workerId = getWorkId();
        if (workerId < 0 || workerId > WORKER_ID_MAX) {
            throw new IllegalArgumentException(String.format("cmallshop.workerId範圍: 0 ~ %d 目前: %d", WORKER_ID_MAX, workerId));
        }
        WORKER_ID = workerId;
    }

    private static Long getWorkId(){
        try {
            String hostAddress = Inet4Address.getLocalHost().getHostAddress();
            int[] ints = StringUtils.toCodePoints(hostAddress);
            int sums = 0;
            for(int b : ints){
                sums += b;
            }
            return (long)(sums % WORKER_ID_MAX);
        } catch (UnknownHostException e) {
            // 如果獲取失敗,則使用亂數備用
            return RandomUtils.nextLong(0,WORKER_ID_MAX-1);
        }
    }


    /** 私有建構函式禁止外部存取 */
    private SnowFlakeUtils() {}

    /**
     * 獲取自增序列
     * @return long
     */
    public static long nextId() {
        return nextId(SystemClock.now() / 1000);
    }

    /**
     * 主機器自增序列
     * @param timestamp 當前Unix時間戳
     * @return long
     */
    private static synchronized long nextId(long timestamp) {
        // 時鐘回撥檢查
        if (timestamp < lastTimestamp) {
            // 發生時鐘回撥
            log.warn("時鐘回撥, 啟用備份機器ID: now: [{}] last: [{}]", timestamp, lastTimestamp);
            return nextIdBackup(timestamp);
        }

        // 開始下一秒
        if (timestamp != lastTimestamp) {
            lastTimestamp = timestamp;
            sequence = 0L;
        }
        if (0L == (++sequence & SEQUENCE_MAX)) {
            // 秒內序列用盡
//            log.warn("秒內[{}]序列用盡, 啟用備份機器ID序列", timestamp);
            sequence--;
            return nextIdBackup(timestamp);
        }

        return ((timestamp - OFFSET) << OFFSET_SHIFT_BITS) | (WORKER_ID << WORKER_SHIFT_BITS) | sequence;
    }

    /**
     * 備份機器自增序列
     * @param timestamp timestamp 當前Unix時間戳
     * @return long
     */
    private static long nextIdBackup(long timestamp) {
        if (timestamp < lastTimestampBak) {
            if (lastTimestampBak - SystemClock.now() / 1000 <= BACK_TIME_MAX) {
                timestamp = lastTimestampBak;
            } else {
                throw new RuntimeException(String.format("時鐘回撥: now: [%d] last: [%d]", timestamp, lastTimestampBak));
            }
        }

        if (timestamp != lastTimestampBak) {
            lastTimestampBak = timestamp;
            sequenceBak = 0L;
        }

        if (0L == (++sequenceBak & SEQUENCE_MAX)) {
            // 秒內序列用盡
//            logger.warn("秒內[{}]序列用盡, 備份機器ID借取下一秒序列", timestamp);
            return nextIdBackup(timestamp + 1);
        }

        return ((timestamp - OFFSET) << OFFSET_SHIFT_BITS) | ((WORKER_ID ^ BACK_WORKER_ID_BEGIN) << WORKER_SHIFT_BITS) | sequenceBak;
    }


    /**
     * 並行數
     */
    private static final int THREAD_NUM = 30000;
    private static volatile CountDownLatch countDownLatch = new CountDownLatch(THREAD_NUM);

    public static void main(String[] args) {
        ConcurrentHashMap<Long,Long> map = new ConcurrentHashMap<>(THREAD_NUM);
        List<Long> list = Collections.synchronizedList(new LinkedList<>());

        for (int i = 0; i < THREAD_NUM; i++) {
            Thread thread = new Thread(() -> {
                // 所有的執行緒在這裡等待
                try {
                    countDownLatch.await();

                    Long id = SnowFlakeUtils.nextId();
                    list.add(id);
                    map.put(id,1L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });

            thread.start();
            // 啟動後,倒計時計數器減一,代表有一個執行緒準備就緒了
            countDownLatch.countDown();
        }

        try{
            Thread.sleep(50000);
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println("listSize:"+list.size());
        System.out.println("mapSize:"+map.size());
        System.out.println(map.size() == THREAD_NUM);
    }
}

到此這篇關於MyBatis使用雪花ID的實現的文章就介紹到這了,更多相關MyBatis 雪花ID內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com