The core of the distributed principle should be regarded as a consistent system design, not a technology
The design of a distributed unique tracking number must have a certain format, and the unique tracking number cannot be realized by pure randomness. The format can solve the distributed problem.
GUID/UUID tool generation is used.
For the current date and time, the first part of the UUID is related to the time. If you generate a UUID after a few seconds after generating a UUID, the first part is different, and the rest are the same.
Clock sequence.
The globally unique IEEE machine identification number. If there is a network card, it is obtained from the MAC address of the network card. If there is no network card, it is obtained by other means. —(The mac address part can solve the distributed problem, but the machine is different because this part is different, so different machines must generate different numbers)
The only drawback of UUID is that the generated result will be longer. The most commonly used standard for UUID is Microsoft’s GUID (Globals Ujique Identifiers
SnowFlake https://blog.csdn.net/li396864285/article/details/54668031
在分布式系统中不同机器产生的id必须不同.Snowflake算法核心把时间戳,工作机器id,序列号(毫秒级时间41位+机器ID 10位+毫秒内序列12位)组合在一起。
Member team> Single number generation component> image2021-1-28_11-56-57.png
Machine id solves the distributed problem
mysql, redis increment
最终由一台机器,一个线程实现的,固定的就是一个特殊隐藏格式"空"
In summary, the conditions for "distributed" strictly unique single number production are 3 points
1 with fixed format machine ID (denoted as "machine ID") + unique ID generated by thread-safe method (denoted as "random number" here) + There are separators for different identification segments (including invisible separators such as: the length of the machine identifier is fixed, then the way after the machine separator is empty is also a fixed separator, machine 1&abc, and machine 1abc, these two effects are the same)
2 There is a unique machine ID, (myqsql, redis can also be said to have IDs, empty IDs, because there is only one machine in the end)
3 The random number generation in the stand-alone generation logo must be thread-safe
Method 1 (strictly unique) The machine generated by the final group of the single number: business machine
Format: yyyyMMssSSS+ redis self-increment (key is MinuteOfHour, expiration time is 1 minute)
Machine ID: Invisible empty (unified redis machine)
Random number identification: redis increment num (key is MinuteOfHour, expiration time is 1 minute)
Disadvantages: redis availability is highly dependent: business services need to be connected to redis
Method 2: (strictly unique) The machine generated by the final group of the single number: business machine
Format: jvm machine number + (yyyyMMssSSS+ thread-safely generated one-second unique number)
Machine ID: jvm machine number
Random number identification: (yyyyMMssSSS+ thread-safe generation of unique numbers for one second)
Disadvantages: each machine has to manually configure the machine number
Method 3: (Strictly unique) The machine where the final group of tracking numbers is generated: unified tracking number generation service
Format: yyyyMMssSSS+ redis self-increment (key is caller number + MinuteOfHour, expiration time is 1 minute)
Machine ID: The machine number is invisible (unified redis machine)
Random number identification: redis increment (key is caller number + MinuteOfHour, expiration time is 1 minute)
Disadvantages: Single number service must be guaranteed to be available
Method 4: (Strictly unique) The final group generating machine for tracking numbers: unified tracking number generation service, or the business itself
Format: arbitrary number identification + replacement of the number by recording a unique id (it can also abstractly say that all data is not repeated in a format)
Machine ID: The machine number is invisible and empty (it can be considered as the warehouse mysql storing the single number library, etc.)
Random number identification: The storage order number library has been removed in advance
Explain: the single-number warehouse must generate a batch of unique numbers after deduplication in advance, and the business Xitong uses the unique id (the sub-table can be expanded) in exchange for the unique number of the warehouse
Disadvantages: to maintain a set of single number warehouse
Method 5: (not strictly unique) The machine generated by the final group of the single number: business machine ----
Format: yyyyMMssSSS+ uid + one English character + 2 random characters (random from 26 English letters + 10 Arabic numbers)
Machine ID: None
Random number identification: yyyyMMssSSS+ uid + one English character + 2 random characters
Disadvantages: the same user's tracking number may be repeated)
The final option 2:
Reasons:
1 The current demand requires strict single number uniqueness (exclusion plan 5)
2 The generation of the tracking number depends on external services, and the stability of the service cannot be guaranteed (excluding 3, 4)
3 The cost of establishing a single number database is too high (exclusion plan 4)
4 At present, the stability of the company's redis service itself is not guaranteed, and every time redis is requested to obtain a tracking number, there is also network overhead (exclusion plan 2)
Option 2 specific to business details
The 3 conditions for a unique order number are as follows: The machine generated by the final group of the order number: business machine
Format: yyyyMMddHHmmss (14) + machine number (8 digits) + random code (4 digits)
Machine identification: access to the unified machine coding service through http when the system is started
Random number identification: the machine locally uses a thread-safe method to obtain a 4-digit unique code (within 1 second)
Functions are more realizing the work done
Provide a machine code management tool to generate unique machine code services for machines,
Provide a springboot-start, and the access party introduces the pom file to complete the access