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
DROP TABLE IF EXISTS WORKER_NODE;
CREATE TABLE WORKER_NODE
(
ID BIGINT NOT NULL AUTO_INCREMENT COMMENT 'auto increment id',
HOST_NAME VARCHAR(64) NOT NULL COMMENT 'host name',
PORT VARCHAR(64) NOT NULL COMMENT 'port',
TYPE INT NOT NULL COMMENT 'node type: ACTUAL or CONTAINER',
LAUNCH_DATE DATE NOT NULL COMMENT 'launch date',
MODIFIED TIMESTAMP NOT NULL COMMENT 'modified time',
CREATED TIMESTAMP NOT NULL COMMENT 'created time',
PRIMARY KEY(ID)
)
 COMMENT='DB WorkerID Assigner for UID Generator',ENGINE = INNODB;
Copy the code

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

  • Add dependencies to the POM
< the dependency > < groupId > cn. Codesheep < / groupId > < artifactId > id - spring - the boot - starter < / artifactId > < version > 1.0.0 < / version > </dependency>Copy the code
  • Configuring database Connections
url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/demo? useUnicode=true&characterEncoding=utf8&autoReconnect=true&useOldAliasMetadataBehavior=true&connectionCollation=utf8mb4_unicode_ci&rewriteBatchedStatements=true&allowMultiQueries=true
username: xxxxxx
password: xxxxxx
Copy the code

Again, the UidGenerator requires database support

  • Modify the Spring Boot main class

Mybatis = Spring Boot;

@MapperScan({"com.baidu.fsg.uid.worker.dao"})
Copy the code
  • Code usage
@RestController
public class TestController {

  @Autowired
  private UidGenService uidGenService;

  @GetMapping("/uid")
  public String genUid() {
    return String.valueOf("The unique ID number generated this time is:"+uidGenService.getUid()); }}Copy the code

How about, super easy to use:

  1. First of all useAutowiredThe introduction ofUidGenServiceClass;
  2. Direct callUidGenServiceOf the classgetUid()Method to obtain oneLongType ofIDNo.
  • Running effect

Demo source code here, please help yourself:

  • Github.com/hansonwang9…

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.

  • Github.com/hansonwang9…

A few notes:

  • Due to theUidGeneratorDatabase support is required, so data tables must be derived and configured before useMapperScan
  • It needs to be highly customizedUidGeneratorComponent details can be modifiedid-spring-boot-starterThe inside of thecached-uid-spring.xmlFile, and then type it againjarpackage
  • Due to theIDNumber generation is generally a basic service of the system, so it can be an independent microservice for other microservices to call

Write in the last

Due to the limited ability, if there is a mistake or improper place, please also criticize and correct, study together!

  • My Personal Blog: CodeSheep program sheep