代码拉取完成,页面将自动刷新
基于Twitter的SnowFlake算法实现的分布式ID发号器。支持手动或通过Zookeeper分配workerId。配置简单,操作简易。生成的id具备全局唯一,粗略有序,可反向解码等特性。
时间位 | 数据中心 | 工作机器 | 序号位 | |
---|---|---|---|---|
位数 | 41 | 5 | 5 | 12 |
时间位 | 数据中心 | 工作机器 | 序号位 | |
---|---|---|---|---|
位数 | 31 | 5 | 5 | 22 |
<repositories>
<repository>
<id>iwanttomakemoney_admin</id>
<url>https://gitee.com/iwanttomakemoney_admin/maven/raw/master/repository</url>
</repository>
</repositories>
引入以下依赖
<dependencies>
<dependency>
<groupId>com.lxm</groupId>
<artifactId>id-generator-interface</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.lxm</groupId>
<artifactId>id-generator-core</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
@EnableIdGenerator
注解即可. 如@SpringBootApplication
@EnableIdGenerator
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
<bean id="appProperty"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:application.properties</value>
</array>
</property>
</bean>
@EnableIdGenerator
注解即可.如@Configuration
@EnableIdGenerator
public class WebConfigure {
// other configure
}
在项目中增加一个单例类工具类, 如
public class IdUtil {
private static IdUtil _instance = new IdUtil();
private static IdService idService;
public static IdService service() {
return idService;
}
private IdUtil() {
// 使用提供的工厂类生成idService
idService = IdServiceFactoryBean.idService(new AutoConfiguration());
}
}
在需要的地方注入服务即可使用,如:
@Service
class DemoService {
@Autowired
private IdService idService;
public void test() {
// 通过自动装配提供的服务
long id1 = idService.genId();
// 通过单例的形式提供的服务
long id2 = IdUtil.service().genId();
...
}
}
// 生成id
long idService.genId();
// 批量生成id
long[] batchGenId(int count);
// 解析id
Id decode(long id);
// 手动生成id
long encode(long time, long dataCenterId, long workerId, long seq);
// 解析id中的时间戳
Date transTime(long time);
属性 | 类型 | 缺省值 | 描述 |
---|---|---|---|
id.zookeeper.enable | Boolean | false | 是否启用Zookeeper分配workerId。 默认为false表示使用手动分配workerId;若为true则需预先准备至少一个的Zookeeper服务 |
id.zookeeper.serverLists | String | null | 连接Zookeeper服务器的列表 包括IP地址和端口号 多个地址用逗号分隔如:host1:2181,host2:2181 |
id.zookeeper.digest | String | null | 连接Zookeeper的权限令牌 缺省为不需要权限验证 |
id.zookeeper.namespace | String | "id-generator" | Zookeeper的命名空间 |
id.zookeeper.baseSleepTime | Integer | 1000 | 等待重试的间隔时间的初始值 单位:毫秒 |
id.zookeeper.maxSleepTime | Integer | 3000 | 等待重试的间隔时间的最大值 单位:毫秒 |
id.zookeeper.maxRetries | Integer | 3 | 最大重试次数 |
id.zookeeper.sessionTimeout | Integer | 60000 | 会话超时时间 单位:毫秒 |
id.zookeeper.connectionTimeout | Integer | 15000 | 连接超时时间 单位:毫秒 |
id.type.second=false | Boolean | false | true-秒级别 false-毫秒级别 |
id.workerId | Integer | 0 | 手动指定工作机器号,当id.zookeeper.enable=false有效 |
id.datacenterId | Integer | -1 | 手动指定数据中心, 若不指定则将根据mac地址自动分配一个固定的编号 |
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型