CN112235431A - Method and system for automatically configuring machine ID based on snowflake algorithm - Google Patents
Method and system for automatically configuring machine ID based on snowflake algorithm Download PDFInfo
- Publication number
- CN112235431A CN112235431A CN202011065845.0A CN202011065845A CN112235431A CN 112235431 A CN112235431 A CN 112235431A CN 202011065845 A CN202011065845 A CN 202011065845A CN 112235431 A CN112235431 A CN 112235431A
- Authority
- CN
- China
- Prior art keywords
- bit
- machine
- data center
- bits
- bit data
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 241000533950 Leucojum Species 0.000 title claims abstract description 42
- 238000000034 method Methods 0.000 title claims abstract description 20
- 238000010586 diagram Methods 0.000 description 3
- 238000006467 substitution reaction Methods 0.000 description 2
- 230000002159 abnormal effect Effects 0.000 description 1
- 230000009286 beneficial effect Effects 0.000 description 1
- 230000007547 defect Effects 0.000 description 1
- 238000004519 manufacturing process Methods 0.000 description 1
- 230000003287 optical effect Effects 0.000 description 1
Images
Classifications
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L61/00—Network arrangements, protocols or services for addressing or naming
- H04L61/30—Managing network names, e.g. use of aliases or nicknames
- H04L61/3015—Name registration, generation or assignment
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L61/00—Network arrangements, protocols or services for addressing or naming
- H04L61/30—Managing network names, e.g. use of aliases or nicknames
- H04L61/3005—Mechanisms for avoiding name conflicts
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L2101/00—Indexing scheme associated with group H04L61/00
- H04L2101/30—Types of network names
Landscapes
- Engineering & Computer Science (AREA)
- Computer Networks & Wireless Communication (AREA)
- Signal Processing (AREA)
- Data Exchanges In Wide-Area Networks (AREA)
Abstract
The embodiment of the invention provides a method for automatically configuring a machine ID based on a snowflake algorithm, which comprises the following steps: the method comprises the following steps: splitting 10-bit work machine id into 2-bit data center bits and 8-bit machine id bits based on a snowflake algorithm; step two: filling 2-bit data center bits by acquiring 2-bit data center IDs declared in the configuration file; step three: filling the 8-bit machine id bit with the obtained IPV4 address; the embodiment of the invention realizes automatic configuration of the machine ID, reduces the repeated risk of the main key to a certain extent and reduces the configuration amount.
Description
Technical Field
The invention relates to the technical field of computers, in particular to a method and a system for automatically configuring a machine ID based on a snowflake algorithm.
Background
The existing unique primary key generation is generally a database sequence spliced by dates, and if a large concurrency quantity is involved and the serial number is used up, the uniqueness cannot be guaranteed, so that a snowflake algorithm is used. However, in the original structure, the data center position and the machine position need to be configured manually, so that the configuration amount is increased, and the manual configuration is easy to make mistakes to cause production accidents.
Summary of the invention
In order to overcome the defects of the prior art, the invention provides a problem of how to improve the success rate of message delivery and reduce the message loss by using an asynchronous notification method based on dynamic configuration.
The technical scheme adopted by the invention for solving the technical problems is as follows: a method for automatically configuring a machine ID based on a snowflake algorithm comprises the following steps:
the method comprises the following steps: splitting 10-bit work machine id into 2-bit data center bits and 8-bit machine id bits based on a snowflake algorithm;
step two: filling 2-bit data center bits by acquiring 2-bit data center IDs declared in the configuration file;
step three: the 8-bit machine id bit is filled with the obtained IPV4 address.
Specifically, based on the snowflake algorithm, the 10-bit work machine id is split into a 2-bit data center bit and an 8-bit machine id bit, and the steps specifically include:
the snowflake algorithm consists of 1-bit identification bit, 41-bit timestamp bits, 10-bit work machine id and 12-bit random number.
Preferably, before padding the 2-bit data center bits by obtaining the 2-bit data center ID stated in the configuration file, the steps further include:
acquiring a 2-bit data center ID declared in a configuration file in advance;
and when the 2-bit data center ID declared in the configuration file is acquired to be wrong, stopping service starting and performing exception prompt.
Preferably, before padding the 8-bit machine id bit with the obtained IPV4 address, the steps further include:
acquiring an IPV4 address of a current deployment machine;
and intercepting the acquired IPV4 address of the current deployment machine according to decimal points.
Further preferably, after intercepting the obtained IPV4 address of the currently deployed machine by decimal point, the step further includes:
and when the acquired IPV4 address of the current deployment machine or the intercepted IPV4 address of the current deployment machine is wrong, stopping service starting and performing exception prompt.
Preferably, the 1-bit identification bit of the snowflake algorithm is 0.
Preferably, the generation rule of the 41-bit time stamp of the snowflake algorithm is that the current time stamp is subtracted by the set start time stamp.
A system for automatically configuring a machine ID based on a snowflake algorithm, the system comprising:
the splitting unit is used for splitting the 10-bit work machine id into a 2-bit data center bit and an 8-bit machine id bit based on a snowflake algorithm;
a first padding unit for padding the 2-bit data center bits by acquiring the 2-bit data center ID declared in the configuration file;
and a second padding unit for padding the acquired IPV4 address with 8-bit machine id bits.
The invention has the beneficial effects that: based on a snowflake algorithm, a 10-bit work machine ID is divided into a 2-bit data center bit and an 8-bit machine ID bit, the 2-bit data center bit is filled by acquiring the 2-bit data center ID stated in a configuration file, and the 8-bit machine ID bit is filled by the acquired IPV4 address, so that the machine ID is automatically configured, and further, the repeated risk of a main key is reduced to a certain extent and the configuration amount is reduced.
Drawings
FIG. 1 is a flow diagram of a method for automatically configuring a machine ID based on a snowflake algorithm.
FIG. 2 is a functional block diagram of a system for automatically configuring machine IDs based on a snowflake algorithm.
Figure 3 is a block diagram of a snowflake algorithm.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
The following detailed description of specific implementations of the present invention is provided in conjunction with specific embodiments:
the first embodiment is as follows:
fig. 1 shows an implementation flow of a method for automatically configuring a machine ID based on a snowflake algorithm according to an embodiment of the present invention, and for convenience of description, only the parts related to the embodiment of the present invention are shown, which is detailed as follows:
in step S101: splitting 10-bit work machine id into 2-bit data center bits and 8-bit machine id bits based on a snowflake algorithm;
specifically, based on the snowflake algorithm, the 10-bit work machine id is split into a 2-bit data center bit and an 8-bit machine id bit, and the steps specifically include:
the snowflake algorithm consists of 1-bit identification bit, 41-bit timestamp bits, 10-bit work machine id and 12-bit random number.
In step S102: filling 2-bit data center bits by acquiring 2-bit data center IDs declared in the configuration file;
preferably, before padding the 2-bit data center bits by obtaining the 2-bit data center ID stated in the configuration file, the steps further include:
acquiring a 2-bit data center ID declared in a configuration file in advance;
and when the 2-bit data center ID declared in the configuration file is acquired to be wrong, stopping service starting and performing exception prompt.
In step S103: the 8-bit machine id bit is filled with the obtained IPV4 address.
Preferably, before padding the 8-bit machine id bit with the obtained IPV4 address, the steps further include:
acquiring an IPV4 address of a current deployment machine;
and intercepting the acquired IPV4 address of the current deployment machine according to decimal points.
Further preferably, after intercepting the obtained IPV4 address of the currently deployed machine by decimal point, the step further includes:
and when the acquired IPV4 address of the current deployment machine or the intercepted IPV4 address of the current deployment machine is wrong, stopping service starting and performing exception prompt.
Preferably, the 1-bit identification bit of the snowflake algorithm is 0.
Preferably, the generation rule of the 41-bit time stamp of the snowflake algorithm is that the current time stamp is subtracted by the set start time stamp.
It will be understood by those skilled in the art that all or part of the steps in the method for implementing the above embodiments may be implemented by relevant hardware instructed by a program, and the program may be stored in a computer-readable storage medium, such as ROM/RAM, magnetic disk, optical disk, etc.
Example two:
fig. 2 shows a structure of a system for automatically configuring a machine ID based on a snowflake algorithm according to a second embodiment of the present invention, and for convenience of description, only the parts related to the second embodiment of the present invention are shown, which are detailed as follows:
the splitting unit 201 is used for splitting the 10-bit work machine id into a 2-bit data center bit and an 8-bit machine id bit based on a snowflake algorithm;
a first padding unit 202, configured to pad 2-bit data center bits by obtaining a 2-bit data center ID declared in the configuration file;
and a second padding unit 203 for padding the 8-bit machine id bit with the obtained IPV4 address.
In the embodiment of the application, based on a snowflake algorithm, a 10-bit work machine ID is split into a 2-bit data center bit and an 8-bit machine ID bit, the 2-bit data center bit is filled by acquiring the 2-bit data center ID stated in a configuration file, and the 8-bit machine ID bit is filled by the acquired IPV4 address, so that the machine ID is automatically configured, and further, the repeated risk of a primary key is reduced to a certain extent and the configuration amount is reduced. The detailed implementation of each unit can refer to the description of the first embodiment, and is not repeated herein.
Example three:
fig. 3 shows a structure of a snowflake algorithm provided by the third embodiment of the present invention, and for convenience of explanation, only the parts related to the third embodiment of the present invention are shown, which include:
a scheme for automatically configuring a machine ID based on a snowflake algorithm comprises the following steps:
the original snowflake algorithm is composed of 1 identification bit, 41 time stamp bits, 10 working machine id (5 bit data center bits, 5 bit machine id bits) and 12 random numbers. Now the 10-bit work machine id is split into 2-bit data center bits + 8-bit machine id bits.
Filling 2-bit data center bits (initialized once only at start-up); by acquiring the 2-bit data center ID (which may be defined as a number between 0 and 3) stated in the configuration file in advance as the data center bit. And if an error is obtained, stopping service starting and carrying out exception prompt.
Fill in the 8-bit machine id bit (initialized once only at start-up);
and 3.1, acquiring an IPV4 address of the current deployment machine.
And 3.2, intercepting the acquired IPV4 address according to a decimal point (.). The last bit of the truncated array, i.e., the last 8 bits of IPV4, is fetched. Padding is being performed corresponding to the 8-bit machine id bits.
And 3.3, if the address is acquired or intercepted to be wrong, stopping the service starting and carrying out exception prompting.
And generating the rest digits according to the original rule, and finishing the configuration. The configuration of machine bits is not required to be modified when a machine is deployed.
4.1, the first bit is all 0. Because the first bit in the binary is a negative number if it is 1. But the ID generated needs to be a positive number.
4.2, 41 bit time stamp. The generation rule is (current timestamp) minus (set start timestamp) and left shifted (64-1-41) bits. If the current time stamp is smaller than the time stamp of the last ID, the system clock is back, and at the moment, abnormal prompt is performed.
4.3, 2 bits data center bit. The initialized value at start-up is taken (step 2) and shifted left (64-1-41-2).
4.4, 8 bit machine id bits. The initialized value at start-up is taken (step 3) and shifted left (64-1-41-2-8).
4.5, 12 bit sequence. A maximum of 4095 sequences can be achieved within 1 millisecond.
5. The above can constitute a snowflake algorithm number of 15-19 characters (the number of characters depends on the starting time length of the current time interval setting).
Those of ordinary skill in the art will appreciate that the elements and algorithm steps of the embodiments described in connection with the embodiments disclosed herein may be implemented as electronic hardware or combinations of computer software and electronic hardware. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the implementation.
Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present invention. The above description is only for the specific embodiments of the present invention, but the scope of the present invention is not limited thereto, and any person skilled in the art can easily conceive of the changes or substitutions within the technical scope of the present invention, and the changes or substitutions should be covered within the scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.
Claims (8)
1. A method for automatically configuring a machine ID based on a snowflake algorithm is characterized by comprising the following steps:
the method comprises the following steps: splitting 10-bit work machine id into 2-bit data center bits and 8-bit machine id bits based on a snowflake algorithm;
step two: filling 2-bit data center bits by acquiring 2-bit data center IDs declared in the configuration file;
step three: the 8-bit machine id bit is filled with the obtained IPV4 address.
2. The method for automatically configuring the machine ID based on the snowflake algorithm as claimed in claim 1, wherein the 10-bit work machine ID is split into 2-bit data center bits and 8-bit machine ID bits based on the snowflake algorithm, and the steps specifically include:
the snowflake algorithm consists of 1-bit identification bit, 41-bit timestamp bits, 10-bit work machine id and 12-bit random number.
3. The method of claim 2, wherein the step of padding the 2-bit data center bits by obtaining the 2-bit data center ID declared in the configuration file further comprises:
acquiring a 2-bit data center ID declared in a configuration file in advance;
and when the 2-bit data center ID declared in the configuration file is acquired to be wrong, stopping service starting and performing exception prompt.
4. The method of claim 3, wherein before padding the 8-bit machine ID bits with the acquired IPV4 address, the steps further comprise:
acquiring an IPV4 address of a current deployment machine;
and intercepting the acquired IPV4 address of the current deployment machine according to decimal points.
5. The method of claim 4, wherein after intercepting the captured IPV4 address of the currently deployed machine by decimal point, the method further comprises:
and when the acquired IPV4 address of the current deployment machine or the intercepted IPV4 address of the current deployment machine is wrong, stopping service starting and performing exception prompt.
6. The method for automatically configuring the machine ID based on the snowflake algorithm, according to claim 2, wherein the 1-bit identification bit of the snowflake algorithm is 0.
7. The method for automatically configuring the machine ID based on the snowflake algorithm is characterized in that the generation rule of the 41-bit time stamp of the snowflake algorithm is that the current time stamp is subtracted by the set starting time stamp.
8. A system for automatically configuring a machine ID based on a snowflake algorithm, the system comprising:
the splitting unit is used for splitting the 10-bit work machine id into a 2-bit data center bit and an 8-bit machine id bit based on a snowflake algorithm;
a first padding unit for padding the 2-bit data center bits by acquiring the 2-bit data center ID declared in the configuration file;
and a second padding unit for padding the acquired IPV4 address with 8-bit machine id bits.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202011065845.0A CN112235431A (en) | 2020-09-30 | 2020-09-30 | Method and system for automatically configuring machine ID based on snowflake algorithm |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202011065845.0A CN112235431A (en) | 2020-09-30 | 2020-09-30 | Method and system for automatically configuring machine ID based on snowflake algorithm |
Publications (1)
Publication Number | Publication Date |
---|---|
CN112235431A true CN112235431A (en) | 2021-01-15 |
Family
ID=74120528
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202011065845.0A Pending CN112235431A (en) | 2020-09-30 | 2020-09-30 | Method and system for automatically configuring machine ID based on snowflake algorithm |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN112235431A (en) |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115131912A (en) * | 2022-06-29 | 2022-09-30 | 深圳市智莱科技股份有限公司 | Initialization method, device, equipment and storage medium of Bluetooth cabinet |
CN117785385A (en) * | 2023-12-30 | 2024-03-29 | 北京领雁科技股份有限公司 | ID generation system and method based on snowflake algorithm |
Citations (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20150089604A1 (en) * | 2013-09-20 | 2015-03-26 | Oracle International Corporation | Global unified session identifier across multiple data centers |
CN107864233A (en) * | 2017-11-08 | 2018-03-30 | 千寻位置网络有限公司 | Globally unique ID structures and generation method |
CN108282531A (en) * | 2018-01-24 | 2018-07-13 | 深圳市欧辰技术有限公司 | A kind of uniqueness ID generation methods based on distributed system |
CN108322222A (en) * | 2018-01-16 | 2018-07-24 | 四川斐讯信息技术有限公司 | A method of it generates and using distributed unique ID |
CN110634052A (en) * | 2019-09-20 | 2019-12-31 | 中国银行股份有限公司 | Method and device for generating order number by distributed architecture |
CN111353275A (en) * | 2018-12-21 | 2020-06-30 | 北京宸信征信有限公司 | Unique identifier of distributed system and generation method thereof |
CN111694792A (en) * | 2020-05-29 | 2020-09-22 | 中国建设银行股份有限公司 | Identification generation method and device based on snowfly |
-
2020
- 2020-09-30 CN CN202011065845.0A patent/CN112235431A/en active Pending
Patent Citations (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20150089604A1 (en) * | 2013-09-20 | 2015-03-26 | Oracle International Corporation | Global unified session identifier across multiple data centers |
CN107864233A (en) * | 2017-11-08 | 2018-03-30 | 千寻位置网络有限公司 | Globally unique ID structures and generation method |
CN108322222A (en) * | 2018-01-16 | 2018-07-24 | 四川斐讯信息技术有限公司 | A method of it generates and using distributed unique ID |
CN108282531A (en) * | 2018-01-24 | 2018-07-13 | 深圳市欧辰技术有限公司 | A kind of uniqueness ID generation methods based on distributed system |
CN111353275A (en) * | 2018-12-21 | 2020-06-30 | 北京宸信征信有限公司 | Unique identifier of distributed system and generation method thereof |
CN110634052A (en) * | 2019-09-20 | 2019-12-31 | 中国银行股份有限公司 | Method and device for generating order number by distributed architecture |
CN111694792A (en) * | 2020-05-29 | 2020-09-22 | 中国建设银行股份有限公司 | Identification generation method and device based on snowfly |
Cited By (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115131912A (en) * | 2022-06-29 | 2022-09-30 | 深圳市智莱科技股份有限公司 | Initialization method, device, equipment and storage medium of Bluetooth cabinet |
CN117785385A (en) * | 2023-12-30 | 2024-03-29 | 北京领雁科技股份有限公司 | ID generation system and method based on snowflake algorithm |
CN117785385B (en) * | 2023-12-30 | 2024-06-07 | 北京领雁科技股份有限公司 | ID generation system and method based on snowflake algorithm |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN112235431A (en) | Method and system for automatically configuring machine ID based on snowflake algorithm | |
CN110634052A (en) | Method and device for generating order number by distributed architecture | |
CN109815291B (en) | Data synchronization method and device, electronic equipment and storage medium | |
CN110554732A (en) | identification number generation method and device, electronic equipment and storage medium | |
CN115599870B (en) | Data synchronization method based on fusion of stock data and incremental data of message queue | |
CN110908833A (en) | Data backup method, device and equipment and computer readable storage medium | |
CN106657216B (en) | Serial number generation method and device | |
CN110602165B (en) | Government affair data synchronization method, device, system, computer equipment and storage medium | |
CN113505112B (en) | Method and device for generating ID without machine identification based on SnowFlake algorithm | |
CN111580992A (en) | Distributed ID generation method and system | |
CN107656796B (en) | Virtual machine cold migration method, system and equipment | |
CN109558209B (en) | Monitoring method for virtual machine | |
CN114827082A (en) | Method, system, device and medium for generating globally unique ID of distributed system | |
CN109166199B (en) | Password generation method, device and equipment | |
CN111913956A (en) | Unified service coding method and device | |
CN111026724B (en) | File synchronization method, device, equipment and medium based on distributed system | |
CN112383627A (en) | Method for generating unique identifier in distributed system | |
CN116112165B (en) | Key dynamic division management method based on key pool state | |
CN112711600A (en) | Method and system for setting transaction delayed uplink | |
CN116775764A (en) | Distributed ID generation method, distributed ID generation device, and storage medium | |
CN110990161A (en) | Shared resource access method, device, equipment and computer readable storage medium | |
KR102426621B1 (en) | Application service server that handles the sending of an efficient push notification message to the client terminal where the application is installed and operating method thereof | |
CN112783625B (en) | Emergency multitasking short message group sending system and method | |
CN108243397B (en) | Short message sending method and short message gateway equipment | |
CN105930521A (en) | Database management device and method |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
RJ01 | Rejection of invention patent application after publication | ||
RJ01 | Rejection of invention patent application after publication |
Application publication date: 20210115 |