-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Lin Tang edited this page Nov 10, 2020
·
7 revisions
SnowFlake is a maven-based plugin for spring application, which implements the snowflake algorithm proposed by Twitter. It has been proven that snowflake algorithm can generate global unique id in a application. Author, in other word, I try to write this plugin in spring-boot-starter manner, following the tradition convention over configuration.
Here are all parametes of this plugin.
| parameter name | illustration | default |
|---|---|---|
| initial-timestamp | initial timestamp, usually the timestamp when your application is created or deployed | 1604383611644 |
| worker-id-bits | number of bits for worker | 5 |
| data-center-id-bits | number of bits for data center | 5 |
| worker-id | current worker id | 1 |
| data-center-id | current data center id | 1 |
Caution:
- the value of worker-id-bits plus the value of data-center-id-bits equals 10.
- worker-id should not exceed the maximum value limited by worker-id-bits. For example, if the worker-id-bits is 5, then the maximum value of worker-id is 31. So the same is the data-center-id.
- As you can see, all parameters have default values, so this plugin can be used without any configuration for parameters.
Version of SnowFlake-spring-boot-starter:1.0.0 is published on Nov 8, 2020. This is the first version and this plugin seems so simple...And it will be updated in the future, hopefully.
- You should import this starter in the pom.xml in your application like
<dependency>
<groupId>com.github.zon-g</groupId>
<artifactId>SnowFlake-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
- If you want to have some configurations for your own, just configure this starter as you wish in application.yml/application.yaml/application.properties; otherwise, you can skip step 2.
- In anywhere you wanna use this plugin, just do as
……
@Autowired
private Generator generator; // the only way to invoke the snowflake algorithm
……
long id = generator.nextId();
……