基于zookeeper
和snowflake
算法的分布式id生成器
- 添加pom依赖
<dependency> <groupId>cn.waynechu</groupId> <artifactId>biz-boot-starter-sequence</artifactId> </dependency>
- 添加配置
sequence.enable=true sequence.registryFile=./tmp/sequence sequence.zookeeper.serverLists=localhost:2181,localhost:2182,localhost:2183 sequence.zookeeper.namespace=sequence
- 注入
IdGenerator
对象@Autowire private IdGenerator idGenerator;
- 生成分布式id
long id = idGenerator.nextId(); long[] ids = idGenerator.nextIds(1000); String idStr = idGenerator.nextStringId(); String[] idsStr = idGenerator.nextStringIds(1000); ...
- 基于UUID
- Redis分布式自增ID
- 百度UID-Generator,支持雪花ID模式。https://github.com/baidu/uid-generator
- 美团Leaf分布式ID生成器,支持号段模式和雪花ID模式。https://github.com/Meituan-Dianping/Leaf