Unique number generation method and device, electronic equipment and storage medium
By combining the snowflake algorithm and distributed coordination service clusters to generate unique numbers, the problem of low resource consumption and performance of UUID generation strategies in high concurrency environments is solved, and a shorter unique number generation is achieved that is more suitable for high concurrency environments.
Patent Information
- Application Number
- CN202411859443.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-17
- Publication Date
- 2025-04-11
AI Technical Summary
In high concurrency environments, the existing UUID generation strategies have problems such as high resource consumption, low number generation performance, and inability to process in sequence.
Generate unique numbers by combining snowflake algorithm and distributed coordination service cluster. The specific steps include obtaining the initial machine identification when the number generation device is started, determining the timestamp and sequence number in response to high concurrency requests, and generating a unique number based on this information.
The generated unique numbers are shorter and consume less resources. They are suitable for high concurrency environments and can be sorted by serial numbers to improve performance.
Smart Images

Figure CN119808709A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of distributed technology, and in particular to a unique number generation method, device, electronic device and storage medium. Background Art
[0002] In the current big data era, distributed systems are widely used in fields such as the Internet, cloud computing, the Internet of Things, and artificial intelligence.
[0003] In distributed systems, unique number generation in a high-concurrency environment is a key issue. At present, common unique number generation schemes include generation strategies based on UUID (Universally Unique Identifier). However, this method has certain shortcomings: UUID usually includes 36 bytes, which is a huge burden for storage and transmission, especially in systems that need to generate numbers frequently. UUID is randomly generated and has no fixed order, which is disadvantageous for some scenarios that need to be processed in the order of numbers. At the same time, although it is relatively fast, in high-concurrency scenarios, if each request needs to generate a UUID, it will still affect performance. Therefore, there is an urgent need for a new unique number generation method in high-concurrency scenarios. Summary of the invention
[0004] The present invention provides a unique number generation method, device, electronic device, storage medium and computer program product.
[0005] According to one aspect of the present invention, there is provided a method for generating a unique number, comprising:
[0006] After the number generation device is started, the pre-deployed distributed coordination service cluster is accessed, and an initial machine identifier corresponding to the number generation device is obtained from the distributed coordination service cluster;
[0007] In response to receiving a number generation request in a high-concurrency environment, determining a timestamp and a sequence number corresponding to each number generation request;
[0008] Based on the snowflake algorithm, a corresponding unique number is generated for each number generation request according to the initial machine identifier, the timestamp and serial number corresponding to each number generation request.
[0009] In some embodiments, obtaining an initial machine identifier corresponding to a number generation device from a distributed coordination service cluster includes:
[0010] Obtain the distributed lock provided by the distributed coordination service cluster;
[0011] After successfully acquiring the distributed lock, the initial machine identifier corresponding to the number generation device is read from the node storing the machine identifier in the distributed coordination service cluster.
[0012] In some embodiments, the method further comprises:
[0013] In the initial configuration phase, an initial machine identifier is allocated to each number generation device, and a mapping relationship between each number generation device and the initial machine identifier is stored in a preset namespace of the distributed coordination service cluster.
[0014] In some embodiments, the distributed coordination service cluster monitors the status of the number generation device in real time through a monitoring mechanism, and releases the initial machine identifier corresponding to the number generation device after determining that the number generation device is offline;
[0015] Accordingly, the method further includes:
[0016] After the number generation device comes back online, re-access the distributed coordination service cluster to obtain the latest machine ID.
[0017] In some embodiments, the method further comprises:
[0018] Through the monitoring mechanism, the operating status of the distributed coordination service cluster is monitored in real time;
[0019] If the master node of the distributed coordination service cluster is determined to be faulty based on the running status, the cluster fault tolerance mechanism is triggered to perform node recovery.
[0020] In some embodiments, in response to receiving a number generation request in a high concurrency environment, determining a timestamp and a sequence number corresponding to each number generation request includes:
[0021] In response to receiving a number generation request in a high-concurrency environment, obtaining current time information, and converting the current time information into a timestamp corresponding to the number generation request;
[0022] The serial numbers corresponding to the number generation requests are determined by means of self-increment of the serial numbers.
[0023] According to another aspect of the present invention, there is provided a unique number generating device, comprising:
[0024] A first parameter acquisition module is used to access a pre-deployed distributed coordination service cluster after the number generation device is started, and obtain an initial machine identifier corresponding to the number generation device from the distributed coordination service cluster;
[0025] A second parameter acquisition module, configured to determine, in response to receiving a number generation request in a high concurrency environment, a timestamp and a sequence number corresponding to each number generation request;
[0026] The number generation module is used to generate a corresponding unique number for each number generation request based on a snowflake algorithm according to an initial machine identifier, a timestamp and a serial number corresponding to each number generation request.
[0027] In some embodiments, in acquiring an initial machine identifier corresponding to a number generation device from a distributed coordination service cluster, the first parameter acquisition module is specifically configured to:
[0028] Obtain the distributed lock provided by the distributed coordination service cluster;
[0029] After successfully acquiring the distributed lock, the initial machine identifier corresponding to the number generation device is read from the node storing the machine identifier in the distributed coordination service cluster.
[0030] In some embodiments, it also includes:
[0031] The initial configuration module is used to allocate an initial machine identifier to each number generation device in the initial configuration phase, and save the mapping relationship between each number generation device and the initial machine identifier in a preset namespace of the distributed coordination service cluster.
[0032] In some embodiments, the distributed coordination service cluster monitors the status of the number generation device in real time through a monitoring mechanism, and releases the initial machine identifier corresponding to the number generation device after determining that the number generation device is offline;
[0033] Accordingly, the device further comprises:
[0034] The third parameter acquisition module is used to re-access the distributed coordination service cluster to obtain the latest machine identification after the number generation device comes back online.
[0035] In some embodiments, it also includes:
[0036] The monitoring module is used to monitor the running status of the distributed coordination service cluster in real time through the monitoring mechanism;
[0037] The recovery module is used to trigger the cluster fault tolerance mechanism to perform node recovery if it is determined based on the running status that the master node of the distributed coordination service cluster fails.
[0038] In some embodiments, the second parameter acquisition module is specifically used to:
[0039] In response to receiving a number generation request in a high-concurrency environment, obtaining current time information, and converting the current time information into a timestamp corresponding to the number generation request;
[0040] The serial numbers corresponding to the number generation requests are determined by means of self-increment of the serial numbers.
[0041] According to another aspect of the present invention, an electronic device is provided, the electronic device comprising:
[0042] at least one processor; and
[0043] a memory communicatively connected to at least one processor; wherein,
[0044] The memory stores a computer program that can be executed by at least one processor. The computer program is executed by at least one processor so that the at least one processor can execute the unique number generating method of the embodiment of the present invention.
[0045] According to another aspect of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the unique number generation method of an embodiment of the present invention when executed.
[0046] According to another aspect of the present disclosure, a computer program product is provided, including a computer program, and when the computer program is executed by a processor, the steps in the above method are implemented.
[0047] The technical solution of the embodiment of the present invention combines the snowflake algorithm and the distributed coordination service cluster to generate a unique number. Compared with the UUID-based generation strategy, the generated unique number is shorter and occupies less transmission and storage resources; and the unique number corresponding to each number generation request can be sorted by serial number, which is more suitable for a high concurrency environment.
[0048] It should be understood that the contents described in this section are not intended to identify the key or important features of the embodiments of the present invention, nor are they intended to limit the scope of the present invention. Other features of the present invention will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0049] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0050] Figure 1 It is a flowchart of a unique number generation method provided by an embodiment of the present invention;
[0051] Figure 2 It is a flowchart of another unique number generation method provided by an embodiment of the present invention;
[0052] Figure 3 It is a flowchart of another unique number generation method provided by an embodiment of the present invention;
[0053] Figure 4 It is a structural schematic diagram of a unique number generating device provided by an embodiment of the present invention;
[0054] Figure 5 It is a schematic diagram of the structure of an electronic device for implementing the unique number generation method of an embodiment of the present invention. DETAILED DESCRIPTION
[0055] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0056] Embodiment 1
[0057] Figure 1 A flowchart of a unique number generation method is provided for an embodiment of the present invention. This embodiment is applicable to the generation of unique numbers in multiple concurrent scenarios. The method can be executed by a unique number generation device. The unique number generation device can be implemented in the form of hardware and / or software. The unique number generation device can be configured in an electronic device.
[0058] like Figure 1 As shown, the unique number generation method includes:
[0059] S101: After the number generation device is started, access a pre-deployed distributed coordination service cluster, and obtain an initial machine identifier corresponding to the number generation device from the distributed coordination service cluster.
[0060] In an embodiment of the present invention, a number generation device may be a node in a distributed business system in which a number generation algorithm is deployed, and the number generation algorithm may be, for example, a snowflake algorithm or other self-incrementing unique identifier generation algorithm. Exemplarily, the number generation algorithm adopts a snowflake algorithm, and then a number generation device may be regarded as a snowflake node. A pre-deployed distributed coordination service distributed coordination service cluster includes a master node and multiple standby nodes, which are distributed on different physical machines or virtual machines, wherein the master node provides services to the outside, and the standby node is used to synchronize data from the master node. A namespace is specially created for the number generation algorithm in the host point of the distributed coordination service cluster. Thus, in the initial configuration phase, an initial machine identifier (for example, composed of a data center ID and a machine ID) is assigned to each number generation device, and the mapping relationship between each number generation device and the initial machine identifier is saved in the preset namespace of the distributed coordination service cluster. It can be understood that by creating a special namespace, data and nodes related to the number generation algorithm can be isolated from data of other services or functions to avoid data confusion and mutual interference between different services.
[0061] In an embodiment of the present invention, when the number generation device is started, it can establish a connection with a pre-deployed distributed coordination service cluster and access the distributed coordination service cluster to obtain an initial machine identifier corresponding to the number generation device from the namespace of the distributed coordination service cluster, so that a unique number can be subsequently constructed based on the initial machine identifier.
[0062] S102: In response to receiving a number generation request in a high-concurrency environment, determine a timestamp and a sequence number corresponding to each number generation request.
[0063] In the embodiment of the present invention, a high concurrency environment refers to a situation where a large number of requests arrive at the system for processing at the same time. For example, during the promotion of an e-commerce platform, a large number of users place orders and pay for goods at the same time. At this time, it is necessary to send a number generation request to the number generation device to request the number generation device to generate a unique number (such as a payment serial number) for a large number of payment orders generated at the same time. In order to generate a corresponding unique number for each number generation request, the number generation device determines the timestamp and serial number corresponding to each number generation request after receiving the number generation request (the number is multiple) in a high concurrency environment; specifically, for any number generation request in a high concurrency environment, the current time information is obtained, and the current time information is converted into the timestamp corresponding to the number generation request. The serial numbers corresponding to the number generation requests are determined by the self-increment of the serial number. It can be understood that in a high concurrency environment, a number generation device may receive multiple number generation requests in the same millisecond. By the self-increment of the serial number, the serial number corresponding to each number generation request is determined, which can ensure that the serial number of each number generation request is different. In this way, even if the timestamps corresponding to two or more number generation requests are the same, they can be distinguished by the different corresponding serial numbers.
[0064] S103. Based on the snowflake algorithm, a corresponding unique number is generated for each number generation request according to the initial machine identifier, the timestamp and the serial number corresponding to each number generation request.
[0065] In the embodiment of the present invention, the unique number generated by the snowflake algorithm is usually a 64-bit long integer. To generate a corresponding unique number for a number generation request, it is first necessary to determine the target positions of the initial machine identifier, timestamp and serial number corresponding to the number generation request in the 64-bit number; and then, according to the target positions, assemble the initial machine identifier, timestamp and serial number corresponding to the number generation request into the unique number corresponding to the number generation request.
[0066] In an embodiment of the present invention, the snowflake algorithm and the distributed coordination service cluster are combined to generate a unique number. Compared with the UUID-based generation strategy, the generated unique number is shorter and occupies less transmission and storage resources; and the unique number corresponding to each number generation request can be sorted by serial number, which is more suitable for a high concurrency environment.
[0067] Embodiment 2
[0068] Figure 2 A flowchart of a method for generating a unique number is provided for an embodiment of the present invention. Figure 2 , the method comprises the following steps:
[0069] S201. After the number generation device is started, a distributed lock provided by the distributed coordination service cluster is obtained; and after the distributed lock is successfully obtained, an initial machine identifier corresponding to the number generation device is read from a node storing machine identifiers in the distributed coordination service cluster.
[0070] In an embodiment of the present invention, after starting, the number generation device will establish a connection with the distributed coordination service cluster. Since there are multiple number generation devices, if multiple number generation devices simultaneously access the node storing the machine identification in the distributed coordination service cluster to read the initial machine identification corresponding to the number generation device, an identification conflict will occur. For example, after two number generation devices read the same initial machine identification, the numbers generated based on this initial machine identification may be repeated, destroying the uniqueness of the number. In order to avoid this problem, the present invention introduces a distributed lock mechanism. Specifically, when multiple number generation devices simultaneously request to read the initial machine identification corresponding to the number generation device, a distributed lock is first obtained. When a number generation device successfully obtains the lock, the machine identification is read, and then it can use this identification to generate numbers according to a predetermined rule. Other number generation devices need to wait for the lock to be released before they can obtain the initial machine identification. In this way, through the distributed lock, each number generation device can access the node storing the machine identification in the order of obtaining the lock, so that the orderliness of the operation between devices is realized, and there will be no confusion or inconsistency of identification caused by concurrent access.
[0071] S202: In response to receiving a number generation request in a high-concurrency environment, determine a timestamp and a sequence number corresponding to each number generation request.
[0072] Optionally, in response to receiving a number generation request in a high concurrency environment, current time information is obtained and converted into a timestamp corresponding to the number generation request; and serial numbers corresponding to each number generation request are determined by self-incrementing the serial numbers.
[0073] S203. Based on the snowflake algorithm, a corresponding unique number is generated for each number generation request according to the initial machine identifier, the timestamp and the serial number corresponding to each number generation request.
[0074] In the embodiment of the present invention, the unique number generated by the snowflake algorithm is usually a 64-bit long integer. To generate a corresponding unique number for a number generation request, it is first necessary to determine the target positions of the initial machine identifier, timestamp and serial number corresponding to the number generation request in the 64-bit number; and then, according to the target positions, assemble the initial machine identifier, timestamp and serial number corresponding to the number generation request into the unique number corresponding to the number generation request.
[0075] In an embodiment of the present invention, when the numbering device obtains the corresponding initial machine identifier from the distributed coordination service cluster, a distributed lock mechanism is introduced. Through the distributed lock, each numbering device can access the node storing the machine identifier in the order of acquiring the lock, thereby achieving orderliness of operations between devices.
[0076] Embodiment 3
[0077] Figure 3 The present invention provides a flowchart of a unique number generation method. Figure 3 , the method comprises the following steps:
[0078] S301: After the number generation device is started, access a pre-deployed distributed coordination service cluster, and obtain an initial machine identifier corresponding to the number generation device from the distributed coordination service cluster.
[0079] Optionally, after the number generation device is started, a distributed lock provided by the distributed coordination service cluster is obtained; and after successfully obtaining the distributed lock, an initial machine identifier corresponding to the number generation device is read from a node storing machine identifiers in the distributed coordination service cluster.
[0080] In an embodiment of the present invention, the distributed coordination service cluster monitors the status of the number generation device in real time through a monitoring mechanism, and releases the initial machine identifier corresponding to the number generation device after determining that the number generation device is offline (for example, the number generation device is offline due to a fault), so that during the period when the number generation device is offline, if a new number generation device requests the initial machine identifier, the released initial machine identifier can be assigned to the new number generation device; wherein, the new number generation device can be newly expanded to cope with high concurrency scenarios. In this way, it can be avoided that a machine identifier is occupied invalidly for a long time, resulting in a waste of the machine identifier. After the offline number generation device is back online, the distributed coordination service cluster is re-accessed to obtain the latest machine identifier, for example, the currently idle machine identifier is read from the distributed coordination service cluster, that is, the distributed coordination service cluster assigns the currently idle machine identifier to the number generation device that is back online.
[0081] S302: In response to receiving a number generation request in a high-concurrency environment, determine a timestamp and a sequence number corresponding to each number generation request.
[0082] Optionally, in response to receiving a number generation request in a high concurrency environment, current time information is obtained and converted into a timestamp corresponding to the number generation request; and serial numbers corresponding to each number generation request are determined by self-incrementing the serial numbers.
[0083] S303. Based on the snowflake algorithm, a corresponding unique number is generated for each number generation request according to the initial machine identifier, the timestamp and the serial number corresponding to each number generation request.
[0084] S304. Monitor the operating status of the distributed coordination service cluster in real time through a monitoring mechanism.
[0085] S305: If it is determined according to the running status that the master node of the distributed coordination service cluster fails, the cluster fault tolerance mechanism is triggered to perform node recovery.
[0086] In an embodiment of the present invention, the distributed coordination service cluster has high availability and fault tolerance. On this basis, the operating status of the distributed coordination service cluster is monitored in real time through a monitoring mechanism. If the main node of the distributed coordination service cluster is determined to be faulty according to the operating status, the cluster fault tolerance mechanism is triggered to perform node recovery to ensure that the distributed coordination service cluster provides services to the outside normally. Exemplarily, if the main node in the distributed coordination service cluster fails, the cluster fault tolerance mechanism can be used to select one of the multiple backup nodes of the distributed coordination service cluster as the main node to continue to provide services to the outside. That is, when a node in the distributed coordination service cluster fails, it can be repaired through the fault tolerance mechanism to ensure the continuity of services provided by the distributed coordination service cluster.
[0087] In the embodiment of the present invention, it can be ensured that the cluster can still work normally when a node fails, thereby ensuring the continuity of service.
[0088] Embodiment 4
[0089] Figure 4 Schematic diagram of a unique number generation device provided by an embodiment of the present invention. This embodiment is applicable to the generation of unique numbers in multiple concurrent scenarios. Figure 4 As shown, the unique number generating device includes:
[0090] The first parameter acquisition module 401 is used to access the pre-deployed distributed coordination service cluster after the number generation device is started, and obtain the initial machine identifier corresponding to the number generation device from the distributed coordination service cluster;
[0091] A second parameter acquisition module 402, configured to determine a timestamp and a sequence number corresponding to each number generation request in response to receiving a number generation request in a high concurrency environment;
[0092] The number generation module 403 is used to generate a corresponding unique number for each number generation request based on a snowflake algorithm according to the initial machine identifier, the timestamp and the sequence number corresponding to each number generation request.
[0093] In some embodiments, in acquiring an initial machine identifier corresponding to a number generation device from a distributed coordination service cluster, the first parameter acquisition module is specifically configured to:
[0094] Obtain the distributed lock provided by the distributed coordination service cluster;
[0095] After successfully acquiring the distributed lock, the initial machine identifier corresponding to the number generation device is read from the node storing the machine identifier in the distributed coordination service cluster.
[0096] In some embodiments, it also includes:
[0097] The initial configuration module is used to allocate an initial machine identifier to each number generation device in the initial configuration phase, and save the mapping relationship between each number generation device and the initial machine identifier in a preset namespace of the distributed coordination service cluster.
[0098] In some embodiments, the distributed coordination service cluster monitors the status of the number generation device in real time through a monitoring mechanism, and releases the initial machine identifier corresponding to the number generation device after determining that the number generation device is offline;
[0099] Accordingly, the device further comprises:
[0100] The third parameter acquisition module is used to re-access the distributed coordination service cluster to obtain the latest machine identification after the number generation device comes back online.
[0101] In some embodiments, it also includes:
[0102] The monitoring module is used to monitor the running status of the distributed coordination service cluster in real time through the monitoring mechanism;
[0103] The recovery module is used to trigger the cluster fault tolerance mechanism to perform node recovery if it is determined based on the running status that the master node of the distributed coordination service cluster fails.
[0104] In some embodiments, the second parameter acquisition module is specifically used to:
[0105] In response to receiving a number generation request in a high-concurrency environment, obtaining current time information, and converting the current time information into a timestamp corresponding to the number generation request;
[0106] The serial numbers corresponding to the number generation requests are determined by means of self-increment of the serial numbers.
[0107] The unique number generating device provided in the embodiment of the present invention can execute the unique number generating method provided in any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.
[0108] According to an embodiment of the present disclosure, the present disclosure also provides an electronic device, a readable storage medium and a computer program product.
[0109] Embodiment 5
[0110] Figure 5 A schematic diagram of the structure of an electronic device 10 that can be used to implement an embodiment of the present invention is shown. The electronic device is intended to be a controller device in a new energy vehicle. The components shown herein, their connections and relationships, and their functions are only examples and are not intended to limit the implementation of the present invention described and / or required herein.
[0111] like Figure 5 As shown, the electronic device 10 includes at least one processor 11, and a memory connected to the at least one processor 11, such as a read-only memory (ROM) 12, a random access memory (RAM) 13, etc., wherein the memory stores a computer program that can be executed by at least one processor, and the processor 11 can perform various appropriate actions and processes according to the computer program stored in the read-only memory (ROM) 12 or the computer program loaded from the storage unit 18 to the random access memory (RAM) 13. In the RAM 13, various programs and data required for the operation of the electronic device 10 can also be stored. The processor 11, the ROM 12, and the RAM 13 are connected to each other through a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.
[0112] A number of components in the electronic device 10 are connected to the I / O interface 15, including: an input unit 16; an output unit 17, such as various types of displays, speakers, etc.; a storage unit 18, such as a disk, an optical disk, etc.; and a communication unit 19, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 19 allows the electronic device 10 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.
[0113] The processor 11 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, etc. The processor 11 executes the various methods and processes described above, such as executing a unique number generation method.
[0114] In some embodiments, the unique number generation method may be implemented as a computer program, which is tangibly contained in a computer-readable storage medium, such as a storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or installed on the electronic device 10 via the ROM 12 and / or the communication unit 19. When the computer program is loaded into the RAM 13 and executed by the processor 11, one or more steps of the unique number generation method described above may be performed. Alternatively, in other embodiments, the processor 11 may be configured to perform the unique number generation method in any other appropriate manner (e.g., by means of firmware).
[0115] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0116] The computer program for implementing the method of the present invention can be written in any combination of one or more programming languages. These computer programs can be provided to a processor of a general-purpose computer, a special-purpose computer or other programmable unique number generating device, so that when the computer program is executed by the processor, the functions / operations specified in the flow chart and / or block diagram are implemented. The computer program can be executed entirely on the machine, partially on the machine, partially on the machine as a stand-alone software package and partially on a remote machine, or entirely on a remote machine or server.
[0117] In the context of the present invention, a computer-readable storage medium may be a tangible medium that may contain or store a computer program for use by or in combination with an instruction execution system, device or equipment. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices or equipment, or any suitable combination of the foregoing. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0118] To provide interaction with a user, the systems and techniques described herein may be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and a pointing device (e.g., a mouse or trackball) through which the user can provide input to the electronic device. Other types of devices may also be used to provide interaction with the user; for example, the feedback provided to the user may be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user may be received in any form (including acoustic input, voice input, or tactile input).
[0119] The systems and techniques described herein may be implemented in a computing system that includes backend components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes frontend components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such backend components, middleware components, or frontend components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), a blockchain network, and the Internet.
[0120] A computing system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The client and server relationship is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services.
[0121] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps described in the present invention can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solution of the present invention can be achieved, and this document does not limit this.
[0122] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for generating a unique number, characterized in that: include: After the number generation device is started, access the pre-deployed distributed coordination service cluster, and obtain the initial machine identifier corresponding to the number generation device from the distributed coordination service cluster; In response to receiving a number generation request in a high-concurrency environment, determining a timestamp and a sequence number corresponding to each number generation request; Based on the snowflake algorithm, a corresponding unique number is generated for each number generation request according to the initial machine identifier, the timestamp and the serial number corresponding to each number generation request.
2. The method according to claim 1, characterized in that The obtaining, from the distributed coordination service cluster, an initial machine identifier corresponding to the number generation device includes: Obtaining a distributed lock provided by the distributed coordination service cluster; After the distributed lock is successfully acquired, the initial machine identifier corresponding to the number generation device is read from a node storing machine identifiers in the distributed coordination service cluster.
3. The method according to claim 1, characterized in that Also includes: In the initial configuration phase, an initial machine identifier is allocated to each number generation device, and a mapping relationship between each number generation device and the initial machine identifier is stored in a preset namespace of the distributed coordination service cluster.
4. The method according to claim 1, characterized in that: The distributed coordination service cluster monitors the status of the number generation device in real time through a monitoring mechanism, and releases the initial machine identifier corresponding to the number generation device after determining that the number generation device is offline; Accordingly, the method further includes: After the number generation device comes back online, the distributed coordination service cluster is accessed again to obtain the latest machine identification.
5. The method according to claim 1, characterized in that: Also includes: Monitor the running status of the distributed coordination service cluster in real time through a monitoring mechanism; If it is determined according to the running status that the master node of the distributed coordination service cluster fails, a cluster fault tolerance mechanism is triggered to perform node recovery.
6. The method according to claim 1, characterized in that In response to receiving a number generation request in a high concurrency environment, determining a timestamp and a sequence number corresponding to each number generation request, including: In response to receiving a number generation request in a high-concurrency environment, obtaining current time information, and converting the current time information into a timestamp corresponding to the number generation request; The serial numbers corresponding to the number generation requests are determined by means of a self-incrementing serial number.
7. A unique number generating device, characterized in that: include: A first parameter acquisition module, configured to access a pre-deployed distributed coordination service cluster after the number generation device is started, and acquire an initial machine identifier corresponding to the number generation device from the distributed coordination service cluster; A second parameter acquisition module, configured to determine, in response to receiving a number generation request in a high concurrency environment, a timestamp and a sequence number corresponding to each number generation request; The number generation module is used to generate a corresponding unique number for each number generation request based on the snowflake algorithm according to the initial machine identifier, the timestamp and the serial number corresponding to each number generation request.
8. An electronic device, characterized in that: include: at least one processor; as well as a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can perform the method according to any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the method according to any one of claims 1 to 6 when executed.
10. A computer program product, comprising a computer program, which, when executed by a processor, implements the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Distributed ID generation method, device and equipment and storage medium
CN110543518A
Method for generating unique serial number of distributed system
CN111930765A
Distributed identification number generation method, electronic equipment and storage medium
CN115099190A
Generation method and device of globally unique number, equipment and storage medium
CN115686822A
Cited By
Distributed timestamp generation method and system based on table number
CN120407571A
Distributed identifier generation method and system and distributed service cluster
CN121418415A