1. 程式人生 > >分散式ID系列(4)——Redis叢集實現的分散式ID適合做分散式ID嗎

分散式ID系列(4)——Redis叢集實現的分散式ID適合做分散式ID嗎

首先是專案地址:

https://github.com/maqiankun/distributed-id-redis-generator

關於Redis叢集生成分散式ID,這裡要先了解redis使用lua指令碼的時候的EVAL,EVALSHA命令:

https://www.runoob.com/redis/scripting-eval.html
https://www.runoob.com/redis/scripting-evalsha.html

講解一下Redis實現分散式ID的原理,這裡用java語言來講解:

這裡的分散式id我們分成3部分組成:毫秒級時間,redis叢集的第多少個節點,每一個redis節點在每一毫秒的自增序列值

然後因為window是64位的,然後整數的時候第一位必須是0,所以最大的數值就是63位的111111111111111111111111111111111111111111111111111111111111111,這裡呢,我們分出來41位作為毫秒,然後12位作為redis節點的數量,然後10位做成redis節點在每一毫秒的自增序列值

41位的二進位制11111111111111111111111111111111111111111轉換成10進位制的毫秒就是2199023255551,然後我們把 2199023255551轉換成時間就是2039-09-07,也就是說可以用20年的
然後12位作為redis節點,所以最多就是12位的111111111111,也就是最多可以支援4095個redis節點,
然後10位的redis每一個節點自增序列值,,這裡最多就是10位的1111111111,也就是說每一個redis節點可以每一毫秒可以最多生成1023個不重複id值

然後我們使用java程式碼來講解這個原理,下面的1565165536640L是一個毫秒值,然後我們的的redis節點設定成53,然後我們設定了兩個不同的自增序列值,分別是1和1023,下面的結果展示的就是在1565165536640L這一毫秒裡面,53號redis節點生成了兩個不同的分散式id值

package io.github.hengyunabc.redis;

import java.text.SimpleDateFormat;
import java.util.Date;


public class Test {

    public static void main(String[] args) {
        long buildId = buildId(1565165536640L, 53, 1);
        System.out.println("分散式id是:"+buildId);
        long buildIdLast = buildId(1565165536640L, 53, 1023);
        System.out.println("分散式id是:"+buildIdLast);
    }
    
    public static long buildId(long miliSecond, long shardId, long seq) {
        return (miliSecond << (12 + 10)) + (shardId << 10) + seq;
    }


}
public class Test {

    public static void main(String[] args) {
        long buildId = buildId(1565165536640L, 53, 1);
        System.out.println("分散式id是:"+buildId);
        long buildIdLast = buildId(1565165536640L, 53, 1023);
        System.out.println("分散式id是:"+buildIdLast);
    }
    
    public static long buildId(long miliSecond, long shardId, long seq) {
        return (miliSecond << (12 + 10)) + (shardId << 10) + seq;
    }


}

結果如下所示

分散式id是:6564780070991352833
分散式id是:6564780070991353855

那麼有人要說了,你這也不符合分散式id的設定啊,完全沒有可讀性啊,這裡我們可以使用下面的方式來獲取這個分散式id的生成毫秒時間值,

package io.github.hengyunabc.redis;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

    public static void main(String[] args) {
        long buildId = buildId(1565165536640L, 53, 1);
        parseId(buildId);
        long buildIdLast = buildId(1565165536640L, 53, 1023);
        parseId(buildIdLast);
    }
    
    public static long buildId(long miliSecond, long shardId, long seq) {
        return (miliSecond << (12 + 10)) + (shardId << 10) + seq;
    }

    public static void parseId(long id) {
        long miliSecond = id >>> 22;
        long shardId = (id & (0xFFF << 10)) >> 10;
        System.err.println("分散式id-"+id+"生成的時間是:"+new SimpleDateFormat("yyyy-MM-dd").format(new Date(miliSecond)));
        System.err.println("分散式id-"+id+"在第"+shardId+"號redis節點生成");
    }

}

這樣不就ok了,哈哈。

分散式id-6564780070991352833生成的時間是:2019-08-07
分散式id-6564780070991352833在第53號redis節點生成
分散式id-6564780070991353855生成的時間是:2019-08-07
分散式id-6564780070991353855在第53號redis節點生成

實現叢集版的redis的分散式id建立

此時我的分散式redis叢集的埠分別是6380,6381
首先是生成Evalsha命令安全sha1 校驗碼,生成過程如下,
首先是生成6380埠對應的安全sha1 校驗碼,首先進入到redis的bin目錄裡面,然後執行下面的命令下載lua指令碼

wget https://github.com/maqiankun/distributed-id-redis-generator/blob/master/redis-script-node1.lua

然後執行下面的命令,生成6380埠對應的安全sha1 校驗碼,此時看到是be6d4e21e9113bf8af47ce72f3da18e00580d402

./redis-cli -p 6380 script load "$(cat redis-script-node1.lua)"

首先是生成6381埠對應的安全sha1 校驗碼,首先進入到redis的bin目錄裡面,然後執行下面的命令下載lua指令碼

wget https://github.com/maqiankun/distributed-id-redis-generator/blob/master/redis-script-node2.lua


然後執行下面的命令,生成6381埠對應的安全sha1 校驗碼,此時看到是97f65601d0aaf1a0574da69b1ff3092969c4310e

./redis-cli -p 6381 script load "$(cat redis-script-node2.lua)"

然後我們就使用上面的sha1 校驗碼和下面的程式碼來生成分散式id

專案圖片如下

IdGenerator類的程式碼如下所示


package io.github.hengyunabc.redis;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.tuple.Pair;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

public class IdGenerator {
    /**
     * JedisPool, luaSha
     */
    List<Pair<JedisPool, String>> jedisPoolList;
    int retryTimes;

    int index = 0;

    private IdGenerator(List<Pair<JedisPool, String>> jedisPoolList,
            int retryTimes) {
        this.jedisPoolList = jedisPoolList;
        this.retryTimes = retryTimes;
    }

    static public IdGeneratorBuilder builder() {
        return new IdGeneratorBuilder();
    }

    static class IdGeneratorBuilder {
        List<Pair<JedisPool, String>> jedisPoolList = new ArrayList();
        int retryTimes = 5;

        public IdGeneratorBuilder addHost(String host, int port, String luaSha) {
            jedisPoolList.add(Pair.of(new JedisPool(host, port), luaSha));
            return this;
        }

        public IdGenerator build() {
            return new IdGenerator(jedisPoolList, retryTimes);
        }
    }

    public long next(String tab) {
        for (int i = 0; i < retryTimes; ++i) {
            Long id = innerNext(tab);
            if (id != null) {
                return id;
            }
        }
        throw new RuntimeException("Can not generate id!");
    }

    Long innerNext(String tab) {
        index++;
        int i = index % jedisPoolList.size();
        Pair<JedisPool, String> pair = jedisPoolList.get(i);
        JedisPool jedisPool = pair.getLeft();

        String luaSha = pair.getRight();
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            List<Long> result = (List<Long>) jedis.evalsha(luaSha, 2, tab, ""
                    + i);
            long id = buildId(result.get(0), result.get(1), result.get(2),
                    result.get(3));
            return id;
        } catch (JedisConnectionException e) {
            if (jedis != null) {
                jedisPool.returnBrokenResource(jedis);
            }
        } finally {
            if (jedis != null) {
                jedisPool.returnResource(jedis);
            }
        }
        return null;
    }

    public static long buildId(long second, long microSecond, long shardId,
            long seq) {
        long miliSecond = (second * 1000 + microSecond / 1000);
        return (miliSecond << (12 + 10)) + (shardId << 10) + seq;
    }

    public static List<Long> parseId(long id) {
        long miliSecond = id >>> 22;
        long shardId = (id & (0xFFF << 10)) >> 10;

        List<Long> re = new ArrayList<Long>(4);
        re.add(miliSecond);
        re.add(shardId);
        return re;
    }
}

Example的程式碼如下所示,下面的while迴圈的目的就是為了列印多個分散式id,下面的tab變數就是evalsha命令裡面的引數,可以根據自己的需求來定義

package io.github.hengyunabc.redis;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Example {

    public static void main(String[] args) {
        String tab = "這個就是evalsha命令裡面的引數,隨便定義";

        IdGenerator idGenerator = IdGenerator.builder()
                .addHost("47.91.248.236", 6380, "be6d4e21e9113bf8af47ce72f3da18e00580d402")
                .addHost("47.91.248.236", 6381, "97f65601d0aaf1a0574da69b1ff3092969c4310e")
                .build();
        int hello = 0;
        while (hello<3){
            long id = idGenerator.next(tab);

            System.out.println("分散式id值:" + id);
            List<Long> result = IdGenerator.parseId(id);

            System.out.println("分散式id生成的時間是:" + new SimpleDateFormat("yyyy-MM-dd").format(new Date(result.get(0))) );
            System.out.println("redis節點:" + result.get(1));
            hello++;
        }

    }
}

此時列印結果如下所示

分散式id值:6564819854640022531
分散式id生成的時間是:2019-08-07
redis節點:1
分散式id值:6564819855189475330
分散式id生成的時間是:2019-08-07
redis節點:0
分散式id值:6564819855361442819
分散式id生成的時間是:2019-08-07
redis節點:1

到這裡redis叢集版的分散式id就算搞定了,完美؏؏☝ᖗ乛◡乛ᖘ☝؏؏

Redis叢集實現的分散式id是否適合做分散式id呢?

我覺得Redis叢集實現分散式ID是可以供我們開發中的基本使用的,但是我還是覺得它有下面的兩個問題:

1:這裡我們可以給上一篇的資料庫自增ID機制進行對比,其實Redis叢集可以說是解決了資料庫叢集建立分散式ID的效能問題,但是Redis集群系統水平擴充套件還是比較困難,如果以後想對Redis叢集增加Redis節點的話,還是會和資料庫叢集的節點擴充套件一樣麻煩。
2:還有就是如果你的專案裡面沒有使用Redis,那麼你就要引入新的元件,這也是一個比較麻煩的問題。

原文連結

其他分散式ID系列快捷鍵:
分散式ID系列(1)——為什麼需要分散式ID以及分散式ID的業務需求
分散式ID系列(2)——UUID適合做分散式ID嗎
分散式ID系列(3)——資料庫自增ID機制適合做分散式ID嗎
分散式ID系列(4)——Redis叢集實現的分散式ID適合做分散式ID嗎

大佬網址
https://www.itqiankun.com/article/1565227901
https://blog.csdn.net/hengyunabc/article/details/44244951
https://tech.meituan.com/2017/04/21/mt-leaf.html
https://segmentfault.com/a/1190000011282426
https://www.jianshu.com/p/9d7ebe372