CN110287184A - The method for generating distributed unique ID - Google Patents

The method for generating distributed unique ID Download PDF

Info

Publication number
CN110287184A
CN110287184A CN201910453875.XA CN201910453875A CN110287184A CN 110287184 A CN110287184 A CN 110287184A CN 201910453875 A CN201910453875 A CN 201910453875A CN 110287184 A CN110287184 A CN 110287184A
Authority
CN
China
Prior art keywords
unique
application server
key
newly
generated
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
Application number
CN201910453875.XA
Other languages
Chinese (zh)
Inventor
陈晓敬
李迎龙
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Bichenghui (chengdu) Technology Co Ltd
Original Assignee
Bichenghui (chengdu) Technology Co Ltd
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Bichenghui (chengdu) Technology Co Ltd filed Critical Bichenghui (chengdu) Technology Co Ltd
Priority to CN201910453875.XA priority Critical patent/CN110287184A/en
Publication of CN110287184A publication Critical patent/CN110287184A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/22Indexing; Data structures therefor; Storage structures
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/23Updating

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Data Mining & Analysis (AREA)
  • Databases & Information Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Software Systems (AREA)
  • Computer And Data Communications (AREA)

Abstract

The invention discloses a kind of methods for generating distributed unique ID, belong to computer communication technology field.By realizing that automaton node distributes using Redis, and when machine calls back, the automatic waiting time is synchronous and is calibrated.The present invention solves the problem of existing in the prior art because relying on zookeeper cluster or because of high labor cost caused by human configuration machine ID and the generation ID repetition when machine call back.

Description

The method for generating distributed unique ID
Technical field
The invention belongs to computer communication technology fields, more particularly to a kind of method for generating distributed unique ID.
Background technique
Unique ID that Twitter Snowflake is generated is 8 bytes (be expressed as binary system and share 64), including The sign bit of 1bit, the millisecond time of 41bit, the computer room ID and machine ID of 10bit, the sequence number of 12bit, timestamp, work Make machine ID, set of serial numbers is combined, although availability is strong, speed is fast, and computer room ID therein and machine ID need Worker is obtained from zookeeper when starting, and needs to introduce zookeeper and the dedicated server of independent snowflake. It is the uniqueness for guaranteeing machine ID in Twitter Snowflake scheme, has dependence (to need outside when Worker starting Unique No. Worker is obtained from Zookeeper), therefore unique sequence numbers are obtained using Twitter Snowflake, it needs to take Build Zookeeper cluster.Zookeeper cluster is relied in the prior art or needs human configuration machine ID, technical threshold or artificial Higher cost;In addition, when machine time calls back, in fact it could happen that ID is repeated.
Therefore, a kind of method for generating distributed unique ID is needed, is solved existing in the prior art because relying on Zookeeper cluster or because of high labor cost caused by human configuration machine ID, and ID weight occurs when machine calls back Multiple problem.
Summary of the invention
The purpose of the present invention is to provide one kind, globally unique with lower technical threshold and maintenance cost generation to realize The purpose of ID solves existing in the prior art because relying on zookeeper cluster or because of people caused by human configuration machine ID Work is at high cost, and the problem of ID is repeated occurs when machine calls back.
The technical scheme adopted by the invention is that providing a kind of method for generating distributed unique ID, including following step It is rapid:
S1, Redis server is completed and connected to starting application server, using Apply Names as Redis key, described INCR order is executed on key, and the corresponding value of key is increased by 1 and is denoted as N;When starting another application server, the N value It is updated to N+1;
S2 makees remainder calculating to node total number using the numerical value of N that Redis is obtained from the S1, and obtained remainder is made For the node ID parameter of application server;
Metric millisecond timestamp is converted into a 41 bit character strings by S3, application server, simultaneously will Machine node ID is expressed as one 10 strings of binary characters, by both of the aforesaid string of binary characters with application server by suitable 12 binary-coded serial numbers splicing that sequence generates, unique ID of the reconvert at decimal number as generation, and by unique ID In memory, circulation generates ID to record according to this
S4. when newly-generated ID be less than it is preceding be once stored in the ID in memory when, it is to be calibrated that application server is automatic etc., with Ensure to ultimately generate unique ID.
Further, the corresponding value of key if it does not exist, then sets 1 for the value of key in the S1.
Further, the S2 interior joint sum is in the space of 10bit, and the same time can at most accommodate 1024 and answer Use server node.
Further, the process to be calibrated such as automatic in the S3 are as follows: by ID newly-generated every time and be currently held in interior ID in depositing is compared, before newly-generated ID is greater than when saving ID in memory, the newly-generated ID is made For result return.
The beneficial effects of the present invention are:
1. the present invention generates distributed unique ID without relying on zookeeper cluster or needing human configuration machine ID, reduce Technical threshold or cost of labor.
2. the appearance that the present invention can effectively avoid repeating when machine calls back ID.
Detailed description of the invention
In order to more clearly explain the embodiment of the invention or the technical proposal in the existing technology, to embodiment or will show below There is attached drawing needed in technical description to be briefly described, it should be apparent that, the accompanying drawings in the following description is only this Some embodiments of invention for those of ordinary skill in the art without creative efforts, can be with It obtains other drawings based on these drawings.
Fig. 1 is unique ID generator dispensation machines node diagram.
Fig. 2 is server time readjustment processing schematic.
Fig. 3 is 64 binary form diagrams of unique ID.
Specific embodiment
Following will be combined with the drawings in the embodiments of the present invention, and technical solution in the embodiment of the present invention carries out clear, complete Site preparation description, it is clear that described embodiments are only a part of the embodiments of the present invention, instead of all the embodiments.It is based on Embodiment in the present invention, it is obtained by those of ordinary skill in the art without making creative efforts every other Embodiment shall fall within the protection scope of the present invention.
Globally unique ID value reasons for its use is to meet the serial number for recording globally unique order, payment.Each stream Water number must distribute a unique ID, and need certain sequence, and client is facilitated to sort;And in distributed system The ID that middle different machine generates must be unique, supports high concurrent, energy per second generates enough globally unique ID, globally unique ID will have time identifier, facilitate the generation time for judging serial number.
Concrete scheme is as follows:
1. this method is that machine node space is defined as 10bit, the same time can at most accommodate 2 10 powers i.e. 1024 Application node;
2. as shown in Figure 1.Redis server is connected after the completion of starting application server, using Apply Names as Redis Key executes INCR order on the key, and the corresponding value of key is increased by 1 and (if key is not present, is set the value of key to 1) which, is denoted as N.When starting another application server, which will be updated to N+1.
3. remainder calculating is made to node total number (1024) with the numerical value of N obtained from Redis, using obtained remainder as this Machine node ID parameter in method.For example, the numerical value that current application service is obtained from Redis is 1049, with 1049 pair 1024 Remainder, i.e. 1049%1024=25;
4. as shown in Figure 2.When generating 64 2 system numbers, the preceding primary unique ID of return can be recorded in memory, if It was found that newly-generated ID is less than the preceding ID once returned, i.e., explanation is called back in this period machine time, in the case This method will be spinned (circulation, which obtains, generates new ID), and newly generated unique ID ID returned with the last time is compared, After ID until the ID produced again is greater than, can just it return the result.
As shown in Figure 3.Algorithm unique ID generated is the integer of an int64 (i.e. with binary representation, digital length Degree is 64), which is made of four parts altogether: the 1st expression symbol, 2-42 (41) expression current times, 43-52 (10) expression machine nodes, 53-64 (12) sequence numbers being sequentially generated for application server oneself.It will It is the integer of a 18-19 bit length after 64 binary numbers are converted into 10 systems, which is used as unique ID.
Because of hardware reason, server, which uses, is likely to occur time inaccurate problem after a period of time, periodically services with ntp It is a kind of fairly simple effective correcting scheme that device, which sets time update,.And snowflake algorithm relies on system time and generates ID, After server and ntp server complete clock synchronization, system time may occur to call back (system time is such as called back 50ms), Under such scene, snowflake algorithm may generate duplicate ID to break the technical requirements of " unique ID ".This patent For such scene, the realization of snowflake is optimized, when the current time got is less than previous secondary At ID obtain time when, this algorithm can using similar to " spin lock " technical solution, by constantly retrying when current Between be more than the previous time time point for generating ID, avoid the problem that repeating to cause to generate because of the time repeating ID.
The foregoing is merely illustrative of the preferred embodiments of the present invention, is not intended to limit the scope of the present invention.It is all Any modification, equivalent replacement, improvement and so within the spirit and principles in the present invention, are all contained in protection scope of the present invention It is interior.

Claims (4)

1. a kind of method for generating distributed unique ID, which comprises the following steps:
S1, Redis server is completed and connected to starting application server, using Apply Names as Redis key, in the key The corresponding value of key is increased by 1 and is denoted as N by upper execution INCR order;When starting another application server, the N value is by more It is newly N+1;
S2 makees remainder calculating to node total number using the numerical value of N that Redis is obtained from the S1, using obtained remainder as answering With the node ID parameter of server;
Metric millisecond timestamp is converted into a 41 bit character strings by S3, application server, while by machine Node ID is expressed as one 10 strings of binary characters, and both of the aforesaid string of binary characters is given birth in order with application server At the splicing of 12 binary-coded serial numbers, reconvert at decimal number as the unique ID generated, and by the unique ID record In memory, circulation generates ID according to this;
S4. when newly-generated ID be less than it is preceding be once stored in the ID in memory when, it is to be calibrated that application server is automatic etc., to ensure Ultimately generate unique ID.
2. a kind of method for generating distributed unique ID according to claim 1, which is characterized in that key pairs in the S1 The value answered if it does not exist, then sets 1 for the value of key.
3. a kind of method for generating distributed unique ID according to claim 1, which is characterized in that the S2 interior joint is total Number is in the space of 10bit, and the same time can at most accommodate 1024 application server nodes.
4. a kind of method for generating distributed unique ID according to claim 1, which is characterized in that automatic etc. in the S3 Process to be calibrated are as follows:
ID newly-generated every time is compared with the ID being currently held in memory, the preservation before being greater than to newly-generated ID When ID in memory, the newly-generated ID is returned as a result.
CN201910453875.XA 2019-05-28 2019-05-28 The method for generating distributed unique ID Pending CN110287184A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201910453875.XA CN110287184A (en) 2019-05-28 2019-05-28 The method for generating distributed unique ID

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201910453875.XA CN110287184A (en) 2019-05-28 2019-05-28 The method for generating distributed unique ID

Publications (1)

Publication Number Publication Date
CN110287184A true CN110287184A (en) 2019-09-27

Family

ID=68002834

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201910453875.XA Pending CN110287184A (en) 2019-05-28 2019-05-28 The method for generating distributed unique ID

Country Status (1)

Country Link
CN (1) CN110287184A (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111078693A (en) * 2019-11-29 2020-04-28 叮当快药科技集团有限公司 Method and device for generating database record primary key
CN113708983A (en) * 2021-11-01 2021-11-26 湖南新云网科技有限公司 Distributed node ID generation method, device, equipment and storage medium

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US9286369B2 (en) * 2009-12-30 2016-03-15 Symantec Corporation Data replication across enterprise boundaries
CN107454203A (en) * 2017-07-19 2017-12-08 成都数联铭品科技有限公司 The method for generating unique ID in a distributed system
CN108322222A (en) * 2018-01-16 2018-07-24 四川斐讯信息技术有限公司 A method of it generates and using distributed unique ID
US10152377B2 (en) * 2015-02-19 2018-12-11 Netapp, Inc. Layering a distributed storage system into storage groups and virtual chunk spaces for efficient data recovery
CN109697133A (en) * 2018-12-28 2019-04-30 广州华多网络科技有限公司 ID generation method, apparatus and system

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US9286369B2 (en) * 2009-12-30 2016-03-15 Symantec Corporation Data replication across enterprise boundaries
US10152377B2 (en) * 2015-02-19 2018-12-11 Netapp, Inc. Layering a distributed storage system into storage groups and virtual chunk spaces for efficient data recovery
CN107454203A (en) * 2017-07-19 2017-12-08 成都数联铭品科技有限公司 The method for generating unique ID in a distributed system
CN108322222A (en) * 2018-01-16 2018-07-24 四川斐讯信息技术有限公司 A method of it generates and using distributed unique ID
CN109697133A (en) * 2018-12-28 2019-04-30 广州华多网络科技有限公司 ID generation method, apparatus and system

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
普通程序员: "ID生成策略-SnowFlake", 《HTTP://BLOG.ITPUB.NET/31556438/VIEWSPACE-2636137/》 *

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111078693A (en) * 2019-11-29 2020-04-28 叮当快药科技集团有限公司 Method and device for generating database record primary key
CN113708983A (en) * 2021-11-01 2021-11-26 湖南新云网科技有限公司 Distributed node ID generation method, device, equipment and storage medium
CN113708983B (en) * 2021-11-01 2022-02-11 湖南新云网科技有限公司 Distributed node ID generation method, device, equipment and storage medium

Similar Documents

Publication Publication Date Title
CA2746011C (en) Method and device for mapping and demapping a client signal
Ozfatura et al. Speeding up distributed gradient descent by utilizing non-persistent stragglers
CN104486743B (en) A method of user information correlation is carried out to each interface XDR data of core net
US3903371A (en) Common control framing detector
CN108959386A (en) Distributed globally unique ID generation method, device, equipment and storage medium
CN110287184A (en) The method for generating distributed unique ID
CN106878528A (en) A kind of disturbance incoming call SMS interception method and system based on block chain technology
CN110943797B (en) Data compression method in SDH network
CN101997714A (en) Time processing method, device and system
CN100579133C (en) Method for generating ID symbol
CN109510681A (en) A kind of communication system time synchronization the smallest datum node selection method of series
CN104699046B (en) It is a kind of to be realized with public network to photovoltaic or the method and system of wind power station remote control
CN109729591A (en) A kind of time division multiple access slot distribution method based on genetic algorithm
CN112822054B (en) Remote management method for multiple related devices by using same network element
CN116723239B (en) Block chain data transmission method and device, electronic equipment and readable medium
CN101232344A (en) Method and apparatus for frame alignment
Lien A new node-join-tree distributed algorithm for minimum weight spanning trees
CN101621344B (en) Method for determining virtual concatenation alignment out-of-limit and device for processing communication services
CN115865346A (en) Key synchronization method and system based on multidimensional factors
CN113316170B (en) Method and system for improving networking stability of smoke alarm system
CN107612674A (en) A kind of multiple servers automatic synchronization time method and device
CN110995605B (en) Message synchronization method, system and communication equipment
DE4109150A1 (en) Local area communications network - feeds synchronisation information along lines extending between independently clocked nodes
Pang et al. Access point decoding coded MapReduce for tree fog network
Jin et al. A Dynamic VONE Algorithm Considering Topology For Hybrid Services in SDM-EON

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

Application publication date: 20190927

RJ01 Rejection of invention patent application after publication