• Stars
    star
    463
  • Rank 94,661 (Top 2 %)
  • Language
    Java
  • Created over 9 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

distributed id generator based on redis.

redis-id-generator

distributed id generator based on redis.

基于redis的分布式ID生成器。

准备

首先,要知道redis的EVAL,EVALSHA命令:

http://redis.io/commands/eval

http://redis.io/commands/evalsha

原理

利用redis的lua脚本执行功能,在每个节点上通过lua脚本生成唯一ID。 生成的ID是64位的:

  • 使用41 bit来存放时间,精确到毫秒,可以使用41年。
  • 使用12 bit来存放逻辑分片ID,最大分片ID是4095
  • 使用10 bit来存放自增长ID,意味着每个节点,每毫秒最多可以生成1024个ID

比如GTM时间 Fri Mar 13 10:00:00 CST 2015 ,它的距1970年的毫秒数是 1426212000000,假定分片ID是53,自增长序列是4,则生成的ID是:

5981966696448054276 = 1426212000000 << 22 + 53 << 10 + 4

redis提供了TIME命令,可以取得redis服务器上的秒数和微秒数。因些lua脚本返回的是一个四元组。

second, microSecond, partition, seq

客户端要自己处理,生成最终ID。

((second * 1000 + microSecond / 1000) << (12 + 10)) + (shardId << 10) + seq;

集群实现原理

假定集群里有3个节点,则节点1返回的seq是:

0, 3, 6, 9, 12 ...

节点2返回的seq是

1, 4, 7, 10, 13 ...

节点3返回的seq是

2, 5, 8, 11, 14 ...

这样每个节点返回的数据都是唯一的。

注意事项

  • 要求redis server版本是3.2以上,因为使用到了redis.replicate_commands()

参考:http://redis.io/commands/eval

  • 因为是利用了redis的time命令来获取到redis服务器上的时间,所以reids服务器的时间要保证是只增长的,要关闭服务器上的ntp等时间同步机制。

单个节点部署

下载redis-script-node1.lua,并把它load到redis上。

cd redis-directory/
wget https://raw.githubusercontent.com/hengyunabc/redis-id-generator/master/redis-script-node1.lua
./redis-cli script load "$(cat redis-script-node1.lua)"

获取lua脚本的sha1值,可能是:

c5809078fa6d652e0b0232d552a9d06d37fe819c

在代码里,通过EVALSHA命令,传递这个sha1值,就可以得到生成的ID。

比如,通过命令行执行:

./redis-cli EVALSHA c5809078fa6d652e0b0232d552a9d06d37fe819c 2 test 123456789

结果可能是:

1) (integer) 1426238286
2) (integer) 130532
3) (integer) 277
4) (integer) 4

集群部署

假定集群是3个节点,则分别对三个节点执行:

./redis-cli -host node1 -p 6379 script load "$(cat redis-script-node1.lua)"
./redis-cli -host node2 -p 7379 script load "$(cat redis-script-node2.lua)"
./redis-cli -host node3 -p 8379 script load "$(cat redis-script-node3.lua)"

性能

redis默认配置。

单节点,单线程:
time:0:00:00.959
speed:10427.52867570386
单节点,20线程:
time:0:00:06.710
speed:29806.259314456034

结论:

  • 单节点,qps约3w
  • 可以线性扩展,3个结点足以满足绝大部分的应用

java客户端封装

在redis-id-generator-java目录下,有example和benchmark代码。

在调用时,要传入两个参数

  • tag,即为哪一类服务生成ID
  • shardId,即分片由哪个ID生成,比如一个用户的订单,则分片ID应该由userId来生成
public class Example {

	public static void main(String[] args) {
		String tab = "order";
		long userId = 123456789;

		IdGenerator idGenerator = IdGenerator.builder()
				.addHost("127.0.0.1", 6379, "c5809078fa6d652e0b0232d552a9d06d37fe819c")
//				.addHost("127.0.0.1", 7379, "accb7a987d4fb0fd85c57dc5a609529f80ec3722")
//				.addHost("127.0.0.1", 8379, "f55f781ca4a00a133728488e15a554c070b17255")
				.build();

		long id = idGenerator.next(tab, userId);

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

		System.out.println("miliSeconds:" + result.get(0) + ", partition:"
				+ result.get(1) + ", seq:" + result.get(2));
	}
}

多语言客户端

只要支持redis evalsha命令就可以了。

More Repositories

1

xdiamond

全局配置中心
Java
576
star
2

dumpclass

Dump classes from running JVM process.
Java
378
star
3

spring-boot-inside

spring boot技术细节的相关demo
Java
218
star
4

zabbix-api

zabbix-api for java
Java
91
star
5

dubbo-arthas-demo

演示Arthas排查Dubbo问题的Demo
Java
87
star
6

zabbix-sender

java zabbix-sender
Java
75
star
7

zpush

Apple APNs java client.
Java
44
star
8

executable-embeded-tomcat-sample

executable-embeded-tomcat-sample, support traditional web.xml.
Java
43
star
9

dubbo-apache-commons-collections-bug

演示dubbo rpc Apache commons collections 的Java序列化漏洞
Shell
42
star
10

metrics-kafka

Dropwizard Metrics reporter for kafka.
Java
35
star
11

arthas-mybatis-demo

Java
26
star
12

jenkins-ansible-supervisor-deploy

Use jenkins, ansible, supervisor to deploy java application.
Shell
25
star
13

spring-boot-fat-jar-jsp-sample

Java
25
star
14

metrics-zabbix

metrics-zabbix. Report dropwizard/metrics data to zabbix.
Java
24
star
15

xsession

A java web servlet filter for distributed session cached . 分布式Java Web Session缓存。
Java
24
star
16

springboot-classchecker

A tool to find incompatible classes between spring boot 1 and spring boot 2, help upgrade from spring boot 1 to spring boot 2.
Shell
19
star
17

spring-boot-utils

Spring boot 1 and spring boot 2 compatible utils.
Java
17
star
18

maven-repo

My personal maven repository.
12
star
19

spring-config

spring config switch with dev, test, product environment.
Java
11
star
20

kafka-zabbix

Consumer metrics from kafka, then send to zabbix.
Java
10
star
21

mybatis-ehcache-spring

Mybatis ehcache with spring. Mybatis cache metrics.
Java
6
star
22

dubbo-spring-boot-demo

http://start.dubbo.io/
Java
6
star
23

hengyunabc.github.io

HTML
6
star
24

jmh-demo

Java Microbenchmark Demo
Java
5
star
25

lite-jdk

5
star
26

java-telnet-client-demo

A pure java telnet client demo, connect to remote telnet server.
Java
4
star
27

spring-boot-demo

spring boot demo project.
Shell
4
star
28

jpms-tools

Java
4
star
29

telnetd-x

Automatically exported from code.google.com/p/telnetd-x
Java
3
star
30

dubbo-initializr

Java
3
star
31

simple-redis-ratelimiter

A very simple redis ratelimiter.
Java
3
star
32

database-test

测试以db为消息队列的存储的性能
Java
2
star
33

arthas-repackage-deps

Java
2
star
34

netty-grpc-web-proxy

Java
2
star
35

spring-boot-prometheus-grafana-demo

Spring Boot应用集成k8s prometheus grafana demo
Java
2
star
36

endpoints-spring-boot-starter

Show all spring boot endpoints mappings url.
Java
2
star
37

skywalking-error-demo

Java
1
star
38

java9-guide

Java
1
star
39

classpath-demo

演示ClassaPath相关知识的demo
Java
1
star
40

katacoda-scenarios

Katacoda Scenarios
Shell
1
star
41

java-syntax-highlighter

Automatically exported from code.google.com/p/java-syntax-highlighter
Java
1
star
42

xqwizard-code

Visual Basic
1
star