Skip to content

fuqqqq/snowflake4j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

语言: 中文 | English

Snowflake4J

GitHub Release License

snowflake4j是一个Java库,用于获取推特雪花ID。

参考:

引入

  • Maven
<dependency>
    <groupId>com.fuqqqq</groupId>
    <artifactId>snowflake4j</artifactId>
    <version>1.1.0</version>
</dependency>
  • Gradle
compile 'com.fuqqqq:snowflake4j:1.1.0'

用法

package com.example.demo.snowflake4j;

import com.fuqqqq.snowflake4j.*;

public class SimpleExample {

    public long test() {
        long epoch = 1643738522222L; // 2022-02-22 22:22:22.222
        long workerId = 0L;          // 0-31
        long datacenterId = 0L;      // 0-31
        // 建构实例
        IdWorker idWorker = new IdWorkerBuilder()
                .setEpoch(epoch)
                .setDataCenterId(datacenterId)
                .setWorkerId(workerId)
                .build();

        return idWorker.nextId();
    }
}

备注:同一系统中的纪元时间戳epoch必须保持一致。

许可证

根据 Apache 2.0 License 发布。