在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:id-generator开源软件地址:https://gitee.com/simpleweb/id-generator开源软件介绍:id-generator项目介绍基于Twitter的SnowFlake算法实现的分布式ID发号器。支持手动或通过Zookeeper分配workerId。配置简单,操作简易。生成的id具备全局唯一,粗略有序,可反向解码等特性。 数据结构毫秒级
秒级
安装
<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> 配置基于spring boot的项目
@SpringBootApplication@EnableIdGeneratorpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }} 普通的spring项目
<bean id="appProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <array> <value>classpath:application.properties</value> </array> </property></bean>
@Configuration@EnableIdGeneratorpublic class WebConfigure { // other configure } 非spring项目在项目中增加一个单例类工具类, 如 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()); }} 使用在需要的地方注入服务即可使用,如: @Serviceclass DemoService { @Autowired private IdService idService; public void test() { // 通过自动装配提供的服务 long id1 = idService.genId(); // 通过单例的形式提供的服务 long id2 = IdUtil.service().genId(); ... }} API// 生成idlong idService.genId();// 批量生成idlong[] batchGenId(int count);// 解析idId decode(long id);// 手动生成idlong encode(long time, long dataCenterId, long workerId, long seq);// 解析id中的时间戳Date transTime(long time); 参数
|
请发表评论