Service identifier generation method and device, electronic equipment and storage medium
By dynamically calculating the identifier range in a distributed cluster and generating unique identifiers by itself, the problems of low identification code generation efficiency and insufficient system robustness in the prior art are solved, and efficient and low-dependence identifier generation is achieved, which satisfies the Parallel characteristics and resource utilization of distributed systems.
Patent Information
- Application Number
- CN202411170795.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-23
- Publication Date
- 2024-12-24
- Estimated Expiration
- 2044-08-23
AI Technical Summary
When generating global unique identification codes in distributed clusters, the problem is that the request process takes a long time and waits for a long time, and the identification code generation efficiency is low, which affects the robustness of the system, especially in scenarios where identification code resources are tight and high concurrency are tight. Under the simplified development of distributed systems, the advantages of distributed systems cannot be fully utilized.
By sending a request to the service management center when the service is started or when the service changes are received, obtaining the number and sorting position of the same type of services, dynamically calculate the start value and number threshold of the identifier range, and generate a unique identifier by itself. Thread locks ensure uniqueness and avoid resource waste and dependencies.
It realizes efficient generation of unique identifiers in distributed systems, reduces request time and waiting time, fully utilizes the characteristics of distributed parallelism, improves system efficiency and robustness, avoids resource waste, and meets high concurrency and flexible expansion. need.
Smart Images

Figure CN119182642A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and device for generating a service identifier, an electronic device, and a storage medium. Background Art
[0002] In a distributed cluster, how to enable concurrent programs to generate globally unique identification codes is an inevitable problem. There are two common methods:
[0003] Method 1: Use redis, self-developed and other strong consistency central data processing services to achieve: when the service needs an ID, request it and use it after successful acquisition.
[0004] Method 2: Assign a short unique identification code A to each service in advance, and generate a long unique identification code C = A + custom part B according to unified rules.
[0005] However, the above methods are not applicable. The identification code generation method of method 1 determines that all places where it is used must request the identification code from the same service, and the identification code is generated serially and cannot be generated in parallel. This results in a time-consuming request process and a long waiting time, and the advantages of distributed concurrency cannot be brought into play; and once the central data processing service fails, the unique identifier cannot be generated, which will affect the robustness of the system. Therefore, it is only applicable to scenarios where the frequency of identifier generation is low and the generation efficiency is not required. Method 2 is applicable when the length of the unique identification code is not limited or is sufficient. However, if the length is limited and the number of identification codes is limited, the length of the identification code A limits the number of services (it cannot exceed the maximum value of all representations of A), which leads to a waste of identification codes (when the number of services is less than the maximum value represented by A). Therefore, this method is not applicable when the identification code resources are relatively tight and the number of services can be expanded or reduced.
[0006] For example, the ID data type BusinessObjectIdType in GAT1400 (public security industry standard GA / T1400) has a length of 33 bits, the first 14 bits are fixed according to the system and type, 15 to 28 are 14-bit time strings accurate to seconds (20240722151406), and only the last 5 bits 29 to 33 are customized, which means that the system generates a maximum of 100,000 0000 to 99999 per second. In this case, using method 1 will severely limit the efficiency of the integer system; using method 2, the identification code A reserved for the service is assumed to be one bit, then the customized part B is 4 bits, and assuming that the cluster currently has 5 services, then the number of services in the entire cluster is limited to 10 or less, and only 5*10000=50,000 unique identifiers can be generated in 1 second, limiting the maximum concurrency of the system (lower than the theoretical concurrency of 100,000). Summary of the invention
[0007] In view of the above problems, the present invention proposes a method and device for generating a service identifier, an electronic device, and a storage medium.
[0008] In a first aspect of the present invention, a method for generating a service identifier is first provided, which is applied to a distributed cluster, wherein at least one service is deployed in the distributed cluster, and the service has a corresponding service type, wherein the method comprises:
[0009] For any of the services, when the service is started or receives a service change notification of the same service type, the service sends a service change request to the service management center; the service management center is used to receive the service change request and filter out all services of the same type according to the service type of the service;
[0010] The service receives the number of services of the same type and the sorting position returned by the service management center; wherein the sorting position is obtained by the service management center sorting the services according to a preset sorting strategy;
[0011] The service calculates the start value of the identifier range and the identifier number threshold according to the number of services of the same type, the sorting position and the rule identifier range obtained;
[0012] The service generates a unique identifier within the time interval to which the current time belongs according to the start value of the identifier range and the identifier number threshold.
[0013] Optionally, for any of the services, when the service is started or receives a service change notification of the same service type, the service sends a service change request to a service management center; the service management center is used to receive the service change request and filter out all services of the same type according to the service type of the service, including:
[0014] When the service is started for the first time, a request is made to the service management center to register the service information; the service management center is used to record the service information and filter out all services of the same type according to the service type recorded in the service information;
[0015] or,
[0016] When the service receives a service change notification of the same service type, it sends a service change request to the service management center; the service management center is used to receive the service change request and filter out all services of the same type in real time according to the service type of the service.
[0017] Optionally, the service receives the number of services of the same type and the sorting position returned by the service management center, including:
[0018] The service requests the service management center to obtain the number of services of the same type; the service management center is used to receive the request of the service to obtain the number of services of the same type, and return the number of all services of the same type filtered according to the service type of the service;
[0019] After obtaining the number of services of the same type returned by the service management center, the service requests the service management center to obtain the sorting position; the service management center is used to receive the request for the service to obtain the sorting position, and return the sorting position obtained by sorting the service according to a preset sorting strategy.
[0020] Optionally, the start value of the service identifier range is obtained by dividing the rule identifier range by the number of services of the same type and rounding down;
[0021] The identifier number threshold of the service identifier range is obtained by dividing the rule identifier range by the number of services of the same type, rounding down, and multiplying by the sorting position.
[0022] Optionally, the service generates a unique identifier within a time interval to which the current time belongs according to a start value of the identifier range and a threshold value of the number of identifiers, including:
[0023] Set the time interval corresponding to the last unique identifier generation and the sequence number when the last unique identifier was generated as the initial value;
[0024] Obtaining the current time of the service, and determining the time interval to which the current time of the service belongs according to the current time of the service;
[0025] Determine whether the time interval to which the current time of the service belongs is consistent with the time interval corresponding to the last time the unique identifier was generated;
[0026] If the time interval to which the current time of the service belongs is consistent with the time interval corresponding to the last generation of the unique identifier, determining whether the sequence number when the unique identifier was generated last time is less than the identifier number threshold of the service;
[0027] If the sequence number of the last unique identifier generated is greater than or equal to the identifier number threshold of the service, then the sleep time from the current time of the service to the start position of the next time interval is calculated;
[0028] After the service has been asleep for a certain period of time, the service is awakened and the current time of the service is repeatedly acquired, and the time interval to which the current time of the service belongs is determined according to the current time of the service;
[0029] If the sequence number of the last generated unique identifier is less than the identifier number threshold of the service, update the sequence number of the last generated unique identifier, increase the sequence number of the last generated unique identifier by a preset value, and obtain a new sequence number of the generated unique identifier;
[0030] If the time interval to which the current service time belongs is inconsistent with the time interval corresponding to the last generation of the unique identifier, the time interval corresponding to the last generation of the unique identifier is updated to the time interval to which the current service time belongs, and the sequence number when the unique identifier was generated last time is reset to the initial value to obtain a new sequence number when the unique identifier is generated;
[0031] The unique identifier of the service is the sum of the fixed part, the sequence number when the new unique identifier is generated, and the start value of the service identifier range.
[0032] Optionally, before the service generates a unique identifier within the time interval to which the current time belongs, according to the start value of the identifier range and the identifier number threshold, it further includes:
[0033] Add a thread lock to the process of generating a unique identifier for the service.
[0034] The service generates a unique identifier under the protection of the thread lock.
[0035] Optionally, adding a thread lock to the process of generating a unique identifier for the service includes:
[0036] Creating the thread lock;
[0037] Before the service generates a unique identifier, acquiring the thread lock;
[0038] After the service generates a unique identifier, the thread lock is released.
[0039] In a second aspect of the present invention, a device for generating a service identifier is provided, the device comprising:
[0040] A request module, for any of the services, when the service is started or receives a service change notification of the same service type, the service sends a service change request to the service management center; the service management center is used to receive the service change request and filter out all services of the same type according to the service type of the service;
[0041] A receiving module, used for the service to receive the number and sorting position of services of the same type returned by the service management center; wherein the sorting position is obtained by the service management center sorting the services according to a preset sorting strategy;
[0042] A calculation module, used for the service to calculate the start value of the identifier range and the identifier number threshold according to the number of services of the same type, the sorting position and the rule identifier range obtained;
[0043] The generating module is used to generate a unique identifier within the time interval to which the current time belongs, according to the starting value of the identifier range and the identifier number threshold.
[0044] In the third aspect of the implementation of the present invention, there is also provided an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, a method for generating a service identifier as described above is implemented.
[0045] In a fourth aspect of the implementation of the present invention, there is also provided a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the method for generating a service identifier as described above is implemented.
[0046] The embodiments of the present invention have the following advantages:
[0047] In the embodiment of the present invention, for any service, when the service is started or receives a service change notification of the same service type, the service sends a service change request to the service management center; the service management center is used to receive the service change request and filter out all services of the same type according to the service type of the service; the service receives the number of services of the same type and the sorting position returned by the service management center; wherein the sorting position is obtained by the service management center sorting the services according to the preset sorting strategy; the service calculates the start value of the identifier range and the identifier number threshold according to the number of services of the same type, the sorting position and the rule identifier range obtained; the service generates a unique identifier according to the start value of the identifier range and the identifier number threshold within the time interval to which the current time belongs. The dynamically adjusted identifier range can not only make the number of services unlimited, give full play to the characteristics of distributed flexible expansion and contraction, but also make full use of the identifier, avoid resource waste, and enable the system to reach its theoretical maximum value; the distributed and non-central generation method simplifies the process of obtaining the identifier, saves the request time and waiting time, gives full play to the parallel characteristics of the distributed, and improves the efficiency; generates a unique identifier in the required service, reduces the dependence between services, and improves the robustness of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art are briefly introduced below.
[0049] Figure 1 It is a flowchart of a method for generating a service identifier provided by an embodiment of the present invention;
[0050] Figure 2 is a flowchart of a process for generating an identifier range provided by an embodiment of the present invention;
[0051] Figure 3 is a flowchart of a process for generating an identifier provided by an embodiment of the present invention;
[0052] Figure 4 It is a structural block diagram of a device for generating a service identifier provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0053] To make the purpose, technical scheme and advantages of the embodiments of the present invention clearer, the embodiments of the present invention will be described in detail below in conjunction with the accompanying drawings. However, it will be appreciated by those skilled in the art that in the embodiments of the present invention, many technical details are proposed in order to enable the reader to better understand the present application. However, even without these technical details and various changes and modifications based on the following embodiments, the technical scheme claimed in the present application can be implemented. The division of the following embodiments is for the convenience of description, and should not constitute any limitation on the specific implementation of the present invention. The various embodiments can be combined and referenced with each other without contradiction.
[0054] In the embodiment of the present invention, the core concept is that when the service is started and the same type of services are found to increase or decrease, the latest number of services and the ranking position of the current service are obtained, and the range of identification codes available for the current service is dynamically calculated based on the total number of services and the ranking position of the current service; when the current service needs to use a unique identification code, a unique identification code is automatically generated according to the agreed rules. There is no limit on the number of services, no waste of identifiers, and self-generation is more efficient and less dependent.
[0055] Reference Figure 1 , shows a flow chart of a method for generating a service identifier provided by an embodiment of the present invention, which may specifically include the following steps:
[0056] Step 101, for any of the services, when the service is started or receives a service change notification of the same service type, the service sends a service change request to the service management center; the service management center is used to receive the service change request and filter out all services of the same type according to the service type of the service.
[0057] Among them, the same type of services refer to service processes in a distributed cluster that have the same functions and roles and can replace each other.
[0058] In an embodiment of the present invention, when any service is started for the first time, it will request to register its own information, including service type, service name, IP address, port number, etc., to the service management center (such as a registration service, a service configuration database, etc.). The service management center will receive the registration request, record the registration information of the service and filter out all services of the same type based on the service type in this information.
[0059] When any service receives a service change notification from a service of the same type (for example, an increase or decrease in the number of services of the same type), it will send a service change request to the service management center. The service management center will receive the service change request and filter out all services of the same type in real time according to the service type of the service, and notify the relevant services to update the number of services of the same type in their local cache. Specifically, the service can obtain the update of the number of services of the same type in real time through polling or subscription mechanism.
[0060] Step 102, the service receives the number of services of the same type and the sorting position returned by the service management center; wherein the sorting position is obtained by the service management center sorting the services according to a preset sorting strategy.
[0061] In the embodiment of the present invention, the service will request the service management center to obtain the number of services of the same type. After receiving the request, the service management center will return to the service the number of all services of the same type screened according to the service type of the service.
[0062] After obtaining the number of services of the same type returned by the service management center, the service needs to request its own ranking position from the service management center.
[0063] Specifically, when a service is started for the first time, the service requests the service management center (such as the service registration center, configuration database, etc.) to register its own information. The service management center receives the registration request, records the registration information of the service, and sorts the service according to the preset sorting strategy. After the service is registered, it will request the service management center to obtain its own sorting position. After receiving the request, the service management center will return the sorting position obtained by sorting the service according to the preset sorting strategy. Among them, the sorting strategy can be a simple lexicographic order (such as sorting by service name or IP address) or a complex logic (such as sorting by service load, health status, etc.). The sorting strategy should have a clear agreement between the service management center and the service to ensure consistency and correctness.
[0064] When a service receives a service change notification from a service of the same type (for example, an increase or decrease in the number of services of the same type), it sends a service change request to the service management center, which recalculates the ranking position of the service and notifies related services to update their ranking positions. Services can obtain updates to their ranking positions in real time through polling or subscription mechanisms.
[0065] Step 103: The service calculates a start value of an identifier range and an identifier number threshold according to the acquired number of services of the same type, the sorting position, and the rule identifier range.
[0066] In the embodiment of the present invention, the calculation formula of the identifier number threshold of the service identifier range, that is, the maximum number maxNum, is:
[0067]
[0068] in, Indicates rounding down, maxNum indicates the maximum number of the service identifier range, D indicates the rule identifier range assumed based on business requirements, and A indicates the number of services of the same type.
[0069] The calculation formula for the start value baseNum of the service identifier range is:
[0070]
[0071] in, Indicates rounding down, baseNum indicates the starting value of the service identifier range, D indicates the rule identifier range assumed based on business requirements, A indicates the number of services of the same type, and B indicates the sorting position of the service.
[0072] Step 104: the service generates a unique identifier within the time interval to which the current time belongs according to the start value of the identifier range and the identifier number threshold.
[0073] In the embodiment of the present invention, before generating a unique identifier, firstly, the time interval corresponding to the last generation of the unique identifier is set as the initial value, ie, lastTime=0, and the sequence number when the unique identifier was last generated is set as the initial value, ie, lastNum=0.
[0074] Specifically, the implementation process of generating a unique identifier is as follows:
[0075] 1. Add a thread lock to the process of generating a unique identifier.
[0076] Specifically, a thread lock is first created, and then before the service generates a unique identifier, the thread lock is acquired so that the service generates a unique identifier under the protection of the thread lock, thereby ensuring the uniqueness of the generated identifier.
[0077] 2. Get the current time, and then determine the time interval time to which the current time belongs based on the current time.
[0078] Specifically, first determine the size of the time interval. For example, if the time interval is every hour (3600 seconds), each time interval is 1 hour. Then divide the current time by the time interval and round it to the integer to get the start time of the time interval to which the current time belongs.
[0079] 3. Determine whether the current time interval time is consistent with lastTime. If they are consistent, proceed to step 4; if they are inconsistent, update the record value lastNum=0, lastTime=time, and jump to step 6.
[0080] Specifically, the time interval is determined to ensure that the generated identifier is unique and ordered in each time interval. By determining whether time is consistent with lastTime, it can be ensured that the identifier generated in each time interval is unique. If the current time has entered the next time interval, the sequence number needs to be reset to avoid duplicate identifiers in different time intervals.
[0081] 4. Determine whether lastNum is less than maxNum. If not, proceed to step 5; if yes, update the record value lastNum=lastNum+1; jump to step 6.
[0082] Specifically, the purpose of determining the sequence number range is to ensure that the number of identifiers generated in each time interval does not exceed the preset maximum value maxNum. By setting the maximum number of identifiers allowed to be generated in each time interval (maxNum), sequence number overflow can be prevented. If the sequence number reaches the maximum value, the service will no longer generate new identifiers, thereby avoiding the sequence number from exceeding the range; by limiting the number of identifiers generated in each time interval, the generation rate of identifiers can be controlled, which is particularly important in high-concurrency scenarios, which can avoid system overload and ensure the stable operation of the system; in the same time interval, by increasing the sequence number to generate identifiers, it can ensure that the generated identifiers are unique. If the sequence number reaches the maximum value, the service will wait for the next time interval to continue generating identifiers, thereby ensuring the uniqueness of each identifier. Different business scenarios may have different requirements for the generation of identifiers. By flexibly setting the sequence number range, different business requirements can be met, such as fast generation in high-concurrency scenarios, stable generation in low-frequency scenarios, etc.
[0083] 5. Calculate the time from the current time to the starting position of the next time interval sleepTime = next time - current time, the service sleeps for sleepTime, and jumps to step 2 after waking up.
[0084] Specifically, when the number of identifiers generated in the current time interval reaches the maximum, the service needs to wait for the start of the next time interval to continue generating new identifiers. Calculating the sleep time is to determine the length of time required to wait so that the service automatically wakes up and continues to generate identifiers at the start of the next time interval.
[0085] 6. Generate a unique identification code ID = lastNum + baseNum, where the unique identifier is the fixed part + ID.
[0086] Specifically, the fixed part is fixed according to business requirements.
[0087] 7. Release the thread lock and end.
[0088] Through the above operations, the identifier range is dynamically adjusted, which can not only make the number of services unlimited and give full play to the characteristics of distributed flexible expansion and contraction, but also make full use of identifiers to avoid waste of resources and enable the system to reach its theoretical maximum value; at the same time, the distributed rather than centralized generation method simplifies the process of obtaining identifiers, saves request time and waiting time, gives full play to the parallel characteristics of distribution, and improves efficiency; in addition, generating identifiers in the required services reduces the dependence between services and improves the robustness of the system.
[0089] When generating a unique identifier, you can also use the current timestamp (such as a millisecond or microsecond timestamp) plus a locally incremented serial number to generate an identifier based on a timestamp and an incrementing sequence. This is not only simple and efficient, but also does not rely on an external system. At the same time, when the service starts, the serial number is initialized, and when a notification of service increase or decrease is received, the starting value of the serial number is adjusted to avoid conflicts with identifiers generated by other services.
[0090] Furthermore, in the embodiment of the present invention, step 101 further includes the following steps:
[0091] S1011, when the service is started for the first time, requesting the service management center to register the service information; the service management center is used to record the service information and filter out all services of the same type according to the service type recorded in the service information;
[0092] or,
[0093] When the service receives a service change notification of the same service type, it sends a service change request to the service management center; the service management center is used to receive the service change request and filter out all services of the same type in real time according to the service type of the service.
[0094] Specifically, when any new service is started, it needs to register with the service management center. The service sends a registration request to the service management center through a communication protocol (such as HTTP (Hypertext Transfer Protocol), RPC (Remote Procedure Call), etc.). This request usually contains basic information about the service, such as service name, service type, service address, port number, version number, etc. After receiving the registration request, the service management center will record the service information in its internal data storage system, such as a database, memory cache, etc. The service management center filters out all services of the same type in its recorded service list based on the service type recorded in the service information. This step is usually for the purpose of service discovery and service load balancing.
[0095] When any service receives a service change notification from a service of the same type (such as an increase or decrease in the number of services of the same type), it will send a service change request to the service management center through a communication protocol (such as HTTP, RPC, etc.). After receiving the service change request, the service management center will update its internally recorded service information and filter out all service instances of the same type in real time based on the service type. This step ensures that the service list in the service management center is always up to date.
[0096] Furthermore, in the embodiment of the present invention, step 102 further includes the following steps:
[0097] S1021, the service requests the service management center to obtain the number of services of the same type; the service management center is used to receive the request from the service to obtain the number of services of the same type, and return the number of all services of the same type filtered according to the service type of the service.
[0098] Specifically, the service needs to know the number of services of the same type in the current system in order to perform load balancing, service discovery or other business logic processing. Therefore, it will send a request to the service management center to obtain the number of services of the same type. After receiving this request, the service management center will filter in its internal service registry according to the service type information contained in the request. It will find all registered services of the same type, count their number, and return the number of filtered services of the same type as a response to the requesting service.
[0099] S1022, after the service obtains the number of services of the same type returned by the service management center, the service requests the service management center to obtain the sorting position; the service management center is used to receive the request for the service to obtain the sorting position, and return the sorting position obtained by sorting the service according to a preset sorting strategy.
[0100] Specifically, after the service obtains the number of services of the same type from the service management center, the service may need to know its ranking position among all services of the same type in order to perform load balancing, service scheduling or other business logic processing. Therefore, it will send a request to the service management center to request its own ranking position. After receiving this request, the service management center will sort all services of the same type according to the preset sorting strategy, and return the sorting position of the requested service as a response to the service. Among them, the preset sorting strategy can be a simple lexicographic order (such as sorting by service name or IP address) or a complex logic (such as sorting by service load, health status, etc.). The sorting strategy should have a clear agreement between the service management center and the service to ensure consistency and correctness.
[0101] Furthermore, in the embodiment of the present invention, step 104 further includes the following steps:
[0102] S1041, setting the time interval corresponding to the last generation of the unique identifier and the sequence number when the unique identifier was generated last time as initial values.
[0103] In the embodiment of the present invention, lastTime=0 is set, that is, the time interval corresponding to the last generation of the unique identifier is 0; lastNum=0, the time interval corresponding to the last generation of the unique identifier is 0.
[0104] S1042, obtaining the current time of the service, and determining the time interval to which the current time of the service belongs according to the current time of the service.
[0105] In an embodiment of the present invention, the size of the time interval is first determined. For example, if the time interval is every hour (3600 seconds), each time interval is 1 hour. Then the current time is divided by the time interval and then rounded to the integer to obtain the start time of the time interval to which the current time belongs, that is, the time interval time to which the current time of the service belongs.
[0106] S1043, determining whether the time interval to which the current service time belongs is consistent with the time interval corresponding to the last time the unique identifier was generated.
[0107] In the embodiment of the present invention, it is determined whether time is consistent with lastTime. By determining whether time is consistent with lastTime, it can be ensured that the identifier generated in each time interval is unique.
[0108] S1044: If the time interval to which the current time of the service belongs is consistent with the time interval corresponding to the last generation of the unique identifier, determine whether the sequence number when the unique identifier was generated last time is less than the identifier number threshold of the service.
[0109] In the embodiment of the present invention, if time is consistent with lastTime, it is necessary to further determine whether lastNum is less than maxNum.
[0110] S1045: If the sequence number of the last unique identifier generated is greater than or equal to the identifier number threshold of the service, calculate the sleep time from the current time of the service to the start position of the next time interval.
[0111] In the embodiment of the present invention, if lastNum is greater than or equal to maxNum, it means that the sequence number has reached the maximum value, and the number of identifiers generated in the current time interval has reached the maximum value, and the service will no longer generate new identifiers to avoid the sequence number being out of range. At this time, it is necessary to wait for the start of the next time interval to continue to generate new identifiers. Calculating the sleep time is to determine the length of time required to wait so that the service automatically wakes up at the start of the next time interval and continues to generate identifiers.
[0112] S1046, after the service has been asleep for a certain period of time, the service is awakened and the process of repeatedly obtaining the current time of the service is repeated, and the time interval to which the current time of the service belongs is determined according to the current time of the service.
[0113] In the embodiment of the present invention, the service automatically wakes up and repeatedly executes S1042 after the sleep time has elapsed.
[0114] S1047, if the sequence number of the last generated unique identifier is less than the identifier number threshold of the service, update the sequence number of the last generated unique identifier, increase the sequence number of the last generated unique identifier by a preset value, and obtain a new sequence number of the generated unique identifier.
[0115] In the embodiment of the present invention, if lastNum is less than maxNum, it means that the sequence number has not reached the maximum value, and a new identifier can be generated, and lastNum=lastNum+1 is updated to obtain a new sequence number when generating a unique identifier. In the same time interval, by generating identifiers by increasing the sequence number, it can be ensured that the generated identifiers are ordered, which not only facilitates management and query, but also improves the maintainability of system services.
[0116] S1048, if the time interval to which the current service time belongs is inconsistent with the time interval corresponding to the last time the unique identifier was generated, update the time interval corresponding to the last time the unique identifier was generated to the time interval to which the current service time belongs, reset the serial number when the unique identifier was generated last time to the initial value, and obtain a new serial number when the unique identifier was generated.
[0117] In the embodiment of the present invention, if time is inconsistent with lastTime, it means that the current time has entered the next time interval, and the sequence number needs to be reset to avoid duplicate identifiers in different time intervals. Specifically, lastNum=0 and lastTime=time are updated.
[0118] S1049: The unique identifier of the service is the sum of the fixed part, the sequence number when the new unique identifier is generated, and the start value of the service identifier range.
[0119] In the embodiment of the present invention, the unique identifier of the service = fixed part + lastNum + baseNum, wherein the fixed part is a part fixed according to business requirements, and lastNum is a serial number after update or reset.
[0120] Furthermore, in the embodiment of the present invention, the following steps are also included before step 104:
[0121] S1, adding a thread lock to the process of generating a unique identifier for the service.
[0122] Specifically, before the service performs the process of generating a unique identifier, a thread lock is added.
[0123] S2, the service generates a unique identifier under the protection of the thread lock.
[0124] Specifically, after the thread lock is added, the service starts the process of generating the unique identifier, so that the process of generating the service unique identifier is under the protection of the thread lock.
[0125] Through the above operations, a thread lock is added to the process of generating a unique identifier for the service, thereby ensuring the uniqueness of the generated identifier.
[0126] Furthermore, in the embodiment of the present invention, S1 further includes the following steps:
[0127] S1-1, creating the thread lock.
[0128] Specifically, different programming languages use different methods to create thread locks. For example, in Python, you can use the threading module to create a thread lock; in Java, you can use the Lock interface in the java.util.concurrent.locks package to create a thread lock; in C++, you can use std::mutex to create a thread lock, etc.
[0129] S1-2, before the service generates a unique identifier, acquiring the thread lock.
[0130] S1-3, after the service generates a unique identifier, releasing the thread lock.
[0131] Further, in an embodiment of the present invention, if Figure 2 As shown, a process flow chart for generating identifier ranges is provided. The specific process is as follows:
[0132] 1. Obtain the number A of services of the same type, and obtain the number A of services of the same type stored in the system from the service management center of the distributed cluster (such as registration service, service configuration database, etc.);
[0133] 2. Obtain the fixed sorting position B of this service, and obtain the sorting position B of the current service from the service management center of the distributed cluster (such as registration service, service configuration database, etc.) according to the agreed sorting strategy;
[0134] 3. Calculate the service identifier range, calculate the starting value baseNum of the current service identifier range, and the maximum number maxNum. Assuming that the rule identifier range is D according to business requirements, the calculation formula is as follows:
[0135]
[0136] Through the above operations, the dynamically adjusted identifier range can not only make the number of services unlimited and give full play to the characteristics of distributed flexible expansion and contraction, but also make full use of the identification code to avoid waste of resources, so that the system can reach its theoretical maximum value.
[0137] Further, in an embodiment of the present invention, if Figure 3 As shown, a flowchart of the identifier generation process is provided. The specific process is as follows:
[0138] 1. Get the current time, and determine the current time interval to which the current time belongs according to the current time;
[0139] 2. Determine whether the current time interval time is the same as the time interval lastTime corresponding to the last time the unique identifier was generated;
[0140] 3. If yes, continue to determine whether the last generated identifier serial number lastNum is less than the calculated maximum number maxNum of the service identifier range; if not, update the record value lastNum to 0, and lastTime = time;
[0141] 4. If the last generated identifier sequence number lastNum is less than the calculated maximum number maxNum of the service identifier range, update lastNum=lastNum+1;
[0142] 5. If the last generated identifier sequence number lastNum is not less than the calculated maximum number maxNum of the service identifier range, the sleep time from the current time to the start of the next time interval is calculated. After the service has slept for a certain period of time, it automatically wakes up and repeats step 1.
[0143] 6. The generated unique identifier = lastNum + baseNum + fixed part, where lastNum is the serial number of the newly generated identifier after the update.
[0144] Through the above operations, according to the identifier range of the service, when the service needs to use a unique identifier, a unique identifier is generated by itself according to the agreed rules, which is more efficient.
[0145] In the embodiment of the present invention, for any service, when the service is started or receives a service change notification of the same service type, the service sends a service change request to the service management center; the service management center is used to receive the service change request and filter out all services of the same type according to the service type of the service; the service receives the number of services of the same type and the sorting position returned by the service management center; wherein the sorting position is obtained by the service management center sorting the services according to the preset sorting strategy; the service calculates the start value of the identifier range and the identifier number threshold according to the number of services of the same type, the sorting position and the rule identifier range obtained; the service generates a unique identifier according to the start value of the identifier range and the identifier number threshold within the time interval to which the current time belongs. The dynamically adjusted identifier range can not only make the number of services unlimited, give full play to the characteristics of distributed flexible expansion and contraction, but also make full use of the identifier, avoid resource waste, and enable the system to reach its theoretical maximum value; the distributed and non-central generation method simplifies the process of obtaining the identifier, saves the request time and waiting time, gives full play to the parallel characteristics of the distributed, and improves the efficiency; generates a unique identifier in the required service, reduces the dependence between services, and improves the robustness of the system.
[0146] It should be noted that, for the sake of simplicity, the method embodiments are described as a series of action combinations, but those skilled in the art should be aware that the embodiments of the present invention are not limited by the order of the actions described, because according to the embodiments of the present invention, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of the present invention.
[0147] Reference Figure 4 , shows a schematic diagram of the structure of a device for generating a service identifier provided by an embodiment of the present invention, which may specifically include the following modules:
[0148] The request module 201 is used for sending a service change request to a service management center for any of the services when the service is started or receives a service change notification of the same service type; the service management center is used to receive the service change request and filter out all services of the same type according to the service type of the service;
[0149] Receiving module 202, used for the service to receive the number of services of the same type and the sorting position returned by the service management center; wherein the sorting position is obtained by the service management center sorting the services according to a preset sorting strategy;
[0150] A calculation module 203, used for the service to calculate the start value of the identifier range and the identifier number threshold according to the obtained number of services of the same type, the sorting position and the rule identifier range;
[0151] The generating module 204 is configured to generate a unique identifier within the time interval to which the current time belongs, according to the start value of the identifier range and the identifier number threshold.
[0152] Optionally, the request module 201 includes:
[0153] A module for obtaining the number of services of the same type, used to request the service management center to register the service information when the service is started for the first time; the service management center is used to record the service information and filter out all services of the same type according to the service type recorded in the service information;
[0154] or,
[0155] When the service receives a service change notification of the same service type, it sends a service change request to the service management center; the service management center is used to receive the service change request and filter out all services of the same type in real time according to the service type of the service.
[0156] Optionally, the receiving module 202 includes:
[0157] A module for receiving the number of services of the same type, used for the service to request the service management center to obtain the number of services of the same type; the service management center is used to receive the request of the service to obtain the number of services of the same type, and return the number of all services of the same type filtered according to the service type of the service;
[0158] A sorting position receiving module is used for the service to request the service management center to obtain the sorting position after the service obtains the number of services of the same type returned by the service management center; the service management center is used to receive the request for the service to obtain the sorting position, and return the sorting position obtained by sorting the service according to a preset sorting strategy.
[0159] Optionally, the generating module 204 includes:
[0160] A setting module, used to set the time interval corresponding to the last generation of the unique identifier and the sequence number when the unique identifier was generated last time as initial values;
[0161] A time interval acquisition module, used to acquire the current time of the service and determine the time interval to which the current time of the service belongs according to the current time of the service;
[0162] A first determination module, used to determine whether the time interval to which the current time of the service belongs is consistent with the time interval corresponding to the last time the unique identifier was generated;
[0163] A second judgment module is used to judge whether the sequence number when the unique identifier was generated last time is less than the identifier number threshold of the service if the time interval to which the current time of the service belongs is consistent with the time interval corresponding to the last time the unique identifier was generated;
[0164] A sleep time calculation module, configured to calculate the sleep time from the current time of the service to the start position of the next time interval if the sequence number when the unique identifier was generated last time is greater than or equal to the identifier number threshold of the service;
[0165] A wake-up module, used for waking up the service after the service has been asleep for a certain period of time and repeatedly obtaining the current time of the service, and determining the time interval to which the current time of the service belongs according to the current time of the service;
[0166] A first updating module, configured to update the sequence number of the last generated unique identifier if the sequence number of the last generated unique identifier is less than the identifier number threshold of the service, and increase the sequence number of the last generated unique identifier by a preset value to obtain a new sequence number of the last generated unique identifier;
[0167] A second updating module is used to update the time interval corresponding to the last time the unique identifier was generated to the time interval to which the service current time belongs if the time interval to which the service current time belongs is inconsistent with the time interval corresponding to the last time the unique identifier was generated to reset the sequence number when the unique identifier was generated last to an initial value to obtain a new sequence number when the unique identifier was generated;
[0168] The identifier generating module is used for the unique identifier of the service, which is the sum of the fixed part, the sequence number when the new unique identifier is generated, and the starting value of the service identifier range.
[0169] In the embodiment of the present invention, for any service, when the service is started or receives a service change notification of the same service type, the service sends a service change request to the service management center; the service management center is used to receive the service change request and filter out all services of the same type according to the service type of the service; the service receives the number of services of the same type and the sorting position returned by the service management center; wherein the sorting position is obtained by the service management center sorting the services according to the preset sorting strategy; the service calculates the start value of the identifier range and the identifier number threshold according to the number of services of the same type, the sorting position and the rule identifier range obtained; the service generates a unique identifier according to the start value of the identifier range and the identifier number threshold within the time interval to which the current time belongs. The dynamically adjusted identifier range can not only make the number of services unlimited, give full play to the characteristics of distributed flexible expansion and contraction, but also make full use of the identifier, avoid resource waste, and enable the system to reach its theoretical maximum value; the distributed and non-central generation method simplifies the process of obtaining the identifier, saves the request time and waiting time, gives full play to the parallel characteristics of the distributed, and improves the efficiency; generates a unique identifier in the required service, reduces the dependence between services, and improves the robustness of the system.
[0170] An embodiment of the present invention further provides an electronic device, which may include a processor, a memory, and a computer program stored in the memory and capable of running on the processor. When the computer program is executed by the processor, the method for generating a service identifier as described above is implemented.
[0171] An embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the method for generating a service identifier as described above is implemented.
[0172] As for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0173] Those skilled in the art will appreciate that the embodiments of the present invention may be provided as methods, devices, or computer program products. Therefore, the embodiments of the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the embodiments of the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.
[0174] The embodiments of the present invention are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0175] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing terminal device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0176] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable terminal device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0177] Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.
[0178] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of further restrictions, the elements defined by the sentence "including one..." do not exclude the existence of other identical elements in the process, method, article or terminal device including the above elements.
[0179] The above is a detailed introduction to a method and device for generating a service identifier, an electronic device, and a storage medium. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. Although the implementation methods of the present invention are described as above, the above description content and definitions are only for the convenience of understanding the implementation methods of the present invention, and are not intended to limit the present invention. Any modifications and changes made without departing from the spirit and scope of the present invention are within the scope of protection of the present invention. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A method for generating a service identifier, applied to a distributed cluster, wherein at least one service is deployed in the distributed cluster, and the service has a corresponding service type, characterized in that: The method comprises: For any of the services, when the service is started or receives a service change notification of the same service type, the service sends a service change request to the service management center; the service management center is used to receive the service change request and filter out all services of the same type according to the service type of the service; The service receives the number of services of the same type and the sorting position returned by the service management center; wherein the sorting position is obtained by the service management center sorting the services according to a preset sorting strategy; The service calculates the start value of the identifier range and the identifier number threshold according to the number of services of the same type, the sorting position and the rule identifier range obtained; The service generates a unique identifier within the time interval to which the current time belongs according to the start value of the identifier range and the identifier number threshold.
2. The method according to claim 1, characterized in that: For any of the services, when the service is started or receives a service change notification of the same service type, the service sends a service change request to the service management center; The service management center is used to receive the service change request and filter out all services of the same type according to the service type of the service, including: When the service is started for the first time, a request is made to the service management center to register the service information; the service management center is used to record the service information and filter out all services of the same type according to the service type recorded in the service information; or, When the service receives a service change notification of the same service type, it sends a service change request to the service management center; the service management center is used to receive the service change request and filter out all services of the same type in real time according to the service type of the service.
3. The method according to claim 1 or 2, characterized in that: The service receives the number of services of the same type and the ranking position returned by the service management center, including: The service requests the service management center to obtain the number of services of the same type; the service management center is used to receive the request of the service to obtain the number of services of the same type, and return the number of all services of the same type filtered according to the service type of the service; After obtaining the number of services of the same type returned by the service management center, the service requests the service management center to obtain the sorting position; the service management center is used to receive the request for the service to obtain the sorting position, and return the sorting position obtained by sorting the service according to a preset sorting strategy.
4. The method according to claim 1, characterized in that The start value of the service identifier range is obtained by dividing the rule identifier range by the number of services of the same type and rounding down; The identifier number threshold of the service identifier range is obtained by dividing the rule identifier range by the number of services of the same type, rounding down, and multiplying by the sorting position.
5. The method according to claim 1 or 4, characterized in that: The service generates a unique identifier within the time interval to which the current time belongs according to the start value of the identifier range and the identifier number threshold, including: Set the time interval corresponding to the last unique identifier generation and the sequence number when the last unique identifier was generated as the initial value; Obtaining the current time of the service, and determining the time interval to which the current time of the service belongs according to the current time of the service; Determine whether the time interval to which the current time of the service belongs is consistent with the time interval corresponding to the last time the unique identifier was generated; If the time interval to which the current time of the service belongs is consistent with the time interval corresponding to the last generation of the unique identifier, determining whether the sequence number when the unique identifier was generated last time is less than the identifier number threshold of the service; If the sequence number of the last unique identifier generated is greater than or equal to the identifier number threshold of the service, then the sleep time from the current time of the service to the start position of the next time interval is calculated; After the service has been asleep for a certain period of time, the service is awakened and the current time of the service is repeatedly acquired, and the time interval to which the current time of the service belongs is determined according to the current time of the service; If the sequence number of the last generated unique identifier is less than the identifier number threshold of the service, update the sequence number of the last generated unique identifier, increase the sequence number of the last generated unique identifier by a preset value, and obtain a new sequence number of the generated unique identifier; If the time interval to which the current service time belongs is inconsistent with the time interval corresponding to the last generation of the unique identifier, the time interval corresponding to the last generation of the unique identifier is updated to the time interval to which the current service time belongs, and the sequence number when the unique identifier was generated last time is reset to the initial value to obtain a new sequence number when the unique identifier is generated; The unique identifier of the service is the sum of the fixed part, the sequence number when the new unique identifier is generated, and the start value of the service identifier range.
6. The method according to claim 5, characterized in that The service, before generating a unique identifier according to the start value of the identifier range and the identifier number threshold within the time interval to which the current time belongs, further includes: Add a thread lock to the process of generating a unique identifier for the service. The service generates a unique identifier under the protection of the thread lock.
7. The method according to claim 6, characterized in that Adding a thread lock to the process of generating a unique identifier for the service includes: Creating the thread lock; Before the service generates a unique identifier, acquiring the thread lock; After the service generates a unique identifier, the thread lock is released.
8. A device for generating a service identifier, characterized in that: The device comprises: A request module, for any of the services, when the service is started or receives a service change notification of the same service type, the service sends a service change request to the service management center; the service management center is used to receive the service change request and filter out all services of the same type according to the service type of the service; A receiving module, used for the service to receive the number and sorting position of services of the same type returned by the service management center; wherein the sorting position is obtained by the service management center sorting the services according to a preset sorting strategy; A calculation module, used for the service to calculate the start value of the identifier range and the identifier number threshold according to the number of services of the same type, the sorting position and the rule identifier range obtained; The generating module is used to generate a unique identifier within the time interval to which the current time belongs, according to the starting value of the identifier range and the identifier number threshold.
9. An electronic device, characterized in that: The invention comprises a processor, a memory and a computer program stored in the memory and capable of running on the processor, wherein when the computer program is executed by the processor, the method for generating a service identifier according to any one of claims 1 to 7 is implemented.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for generating a service identifier according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Data service processing method and related equipment
CN113645304A
Attribute information acquisition method and device, equipment and medium
CN114064313A
File generation method and device in Internet of Things, computer equipment and storage medium
CN115803736A
Globally unique way to identify a resource
US20220342773A1
Preserving data integrity in tasks across a computing system
WO2022271253A1
