Skip to content
Lin Tang edited this page Dec 20, 2020 · 7 revisions

Description

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.

In SnowFlake, there are three kinds of implementations of interface Generator, like DefaultGenerator, CachedGenerator and AtomicGenerator.

  • DefaultGenerator is implemented by using explicit Lock like StampLock, a basic supported one.
  • CachedGenerator adopts ConcurrentLinkedQueue as the cache container to maintain identities generated in batches.
  • AtomicGenerator is the most efficient implementation as this kind of generator adpots AtomicLong to generate a global unique identity in version V2.0.0. In other words, this is a Lock Free implementation of Generator. Neverthless, in version V2.0.1, AtomicStampedReference is used to generate a unique identity.

Parameters

Here are all parametes of this plugin.

parameter name illustration default
snowflake.initial-timestamp initial timestamp, usually the timestamp when your application is created or deployed 1604383611644
snowflake.worker-id-bits number of bits for worker 5
snowflake.data-center-id-bits number of bits for data center 5
snowflake.worker-id current worker id 1
snowflake.data-center-id current data center id 1
snowflake.pool-size pool size for CachedGenerator 10
snowflake.enable-cached enable CachedGenerator false
snowflake.enable-atomic enable AtomicGenerator false

Caution:

  1. the value of worker-id-bits plus the value of data-center-id-bits equals 10.
  2. 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.
  3. As you can see, all parameters have default values, so this plugin can be used without any configuration for parameters.

Version

V2.0.1

Version of SnowFlake-spring-boot-starter:2.0.1 change the implementation of AtomicGenerator from AtomicLong to AtomicStampedReference.

V2.0.0

Version of SnowFlake-spring-boot-starter:2.0.0 add CachedGenerator and AtomicGenerator.

V1.0.1

Version of SnowFlake-spring-boot-starter:1.0.1 add spring-boot-configuration-processor to enable IDE prompt configuration informations.

V1.0.0

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.

Instruction

  1. 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>2.0.1</version>
</dependency>
  1. 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.
  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();
……

Clone this wiki locally