introduction
In our life, ID is actually inseparable from our life.
ID card number, QQ, mobile phone number, bank card number, student ID number, even the number lying on your hard disk.
These ID marks are so important that every background programmer has to think about this problem? What are the advantages and disadvantages?
This article takes you to learn several common ID generation strategies in simple terms.
Introductory learning
Due to space priority, it is recommended to read the following article.
Distributed ID generation requirements
UUID strategy explanation
Random generation strategy
Explanation of snowflake algorithm
Open source tools
ID is a common ID implementation strategy for Java design.
So that you can use it out of the box in daily development and enjoy the happiness of leaving work early~
Creative intention
For ID generation, all background systems must face the problem, distributed ID generation is also a very common requirement.
Recently, the code written by my colleagues, in multiple machines and issued a serial number conflict.
I think Id strategies should be aggregated into a toolkit instead of building wheels every time. Sometimes there are problems.
characteristic
- Minimalist API, a line of code to do everything
- Built in a variety of ID generation strategies, there is always one for you
- The jar package is only 13K
Quick start
Maven introduction
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>id</artifactId>
<version>0.0.2</version>
</dependency>
Introductory examples
Test code
final String snowflake = IdHelper.snowflake();
System.out.println(snowflake);
- result
1260199310461505537
Built in method
All IDS can be accessed through theIdHelper
Call directly.
Serial number | method | example |
---|---|---|
1 | uuid8() | dUlmJiEq |
2 | uuid32() | 3f9afd5773d143fb8e28d80de47f3d1a |
3 | random() | 202005122127270743937635635 |
4 | random(prefix, length) |
random("999", 5) It is 9992020051221340961522263 |
5 | snowflake() | 1260199914969849858 |
6 | local() | Starting from 0, gradually add 1 to return the ID |
Road-Map
- [] snowflake customization + code optimization
- [] introduce comb algorithm
- ] compatibility of other open source IDS
- [] ID generation based on common databases
- [] ID generation based on redis