Serial number generation method and device and server

文档序号:1505352 发布日期:2020-02-07 浏览:20次 中文

阅读说明:本技术 序列号生成方法、装置及服务器 (Serial number generation method and device and server ) 是由 高宇 纪朋伟 于 2019-10-09 设计创作,主要内容包括:本发明提供一种序列号生成方法、装置及服务器,该方法包括获取序列号池的标识,该标识用于唯一标记该序列号池;根据该序列号池的当前序列位置按顺序生成偏移量;按照预设规则将该序列号池的标识和偏移量拼装为序列号。该方法保证了生成的序列号的唯一性。(The invention provides a serial number generation method, a serial number generation device and a server, wherein the method comprises the steps of obtaining an identifier of a serial number pool, wherein the identifier is used for uniquely marking the serial number pool; sequentially generating offsets according to the current sequence position of the serial number pool; and assembling the identification and the offset of the serial number pool into a serial number according to a preset rule. The method ensures the uniqueness of the generated serial number.)

1. A method for generating a sequence number, comprising:

acquiring an identifier of a serial number pool, wherein the identifier is used for uniquely marking the serial number pool;

generating offsets in sequence according to the current sequence position of the serial number pool;

and assembling the identification of the serial number pool and the offset into a serial number according to a preset rule.

2. The method of claim 1, wherein prior to obtaining the identification of the pool of sequence numbers, the method further comprises:

selecting the sequence number pool from at least one target sequence number pool;

locking the current sequence position of the sequence number pool.

3. The method of claim 1, wherein the sequentially generating offsets according to the current sequence position of the sequence number pool comprises:

and generating offsets in sequence according to the current sequence position of the serial number pool in an offset interval pre-allocated to the serial number pool.

4. The method of claim 3, wherein the assembling the identifier of the serial number pool and the offset into serial numbers according to a preset rule comprises:

splicing the identifier of the serial number pool, the offset and the machine number of the node where the serial number pool is located into serial numbers in sequence;

the machine number of the node where the serial number pool is located corresponds to the offset interval of the serial number pool, and the offset interval of the serial number pool is not overlapped with the offset intervals of other serial number pools except the serial number pool.

5. The method of claim 1, wherein obtaining the identity of the pool of sequence numbers comprises:

and determining the user-defined code as the identification of the serial number pool.

6. The method of claim 5, wherein determining the user-defined code as an identification of a pool of serial numbers comprises:

executing a preset script to obtain a dynamic code defined by a user;

and determining the dynamic code as the identification of the serial number pool.

7. The method according to any one of claims 1 to 6, wherein the assembling the identifier of the serial number pool and the offset into serial numbers according to a preset rule comprises:

and splicing a preset prefix and/or a preset suffix, the identifier of the serial number pool and the offset into a serial number.

8. A serial number generation apparatus, comprising:

the acquisition module is used for acquiring an identifier of a serial number pool, and the identifier is used for uniquely marking the serial number pool;

the generating module is used for sequentially generating offset according to the current sequence position of the serial number pool;

and the assembling module is used for assembling the identification of the serial number pool and the offset into a serial number according to a preset rule.

9. A server, comprising a memory and a processor; the memory is connected with the processor;

the memory for storing a computer program;

the processor, when being executed by a computer program, is configured to implement the serial number generation method according to any one of claims 1 to 7.

10. A storage medium having stored thereon a computer program for implementing the method of generating a serial number according to any of claims 1 to 7 when executed by a processor.

Technical Field

The present invention relates to computer technologies, and in particular, to a method and an apparatus for generating a serial number, and a server.

Background

In a large number of computer applications, a serial number needs to be generated to uniquely identify various information in an application system, as the data volume is larger and larger, the requirement for the uniqueness of the serial number is higher and higher, and the normal operation of many applications also depends on the uniqueness of the serial number.

Disclosure of Invention

The invention provides a serial number generation method, a serial number generation device and a server, which ensure the uniqueness of a generated serial number.

In a first aspect, the present invention provides a method for generating a sequence number, including:

acquiring an identifier of a serial number pool, wherein the identifier is used for uniquely marking the serial number pool;

generating offsets in sequence according to the current sequence position of the serial number pool;

and assembling the identification of the serial number pool and the offset into a serial number according to a preset rule.

Optionally, before obtaining the identifier of the serial number pool, the method further includes:

selecting the sequence number pool from at least one target sequence number pool;

locking the current sequence position of the sequence number pool.

Optionally, the sequentially generating offsets according to the current sequence position of the sequence number pool includes:

and generating offsets in sequence according to the current sequence position of the serial number pool in an offset interval pre-allocated to the serial number pool.

Optionally, the assembling the identifier of the serial number pool and the offset into a serial number according to a preset rule includes:

splicing the identifier of the serial number pool, the offset and the machine number of the node where the serial number pool is located into serial numbers in sequence;

the machine number of the node where the serial number pool is located corresponds to the offset interval of the serial number pool, and the offset interval of the serial number pool is not overlapped with the offset intervals of other serial number pools except the serial number pool.

Optionally, the obtaining the identifier of the serial number pool includes:

and determining the user-defined code as the identification of the serial number pool.

Optionally, the determining the user-defined code as an identifier of the serial number pool includes:

executing a preset script to obtain a dynamic code defined by a user;

and determining the dynamic code as the identification of the serial number pool.

Optionally, the assembling the identifier of the serial number pool and the offset into a serial number according to a preset rule includes:

and splicing a preset prefix and/or a preset suffix, the identifier of the serial number pool and the offset into a serial number.

In a second aspect, the present invention provides a serial number generating apparatus, including:

the acquisition module is used for acquiring an identifier of a serial number pool, and the identifier is used for uniquely marking the serial number pool;

the generating module is used for sequentially generating offset according to the current sequence position of the serial number pool;

and the assembling module is used for assembling the identification of the serial number pool and the offset into a serial number according to a preset rule.

Optionally, the apparatus further comprises:

a selection module for selecting the pool of sequence numbers from at least one target pool of sequence numbers; locking the current sequence position of the sequence number pool.

Optionally, the generating module is configured to:

and generating offsets in sequence according to the current sequence position of the serial number pool in an offset interval pre-allocated to the serial number pool.

Optionally, the assembly module is configured to:

splicing the identifier of the serial number pool, the offset and the machine number of the node where the serial number pool is located into serial numbers in sequence;

the machine number of the node where the serial number pool is located corresponds to the offset interval of the serial number pool, and the offset interval of the serial number pool is not overlapped with the offset intervals of other serial number pools except the serial number pool.

Optionally, the obtaining module is configured to:

and determining the user-defined code as the identification of the serial number pool.

Optionally, the obtaining module is configured to:

executing a preset script to obtain a dynamic code defined by a user;

and determining the dynamic code as the identification of the serial number pool.

Optionally, the assembly module is configured to:

and splicing a preset prefix and/or a preset suffix, the identifier of the serial number pool and the offset into a serial number.

In a third aspect, the present invention provides a server, including a memory and a processor; the memory is connected with the processor;

the memory for storing a computer program;

the processor is configured to implement the serial number generation method according to any one of the first aspect when the computer program is executed.

In a fourth aspect, the present invention provides a storage medium having stored thereon a computer program which, when executed by a processor, implements the method of generating a serial number as defined in any one of the above first aspects.

The invention provides a serial number generation method, a serial number generation device and a server, wherein the method comprises the steps of obtaining an identifier of a serial number pool, wherein the identifier is used for uniquely marking the serial number pool; sequentially generating offsets according to the current sequence position of the serial number pool; and assembling the identification and the offset of the serial number pool into a serial number according to a preset rule. Because the identification of the serial number pool is unique, the offset of each production in the serial number pool is also unique, thereby ensuring the uniqueness of the serial number obtained by splicing the identification and the offset of the serial number pool and avoiding the occurrence of abnormity of various applications depending on the uniqueness of the serial number.

Drawings

In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings needed to be used in the description of the embodiments or the prior art will be briefly introduced below, and it is obvious that the drawings in the following description are some embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to these drawings without creative efforts.

FIG. 1 is a schematic diagram of a serial number application system according to the present invention;

fig. 2 is a schematic flow chart of a serial number generation method according to the present invention;

FIG. 3 is a diagram illustrating a data structure of a serial number pool according to the present invention;

fig. 4 is a schematic structural diagram of a serial number generation apparatus provided in the present invention;

fig. 5 is a schematic structural diagram of a server according to the present invention.

Detailed Description

In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.

Fig. 1 is a schematic diagram of a serial number application system provided in the present invention. As shown in fig. 1, the system includes a terminal device 101, a terminal device 102, and a server 103, where the server 103 may also be a server cluster. The user can send a request to the server through the terminal device 102 and the terminal device 102 to generate the serial number, the server 103 can obtain the identifier of the serial number pool of the user after receiving the request, further sequentially generate the offset according to the current sequence position of the serial number pool, and assemble the identifier of the serial number pool and the offset into the serial number according to the preset rule, and the uniqueness of the generated serial number can be ensured due to the uniqueness of the identifier of the serial number pool and the offset. The following describes an example of a serial number generation method provided by the present invention with reference to an embodiment.

Fig. 2 is a schematic flow chart of a serial number generation method provided in the present invention. The method may be performed by a server in the system shown in fig. 1. As shown in fig. 2, the method includes:

s201, obtaining an identifier of the serial number pool, wherein the identifier is used for uniquely marking the serial number pool.

The server may have at least one sequence number pool therein, each sequence number pool having a unique identifier to distinguish it from other sequence number pools. The serial number pool can be created by a serial number pool generation module that creates several pools of serial number objects in a single resource, such as a database or a redis, for example. Each serial number pool has a space for storing serial numbers, which is created when the serial number pool is initialized. As shown in fig. 3, each sequence number pool includes a plurality of sequence number arrays, each sequence number array includes a plurality of sequence number blocks, and each sequence number block stores a sequence number. If the space of the serial number pool is full, a new serial number pool can be automatically created, and the new serial number pool can be uniquely marked according to the time stamp of the natural day and the precision of the time stamp of the natural day to millisecond. It can be understood by those skilled in the art that the serial number pool may also be uniquely marked by using other identifiers, which is not limited in this embodiment. Taking the identifier of the serial number as the unique timestamp as an example, the identifier of the serial number pool is obtained as 20190707221123234.

S202, generating offsets in sequence according to the current sequence position of the sequence number pool.

The offset is generated in each sequence number pool according to the sequence, the current sequence position is the offset of the sequence number generated last time, for example, the current sequence position is 23456, that is, the offset of the sequence number generated last time is 23456, and then the offset generated according to the sequence when the sequence number is generated this time is 23457. Because the offsets are generated in sequence, the offset obtained in the serial number pool at each time can be guaranteed to be unique.

S203, assembling the identification and the offset of the serial number pool into a serial number according to a preset rule.

The preset rules can be set according to actual needs, for example, the assembly sequence of serial numbers, the addition of preset fixed character strings, and the like can be set. For example, the sequence numbers 2019070722112323423457 are obtained by splicing according to the sequence of the serial number pool identification and offset. Because the mark of the serial number pool is unique and the offset in the serial number pool is also unique, the serial number can be ensured to be unique. The sequence number generated after completion of assembling is marked as invalid after being extracted and sent to the application requester requesting the sequence number.

In the serial number generation method provided by this embodiment, an identifier of a serial number pool is obtained, where the identifier is used to uniquely mark the serial number pool; sequentially generating offsets according to the current sequence position of the serial number pool; and assembling the identification and the offset of the serial number pool into a serial number according to a preset rule. Because the identification of the serial number pool is unique, the offset of each production in the serial number pool is also unique, thereby ensuring the uniqueness of the serial number obtained by splicing the identification and the offset of the serial number pool and avoiding the occurrence of abnormity of various applications depending on the uniqueness of the serial number.

On the basis of the foregoing embodiment, since there may be a plurality of serial number pools in the server, before obtaining the identifier of the serial number pool in S201, the method further includes:

selecting the sequence number pool from at least one target sequence number pool; the current sequence position of the sequence number pool is locked.

The number of the sequence number pools in the server may be multiple, when the server generates the sequence number, one available sequence number pool is selected from the multiple sequence number pools, for example, if a sequence number space of one sequence number pool is filled, the sequence number pool is not available, one sequence number pool is selected from the available sequence number pools as the sequence number pool for generating the sequence number this time, for example, one sequence number pool may be randomly selected from the multiple available sequence number pools. After the sequence number pool is selected, the current sequence position of the current sequence number pool is locked, so that the sequence number repetition caused by other requests using the current sequence position of the sequence number pool to carry out sequence generation offset can be avoided.

In one possible approach, the generating the offset in S202 according to the current sequence position of the sequence number pool in sequence includes:

and generating the offset in sequence according to the current sequence position of the sequence number pool in the offset interval which is pre-allocated to the sequence number pool.

Each sequence number pool has an offset interval, i.e. a fragment interval of an offset, and when generating a sequence number, the offset is sequentially taken from the fragment interval. For example, the offset of the sequence number pool is 00001-99999, so that the offset of the sequence number generated by the sequence number pool is from 00001, and is sequentially increased by 1 to 99999.

Optionally, the offset interval of the sequence number pool may be obtained from a global database or redis to ensure that there is no duplicate value in the offset interval of each sequence number pool, and the start value of the offset interval may be obtained when the sequence number pool is initialized or a new sequence number pool is generated. Further, each sequence number pool may also have a machine number that is used to mark which virtual machine or which container node the sequence number pool owns. And recording the offset interval record acquired by the serial number pool of the virtual machine or the container node corresponding to each machine number, so as to conveniently and quickly locate the virtual machine or the container node on which a serial number is generated when a fault occurs or maintenance is carried out.

For example, if the offset interval obtained from the serial number pool of the container with the machine number 001 is 0 to 100 when the container is started, the offset when the serial number is generated will be automatically filled as: 00001. 00002, 00003 … until 00100 is filled. Since the offset interval obtained from the serial number pool of the 002 # container is 101-200 when the 002 # container is started from the global acquisition interval, the offset amount is automatically filled to 000101, 000102 and 000103 … until 00200 when the serial number is generated. At this time, the offset interval obtained again for container number 001 is: 201-.

In addition, in this embodiment, the servers may be in a cluster mode, and in the cluster mode, the machine number of each server is recorded in the memory and the database, where a server serial number and a network card MAC address are also recorded. If a node is dynamically expanded in the cluster mode, comparing whether the node is the existing record in the memory and the database, if not, re-allocating a new machine number for the dynamically expanded node and storing the new machine number, thereby ensuring the non-stop dynamic generation of the machine number in the cluster mode.

Correspondingly, assembling the identifier and the offset of the serial number pool into the serial number according to the preset rule in S203 may include:

sequentially splicing the identifier and the offset of the serial number pool and the machine number of the node where the serial number pool is located into a serial number;

the machine number of the node where the serial number pool is located corresponds to the offset interval of the serial number pool, and the offset interval of the serial number pool is not overlapped with the offset intervals of other serial number pools except the serial number pool.

For example, if the serial number pool is identified as 20190707221123234, the offset is 23457, and the machine number is 001, then the generated serial number is 2019070722112323423457001. The serial number generated in the embodiment not only guarantees the uniqueness of the serial number through the identification of the serial number pool and the uniqueness of the offset, but also improves the efficiency of problem location through the machine number carried in the serial number.

In practical application, an application requester requesting a serial number generation service may need to generate a custom serial number according to its own needs, and in order to implement a user-defined function, in S201, obtaining an identifier of a serial number pool may include: and determining the user-defined code as the identification of the serial number pool.

For example, a user-defined code can be configured in the custom configuration table as an identifier of the serial number pool, for example, the first five fixed numerical values of the school number are used as the user-defined code, and a value range of the offset can be configured, for example, the number of students is 1000, and the value range of the offset is 0001-1000, so that when the serial number is generated, the ascending operation can be automatically performed in the value range of the offset, the current latest sequence can be read in each ascending operation, then the ascending operation is performed on the specified number according to the rule, and the ascending uniqueness is ensured by using a thread safety cable mechanism. This allows 1000 serial numbers to be generated as student numbers, each unique.

Further, determining the user-defined code as an identifier of the serial number pool may further include:

executing a preset script to obtain a dynamic code defined by a user; the dynamic code is determined as an identification of a pool of sequence numbers.

The user-defined serial number may be dynamically loaded when the serial number is generated, for example, the user-defined code is a region code, a country code, or the like, in addition to the fixed code, and when the serial number is generated, the program execution script reads the current user-defined code.

On the basis of the above embodiment, assembling the identifier and the offset of the serial number pool into the serial number according to the preset rule in S203 includes:

and splicing the preset prefix and/or the preset suffix, the identification of the serial number pool and the offset into the serial number.

For example, an application requester using the serial number generation service needs to generate a serial number with its own flag, and assuming that the service abbreviation used by the application requester for the serial number is abc, and the application requester needs to embody the abbreviation in the serial number, the sequence number obtained by splicing abc as a prefix or a suffix can be spliced in the serial number, for example, the serial number pool is identified as 20190707221123234, and the offset is 23457, and then the sequence number obtained by splicing abc as a prefix is abc 2019070722112323423457.

The method can add prefix and/or suffix in the serial number according to the actual requirement of the serial number requester, thereby improving the practicability and diversity of the serial number.

Fig. 4 is a schematic structural diagram of a serial number generation apparatus provided in the present invention. As shown in fig. 4, the serial number generation apparatus 40 includes:

an obtaining module 401, configured to obtain an identifier of a serial number pool, where the identifier is used to uniquely mark the serial number pool;

a generating module 402, configured to sequentially generate offsets according to the current sequence position of the sequence number pool;

an assembling module 403, configured to assemble the identifier of the serial number pool and the offset into a serial number according to a preset rule.

Optionally, the serial number generating device 40 further includes:

a selecting module 404, configured to select a sequence number pool from at least one target sequence number pool; the current sequence position of the sequence number pool is locked.

Optionally, the generating module 402 is configured to:

and generating the offset in sequence according to the current sequence position of the sequence number pool in the offset interval which is pre-allocated to the sequence number pool.

Optionally, the splicing module 403 is configured to:

splicing the identifier of the serial number pool, the offset and the machine number of the node where the serial number pool is located into serial numbers in sequence;

the machine number of the node where the serial number pool is located corresponds to the offset interval of the serial number pool, and the offset interval of the serial number pool is not overlapped with the offset intervals of other serial number pools except the serial number pool.

Optionally, the obtaining module 401 is configured to:

and determining the user-defined code as the identification of the serial number pool.

Optionally, the obtaining module 401 is configured to:

executing a preset script to obtain a dynamic code defined by a user;

the dynamic code is determined as an identification of a pool of sequence numbers.

Optionally, the splicing module 403 is configured to:

and splicing the preset prefix and/or the preset suffix, the identification of the serial number pool and the offset into a serial number.

The serial number generation apparatus provided in this embodiment may be configured to execute the serial number generation method in any of the above method embodiments, and the implementation principle and the technical effect are similar, which are not described herein again.

Fig. 5 is a schematic structural diagram of a server according to the present invention. As shown in fig. 5, the server 50 includes a memory 501 and a processor 502; the memory 501 is connected to the processor 502;

the memory 501 is used for storing computer programs;

the processor 502 is configured to implement the serial number generation method in any of the above method embodiments when the computer program is executed.

In a fourth aspect, the present invention provides a storage medium having stored thereon a computer program which, when executed by a processor, implements a method of generating a serial number as in any of the above-described method embodiments.

In the embodiments provided in the present invention, it should be understood that the disclosed apparatus and method may be implemented in other ways. For example, the above-described embodiments of the apparatus are merely illustrative, and for example, the division of modules is only one logical division, and other divisions may be realized in practice, for example, a plurality of modules may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or modules, and may be in an electrical, mechanical or other form.

Modules described as separate parts may or may not be physically separate, and parts displayed as modules may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of the present embodiment.

In addition, functional modules in the embodiments of the present invention may be integrated into one processing unit, or each module may exist alone physically, or two or more modules are integrated into one unit. The unit formed by the modules can be realized in a hardware form, and can also be realized in a form of hardware and a software functional unit.

The integrated module implemented in the form of a software functional module may be stored in a computer-readable storage medium. The software functional module is stored in a storage medium and includes several instructions to enable a computer device (which may be a personal computer, a server, or a network device) or a processor (processor) to execute some steps of the methods according to the embodiments of the present application.

It should be understood that the Processor may be a Central Processing Unit (CPU), other general purpose Processor, a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), etc. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like. The steps of a method disclosed in connection with the present invention may be embodied directly in a hardware processor, or in a combination of the hardware and software modules within the processor.

The memory may comprise a high-speed RAM memory, and may further comprise a non-volatile storage NVM, such as at least one disk memory, and may also be a usb disk, a removable hard disk, a read-only memory, a magnetic or optical disk, etc.

The bus may be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, an Extended ISA (EISA) bus, or the like. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, the buses in the figures of the present application are not limited to only one bus or one type of bus.

The computer-readable storage medium may be implemented by any type or combination of volatile or non-volatile memory devices, such as Static Random Access Memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic or optical disks. A storage media may be any available media that can be accessed by a general purpose or special purpose computer.

Those of ordinary skill in the art will understand that: all or a portion of the steps of implementing the above-described method embodiments may be performed by hardware associated with program instructions. The program may be stored in a computer-readable storage medium. When executed, the program performs steps comprising the method embodiments described above; and the aforementioned storage medium includes: various media that can store program codes, such as ROM, RAM, magnetic or optical disks.

Finally, it should be noted that: the above embodiments are only used to illustrate the technical solution of the present invention, and not to limit the same; while the invention has been described in detail and with reference to the foregoing embodiments, it will be understood by those skilled in the art that: it is also possible to modify the solutions described in the previous embodiments or to substitute some or all of them with equivalents.

12页详细技术资料下载
上一篇:一种医用注射器针头装配设备
下一篇:一种文本规整方法、装置、设备及存储介质

网友询问留言

已有0条留言

还没有人留言评论。精彩留言会获得点赞!

精彩留言,会给你点赞!