An overview of the

ID number generators (or globally unique ID generators) are the infrastructure of server-side systems, and ID numbers are something that most backend developers come into contact with every day. One of the best algorithms for ID generation is the Snowflake algorithm.

UidGenerator is a high performance unique ID generator based on Snowflake algorithm. The UidGenerator has been used in detail in the previous article, but the process of using it is still quite complicated, and I also need to cite the source code of UidGenerator components, which feels a little inconvenient. Therefore, this paper based on the UidGenerator, and then to package a set of more conducive to the use of the Spring Boot project ID number generation component, named id-spring-boot-starter, a look at the name knows that it is out of the box.

usage

Importing SQL Scripts

This is definitely not a step to skip, as UidGenerator requires database support

Configuring database Connections

url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/demo? useUnicode=truecharacterEncoding=utf8autoReconnect=trueuseOldAliasMetadataBehavior=trueconnectionCollation=utf8mb4_unico de_cirewriteBatchedStatements=trueallowMultiQueries=true

username: xxxxxx

password: xxxxxx

Again, the UidGenerator requires database support

Modify the Spring Boot main class

Mybatis = Spring Boot;

@MapperScan({com.baidu.fsg.uid.worker.dao})

Code usage

How about, super easy to use:

Firstly, the UidGenService class is introduced in the way of Autowired.

An ID of type Long is obtained by calling the getUid() method of the UidGenService class

Running effect

  


Demo source code here, please help yourself:

https://github.com/hansonwang99/Spring-Boot-In-Action/tree/master/test-id-spring-boot-starter

Id-spring-boot-starter source code download

If you want to customize the id-spring-boot-starter source code, you can download the source code on Github.

https://github.com/hansonwang99/Spring-Boot-In-Action/tree/master/id-spring-boot-starter

A few notes:

Since UidGenerator requires database support, it is important to derive data tables and configure MapperScan before using it