- AUTO 数据库ID自增
- INPUT 用户输入ID
- ID_WORKER 全局唯一ID,Long类型的主键
- ID_WORKER_STR 字符串全局唯一ID
- UUID 全局唯一ID,UUID类型的主键
- NONE 该类型为未设置主键类型
主键生成的几种方式
1. 数据库自增长序列或字段
2. UUID
3. Redis生成ID
当使用数据库来生成ID性能不够要求的时候,我们可以尝试使用Redis来生成ID。这主要依赖于Redis是单线程的,所以也可以用生成全局唯一的ID。可以用Redis的原子操作 INCR和INCRBY来实现。
可以使用Redis集群来获取更高的吞吐量。假如一个集群中有5台Redis。可以初始化每台Redis的值分别是1,2,3,4,5,然后步长都是5。各个Redis生成的ID为:
A:1,6,11,16,21
B:2,7,12,17,22
C:3,8,13,18,23
D:4,9,14,19,24
E:5,10,15,20,25