首先是项目地址:
关于 Redis 集群生成分布式 ID, 这里要先了解 Redis 使用 lua 脚本的时候的 EVAL,EVALSHA 命令:
https://www.runoob.com/redis/scripting-eval.html
讲解一下 Redis 实现分布式 ID 的原理, 这里用 java 语言来讲解:
这里的分布式 id 我们分成 3 部分组成: 毫秒级时间, Redis 集群的第多少个节点, 每一个 Redis 节点在每一毫秒的自增序列值
然后因为 Windows 是 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, 那么你就要引入新的组件, 这也是一个比较麻烦的问题.
原文链接 https://www.itqiankun.com/article/1565227901
其他分布式 ID 系列快捷键:
分布式 ID 系列 (1)-- 为什么需要分布式 ID 以及分布式 ID 的业务需求 https://www.itqiankun.com/article/1565060480
分布式 ID 系列 (2)--UUID 适合做分布式 ID 吗 https://www.itqiankun.com/article/1565060584
分布式 ID 系列 (3)-- 数据库自增 ID 机制适合做分布式 ID 吗 https://www.itqiankun.com/article/1565142723
分布式 ID 系列 (4)--Redis 集群实现的分布式 ID 适合做分布式 ID 吗 https://www.itqiankun.com/article/1565227901
大佬网址
- https://www.itqiankun.com/article/1565227901
- https://tech.meituan.com/2017/04/21/mt-leaf.html
- https://segmentfault.com/a/1190000011282426
- https://www.jianshu.com/p/9d7ebe37215e
来源: https://www.cnblogs.com/itqiankun/p/11319994.html