A service identification generation method and device, electronic equipment, and storage medium
By dynamically adjusting the identifier range and generating unique identifiers in a distributed cluster, the problem of insufficient utilization of identifier resources in existing technologies is solved, the system's concurrency and robustness are improved, and the identifier acquisition process is simplified.
Patent Information
- Application Number
- CN202411170795.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-23
- Publication Date
- 2025-12-16
- Estimated Expiration
- 2044-08-23
AI Technical Summary
In distributed clusters, existing technologies cannot effectively utilize identifier resources, resulting in limited concurrency capabilities and low system efficiency. Furthermore, when identifier generation frequency is high and the number of services is variable, existing methods cannot meet the system's concurrency requirements and also present system robustness issues.
The identifier range is dynamically adjusted through the service management center. When a service starts up or receives a service change notification, it obtains the number and sorting position of services of the same type, calculates the starting value and number threshold of the identifier range, generates a unique identifier, and adds a thread lock during the generation process to ensure uniqueness.
It enables dynamic adjustment of identifier range in a distributed cluster, avoiding resource waste, improving the system's concurrency and robustness, simplifying the identifier acquisition process, and saving request and waiting time.
Smart Images

Figure CN119182642B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular to a service identifier generation method and device, electronic equipment and storage medium. BACKGROUND
[0002] In a distributed cluster, how to make concurrent programs generate globally unique identifiers is a problem that will inevitably be encountered. The commonly used methods are as follows:
[0003] Method 1: Use redis, self-developed, etc. Strong consistency central data processing service: when the service needs id, request it, and use it after successful acquisition.
[0004] Method 2. Assign a short unique identifier A to each service in advance, and generate a long unique identifier C = A + custom part B according to a unified rule.
[0005] But the above methods have their own unsuitable cases. The identifier generation method of method 1 determines that all places where the identifier is used must request the identifier from the same service, and the identifier is generated in series and cannot be parallel. This results in a long time-consuming request process and a long waiting time, which cannot take advantage of the concurrency of distributed systems. Moreover, if the central data processing service fails, the unique identifier cannot be generated, which will affect the robustness of the system. Therefore, it is only suitable for scenarios where the identifier generation frequency is low and the generation efficiency is not high. Method 2 is suitable when the length of the unique identifier is not limited or sufficient. However, if the length is limited and the number of identifiers is limited, the length of the identifier A limits the number of services (cannot exceed the maximum value represented by A), and also causes waste of identifiers (when the number of services is less than the maximum value represented by A). Therefore, this method is not suitable for situations where identifier resources are relatively scarce and the number of services can be expanded or reduced.
[0006] For example, the ID data type BusinessObjectIdType in GAT1400 (public safety industry standard GA / T1400) is 33 bits long, the first 14 bits are fixed according to the system and type, the 15th-28th are 14-bit time strings accurate to the second (20240722151406), and only the last 5 bits from 29th-33rd are custom, i.e. the system can generate up to 100000 of 00000-99999 per second. In this case, using method 1 will severely limit the efficiency of the integer system; using method 2, assuming that the identifier A reserved for the service is one bit, then the custom part B is four bits, and assuming that the cluster currently has five services, then the number of services in the entire cluster is limited to 10 or less, and only 50000 unique identifiers can be generated per second, which limits the maximum concurrency of the system (lower than the theoretical concurrency of 100000). SUMMARY
[0007] In view of the above problems, the present application provides a service identifier generation method and device, an electronic device, and a storage medium.
[0008] In a first aspect of the present application, a service identifier generation method is first provided, which is applied to a distributed cluster in which at least one service is deployed, and the service has a corresponding service type. The method comprises the following steps:
[0009] 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 a service management center; the service management center is configured 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 sorting the service according to a preset sorting strategy by the service management center;
[0011] The service calculates 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;
[0012] The service generates a unique identifier according to the start value of the identifier range and the identifier number threshold in the time interval to which the current time belongs.
[0013] Optionally, 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 a service management center; the service management center is configured to receive the service change request and filter out all services of the same type according to the service type of the service, comprising:
[0014] When the service is started for the first time, the service management center is requested to register the information of the service; the service management center is configured to record the information of the service and filter out all services of the same type according to the service type recorded in the information of the service;
[0015] Or,
[0016] When the service receives a service change notification of the same service type, the service management center is sent a service change request; the service management center is configured 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, comprising:
[0018] The service requests the service management center to obtain the number of services of the same type; the service management center is configured 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 configured to receive the request of 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 taking the integer part;
[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, taking the integer part, and multiplying the sorting position.
[0022] Optionally, the service generates a unique identifier according to the start value and the identifier number threshold of the identifier range in a time interval to which the current time of the service belongs, including:
[0023] Setting the time interval corresponding to the last time when the unique identifier is generated and the sequence number at the last time when the unique identifier is generated as initial values;
[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] Judging whether the time interval to which the current time of the service belongs is consistent with the time interval corresponding to the last time when the unique identifier is 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 time when the unique identifier is generated, judging whether the sequence number at the last time when the unique identifier is generated is less than the identifier number threshold of the service;
[0027] If the sequence number at the last time when the unique identifier is generated is greater than or equal to the identifier number threshold of the service, calculating the sleep time from the current time of the service to the starting position of the next time interval;
[0028] After the service has been awake for the sleep time length, the service is repeated to obtain the current time of the service, and to determine the time interval to which the current time of the service belongs according to the current time of the service;
[0029] If the sequence number of the last time when the unique identifier is generated is less than the identifier number threshold of the service, the sequence number of the last time when the unique identifier is generated is updated, the sequence number of the last time when the unique identifier is generated is increased by a preset value to obtain a sequence number of the new time when the unique identifier is generated.
[0030] If the time interval to which the current time of the service belongs is inconsistent with the time interval corresponding to the last time when the unique identifier is generated, the time interval corresponding to the last time when the unique identifier is generated is updated to the time interval to which the current time of the service belongs, the sequence number of the last time when the unique identifier is generated is reset to an initial value to obtain a sequence number of the new time when the unique identifier is generated.
[0031] The unique identifier of the service is the sum of a fixed part and the sequence number of the new time when the unique identifier is generated and the start value of the identifier range of the service.
[0032] Optionally, before the service generates the unique identifier according to the start value of the identifier range and the identifier number threshold in the time interval to which the current time belongs, the service further includes:
[0033] A thread lock is added to the process of generating the unique identifier of the service.
[0034] The service generates the unique identifier under the protection of the thread lock.
[0035] Optionally, adding the thread lock to the process of generating the unique identifier of the service includes:
[0036] The thread lock is created.
[0037] The thread lock is acquired before the service generates the unique identifier.
[0038] The thread lock is released after the service generates the unique identifier.
[0039] In a second aspect of the embodiment of the application, a service identification generation device is further provided, and the device includes:
[0040] A request module is configured to, 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 a service management center; and the service management center is configured 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 is configured to enable 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 by sorting the service according to a preset sorting strategy.
[0042] a computing module, configured to calculate, by the service, a start value of the identifier range and an identifier number threshold according to the obtained number of services of the same type, the sorting position and the rule identifier range;
[0043] a generating module, configured to generate, by the service, the unique identifier according to the start value of the identifier range and the identifier number threshold in a time interval to which a current time belongs.
[0044] In a third aspect of the embodiments of the present application, an electronic device is further provided, which comprises a memory, a processor and a computer program stored in the memory and executable on the processor, and the processor implements the service identification generation method according to any one of the above aspects when executing the computer program.
[0045] In a fourth aspect of the embodiments of the present application, a computer readable storage medium is further provided, which stores a computer program, and the computer program is executable on a processor to implement the service identification generation method according to any one of the above aspects.
[0046] The embodiments of the present application have the following advantages:
[0047] In the embodiments of the present application, for any service, when the service starts 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 configured to receive the service change request and filter 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 according to a preset sorting strategy; the service calculates a start value of the identifier range and an identifier number threshold according to the obtained number of services of the same type, the sorting position and the rule identifier range; and the service generates a unique identifier according to the start value of the identifier range and the identifier number threshold in a time interval to which a current time belongs. The dynamically adjusted identifier range can not only make the number of services unlimited, play the distributed flexible expansion and contraction characteristics, but also fully utilize the identifier, avoid resource waste, make the system reach its theoretical maximum value; the distributed rather than centralized generation method simplifies the process of obtaining the identifier, saves the request time and waiting time, fully plays the parallel characteristics of the distributed system and improves the efficiency; the unique identifier is generated in the required service, reduces the dependence between services and improves the robustness of the system. BRIEF DESCRIPTION OF DRAWINGS
[0048] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the following will briefly introduce the drawings needed to be used in the embodiments or the prior art description.
[0049] Figure 1 is a step flow chart of a service identification generation method provided by an embodiment of the present application;
[0050] Figure 2 is a process flow chart for generating an identifier range provided by an embodiment of the present application;
[0051] Figure 3 is a process flow chart for identifier generation provided by an embodiment of the present application;
[0052] Figure 4 is a structural block diagram of a service identifier generation device provided by an embodiment of the present application. DETAILED DESCRIPTION
[0053] To make the objectives, technical solutions and advantages of the embodiments of the present application clearer, the embodiments of the present application will be described in detail below with reference to the drawings. However, those skilled in the art can understand that, in the embodiments of the present application, many technical details are presented in order to make the readers better understand the present application. However, the technical solutions claimed by the present application can be implemented even without these technical details and based on various changes and modifications of the following embodiments. The following embodiments are classified for the convenience of description, and should not constitute any limitation on the specific implementation of the present application, and the embodiments can be combined and referenced with each other without contradiction.
[0054] In the embodiments of the present application, the core idea is that, when a service is started and a same type of service is added or reduced, the latest service number and the current service ranking position are obtained, and the available identifier code range of the current service is dynamically calculated based on the total service number and the current service ranking position; when the current service needs to use a unique identifier code, the unique identifier code is generated according to the agreed rules. The number of services is not limited, and the identifier is not wasted, and the self-generation is more efficient and less dependent.
[0055] Referring to Figure 1 , a step flow chart of a service identifier generation method provided by an embodiment of the present application is shown, which can specifically include the following steps:
[0056] Step 101, 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 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.
[0057] Among them, the same type of service refers to a service process in a distributed cluster, which has the same function and effect and can be replaced by each other.
[0058] In the embodiment of the present application, when any service is started for the first time, it requests the service management center (such as a registration service, a service configuration database, etc.) to register its information, including service type, service name, IP address, port number, etc., the service management center receives the registration request, records the registration information of the service, and filters all services of the same type according to the service type in the information.
[0059] When any service receives a service change notification of the same type of service (for example, an increase or decrease of the same type of service), it sends a service change request to the service management center, the service management center receives the service change request, filters all services of the same type in real time according to the service type of the service, and notifies the relevant service to update the number of the same type of services in its local cache. Specifically, the service can obtain the update of the number of the same type of services in real time through a polling or subscription mechanism.
[0060] In step 102, the service receives the number of the same type of services returned by the service management center and the sorting position; wherein the sorting position is obtained by the service management center according to a preset sorting strategy.
[0061] In the embodiment of the present application, the service requests the service management center to obtain the number of the same type of services, and the service management center returns the number of the same type of services filtered according to the service type of the service to the service after receiving the request.
[0062] After the service obtains the number of the same type of services returned by the service management center, it needs to request the service management center to obtain its sorting position.
[0063] Specifically, when the service is started for the first time, the service requests the service management center (such as a service registration center, a configuration database, etc.) to register its information, the service management center receives the registration request, records the registration information of the service, and sorts the service according to a preset sorting strategy, the service requests the service management center to obtain its sorting position after registration, and the service management center returns the sorting position obtained by sorting the service according to the preset sorting strategy after receiving the request. The sorting strategy can be a simple dictionary order (such as sorting by service name or IP address), or a complex logic (such as sorting by service load, health status, etc.), and the sorting strategy should be clearly agreed between the service management center and the service to ensure consistency and correctness.
[0064] When the service receives a service change notification of the same type of service (for example, an increase or decrease of the same type of service), it sends a service change request to the service management center, which recalculates the ranking position of the service and notifies the relevant service to update its ranking position. Among them, the service can obtain the update of the ranking position in real time through the polling or subscription mechanism.
[0065] Step 103, the service calculates the start value of the identifier range and the identifier number threshold according to the obtained number of the same type of service, the ranking position and the rule identifier range.
[0066] In the embodiment of the application, the calculation formula of the identifier number threshold of the identifier range of the service, that is, the maximum number maxNum, is as follows:
[0067]
[0068] Among them, Downward rounding, maxNum represents the maximum number of the identifier range of the service, D represents the rule identifier range assumed according to the business requirement, and A represents the number of the same type of service of the service.
[0069] The calculation formula of the start value baseNum of the identifier range of the service is as follows:
[0070]
[0071] Among them, Downward rounding, baseNum represents the start value of the identifier range of the service, D represents the rule identifier range assumed according to the business requirement, A represents the number of the same type of service of the service, and B represents the ranking position of the service.
[0072] Step 104, the service generates a unique identifier in 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 application, before generating the unique identifier, first set the time interval corresponding to the last time when the unique identifier is generated as the initial value, that is, lastTime=0, and the serial number when the unique identifier is generated last time as the initial value, that is, 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, first, a thread lock is created, and then the service generates a unique identifier under the protection of the thread lock by acquiring the thread lock before the service generates the unique identifier, to ensure the uniqueness of the generated identifier.
[0077] 2. Obtain the current time, and then determine the time interval time to which the current time belongs according to the current time.
[0078] Specifically, first, the size of the time interval is determined, for example, if the time interval is every hour (3600 seconds), then each time interval is 1 hour, and then the current time is divided by the time interval, and then the integer part is taken to obtain 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 yes, proceed to step 4; if no, 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 within each time interval. By determining whether time is consistent with lastTime, it can be ensured that the generated identifier is unique within each time interval. If the current time has entered the next time interval, the sequence number needs to be reset, thereby avoiding the repetition of identifiers in different time intervals.
[0081] 4. Determine whether lastNum is less than maxNum, if no, proceed to step 5; if yes, update the record value lastNum=lastNum+1; and jump to step 6.
[0082] Specifically, the purpose of determining the sequence number range is to ensure that the number of generated identifiers within each time interval does not exceed the preset maximum value maxNum. By setting the maximum number of identifiers (maxNum) allowed to be generated within each time interval, it can be prevented that the sequence number overflows. If the sequence number reaches the maximum value, the service will no longer generate new identifiers, thereby avoiding the sequence number exceeding the range. By limiting the number of identifiers generated within each time interval, the generation rate of identifiers can be controlled, which is particularly important in high-concurrency scenarios, and can avoid system overload and ensure stable operation of the system. Within the same time interval, the sequence number is incremented to generate identifiers, which can ensure the uniqueness of the generated identifiers. 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 identifier generation. By flexibly setting the sequence number range, different business requirements can be met, such as fast generation in high-concurrency scenarios and stable generation in low-frequency scenarios.
[0083] 5. Calculate the time from the current time to the start of the next time interval: sleepTime = next time - current time. Calculate the sleep duration for the service sleep. After waking up, jump to step 2.
[0084] Specifically, when the number of identifiers generated within the current time interval reaches its maximum, the service needs to wait for the start of the next time interval before it can continue generating new identifiers. Calculating the sleep time determines the length of time that needs to be waited so that the service can automatically wake up at the start of the next time interval and continue generating identifiers.
[0085] 6. Generate a unique identifier ID = lastNum + baseNum, where the unique identifier is a fixed part + ID.
[0086] Specifically, the fixed part is fixed according to business needs.
[0087] 7. Release the thread lock and end.
[0088] By dynamically adjusting the identifier range through the above operations, the number of services can be unlimited, giving full play to the characteristics of distributed flexible expansion and contraction. At the same time, the identifiers can be fully utilized to avoid resource waste and enable the system to reach its theoretical maximum value. Meanwhile, 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 distributed systems, and improves efficiency. In addition, generating identifiers in the required services reduces the dependencies between services and improves the robustness of the system.
[0089] When generating unique identifiers, you can also use timestamps and incrementing sequences. You can use the current timestamp (such as a millisecond or microsecond timestamp) plus a locally incrementing sequence number to generate the identifier. This is not only simple and efficient, but also does not depend on external systems. At the same time, when the service starts, the sequence number is initialized, and when a service is added or removed, the starting value of the sequence number is adjusted to avoid conflicts with identifiers generated by other services.
[0090] Furthermore, in this embodiment of the invention, step 101 further includes the following steps:
[0091] S1011, When the service is started for the first time, it requests 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, a service change request is sent 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 in real time.
[0094] Specifically, when any new service starts, it needs to register with the service management center. The service sends a registration request to the service management center through a certain communication protocol (such as HTTP (Hypertext Transfer Protocol), RPC (Remote Procedure Call), etc.), and the request usually contains basic information of the service, such as service name, service type, service address, port number, version number, etc. After receiving the registration request, the service management center records the service information in its internal data storage system, such as database, memory cache, etc. The service management center filters out all services of the same type in its recorded service list according to the service type recorded in the service information, which is usually for the purpose of service discovery and service load balancing.
[0095] When any service receives a service change notification of the same type (such as an increase or decrease of services of the same type), it sends a service change request to the service management center through a communication protocol (such as HTTP, RPC, etc.), and the service management center receives the service change request and updates the service information recorded in its internal service list, and filters out all services of the same type in real time according to the service type, which ensures that the service list in the service management center is always up-to-date.
[0096] Further, in the embodiment of the application, step 102 further comprises 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 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.
[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 sends a request to the service management center to request the number of services of the same type. After receiving the request, the service management center filters in its internal service registration table according to the service type information contained in the request, finds all registered services of the same type, counts their number, and returns the number of filtered services of the same type as a response to the requesting service.
[0099] S1022, the service requests the service management center to obtain the sorting position after obtaining the number of same type services returned by the service management center; and the service management center is configured to receive the request of the service for obtaining 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 same type services from the service management center, the service needs to know its sorting position in all same type services in order to perform load balancing, service scheduling or other business logic processing. Therefore, the service sends a request to the service management center to request the sorting position. After receiving the request, the service management center sorts all same type services according to a preset sorting strategy, and returns the sorting position of the requesting service as a response to the service. The preset sorting strategy can be a simple dictionary 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 be explicitly agreed between the service management center and the service to ensure consistency and correctness.
[0101] Further, in the embodiment of the application, step 104 further comprises the following steps:
[0102] S1041, setting the time interval corresponding to the last time when the unique identifier is generated and the sequence number at the last time when the unique identifier is generated as initial values.
[0103] In the embodiment of the application, lastTime is set to 0, i.e. the time interval corresponding to the last time when the unique identifier is generated is 0; and lastNum is set to 0, i.e. the time interval corresponding to the last time when the unique identifier is generated 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 the embodiment of the application, the size of the time interval is determined first, for example, if the time interval is one hour (3600 seconds), each time interval is one hour, then the current time is divided by the time interval, and the result is rounded to obtain the start time of the time interval to which the current time belongs, i.e. the time interval to which the current time of the service belongs is time.
[0106] S1043, determining whether the time interval to which the current time of the service belongs is consistent with the time interval corresponding to the last time when the unique identifier is generated.
[0107] In the embodiment of the present application, whether time is consistent with lastTime is judged. By judging whether time is consistent with lastTime, it can be ensured that the identifier generated in each time interval is unique.
[0108] In S1044, if the time interval to which the current time of the service belongs is consistent with the time interval corresponding to the last time when the unique identifier is generated, it is judged whether the serial number at the last time when the unique identifier is generated is less than the identifier number threshold of the service.
[0109] In the embodiment of the present application, if time is consistent with lastTime, it is further judged whether lastNum is less than maxNum.
[0110] In S1045, if the serial number at the last time when the unique identifier is generated is greater than or equal to the identifier number threshold of the service, the sleep time from the current time of the service to the start position of the next time interval is calculated.
[0111] In the embodiment of the present application, if lastNum is greater than or equal to maxNum, it indicates that the serial number reaches the maximum value, and the number of identifiers generated in the current time interval reaches the maximum value. The service will not generate a new identifier any more, so as to avoid that the serial number exceeds the range. At this time, it is needed to wait for the start of the next time interval to continue to generate a new identifier. The sleep time is calculated to determine the length of time to be waited, so that the service is automatically woken up and continues to generate identifiers at the start of the next time interval.
[0112] In S1046, the service is woken up after the sleep time length, 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.
[0113] In the embodiment of the present application, the service is automatically woken up after the sleep time length, and S1042 is repeatedly executed.
[0114] In S1047, if the serial number at the last time when the unique identifier is generated is less than the identifier number threshold of the service, the serial number at the last time when the unique identifier is generated is updated, and the serial number at the last time when the unique identifier is generated is increased by a preset value to obtain the serial number at the new time when the unique identifier is generated.
[0115] In the embodiment of the present application, if lastNum is less than maxNum, it indicates that the serial number has not reached the maximum value, and a new identifier can be generated. lastNum is updated as lastNum+1 to obtain the serial number at the new time when the unique identifier is generated. In the same time interval, the identifier is generated by incrementing the serial number, so as to ensure that the generated identifier is in order. This not only facilitates management and query, but also improves the maintainability of the system service.
[0116] S1048, if the time interval to which the service current time belongs is inconsistent with the time interval corresponding to the last time when the unique identifier is generated, updating the time interval corresponding to the last time when the unique identifier is generated to the time interval to which the service current time belongs, resetting the sequence number at the last time when the unique identifier is generated to an initial value, and obtaining a new sequence number at the time when the unique identifier is generated.
[0117] In the embodiment of the application, if the time is inconsistent with the lastTime, it indicates that the current time has entered the next time interval, and then the sequence number needs to be reset, so as to avoid the repetition of the identifier in different time intervals. Specifically, the lastNum is updated to 0, and the lastTime is updated to the time.
[0118] S1049, the unique identifier of the service is the sum of a fixed part and the sequence number at the time when the new unique identifier is generated and the start value of the service identifier range.
[0119] In the embodiment of the application, the unique identifier of the service is equal to the fixed part + the lastNum + the baseNum, wherein the fixed part is a part fixed according to the service demand, and the lastNum is the sequence number after being updated or reset.
[0120] Further, in the embodiment of the application, the step 104 further includes the following steps:
[0121] S1, adding a thread lock to the process of generating the unique identifier of the service.
[0122] Specifically, the thread lock is added before the service generates the unique identifier.
[0123] S2, the service generates the 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 unique identifier of the service is under the protection of the thread lock.
[0125] Through the above operation, by adding the thread lock to the process of generating the unique identifier of the service, the uniqueness of the generated identifier is ensured.
[0126] Further, in the embodiment of the application, the S1 further includes the following steps:
[0127] S1-1, creating the thread lock.
[0128] Specifically, in different programming languages, different ways are used to create thread locks. For example, in Python, the threading module can be used to create a thread lock; in Java, the Lock interface in the java.util.concurrent.locks package can be used to create a thread lock; in C++, std::mutex can be used to create a thread lock, and the like.
[0129] S1-2, before the service generates a unique identifier, the thread lock is acquired.
[0130] S1-3, after the service generates a unique identifier, the thread lock is released.
[0131] Further, in the embodiments of the present application, as shown in Figure 2 , a process flowchart for generating an identifier range is provided, and the specific process is as follows:
[0132] 1. Obtain the number A of services of the same type of the service, and obtain the number A of services of the same type stored in the system from the service management center (such as a registration service, a service configuration database, etc.) of the distributed cluster;
[0133] 2. Obtain the fixed ordering position B of the service, and obtain the ordering position B of the current service from the service management center (such as a registration service, a service configuration database, etc.) of the distributed cluster according to the agreed ordering strategy;
[0134] 3. Calculate the identifier range of the service, calculate the start value baseNum of the current service identifier range, and the maximum number maxNum, according to the business requirement assumption rule identifier range D, the calculation formula is as follows:
[0135]
[0136] Through the above operation, the dynamically adjusted identifier range can not only make the service number unrestricted, play the characteristics of distributed flexible expansion and contraction, but also fully utilize the identifier code, avoid resource waste, and make the system reach its theoretical maximum value.
[0137] Further, in the embodiments of the present application, as shown in Figure 3 , a process flowchart for generating an identifier is provided, and the specific process is as follows:
[0138] 1. Obtain 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 when the unique identifier is generated;
[0140] 3. If yes, continue to judge whether the sequence number of the last generated identifier lastNum is less than the maximum number maxNum of the calculated identifier range of the service; if no, update the value of the record value lastNum to 0, lastTime = time;
[0141] 4. If the sequence number of the last generated identifier lastNum is less than the maximum number maxNum of the calculated identifier range of the service, update lastNum = lastNum + 1;
[0142] 5. If the sequence number of the last generated identifier lastNum is not less than the maximum number maxNum of the calculated identifier range of the service, calculate the sleep time from the current time to the start position of the next time interval, and after the service passes through the sleep time length, the service is automatically woken up and step 1 is repeated;
[0143] 6. The generated unique identifier = lastNum + baseNum + fixed part, lastNum is the sequence number of the newly generated identifier after update.
[0144] Through the above operation, according to the identifier range of the service, when the service needs to use the unique identifier, the unique identifier is generated according to the agreed rule, which is more efficient.
[0145] In the embodiment of the application, 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 for receiving the service change request and screening all services of the same type according to the service type of the service; the service receives the number and sorting position of the services of the same type returned by the service management center; wherein the sorting position is obtained by the service management center according to a preset sorting strategy; the service calculates 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; the service generates a unique identifier according to the start value of the identifier range and the identifier number threshold in the time interval to which the current time belongs. The dynamically adjusted identifier range can not only make the number of services unlimited, but also play the characteristics of distributed flexible expansion and contraction, fully utilize the identifier, avoid resource waste, make the system reach its theoretical maximum value, and simplify the process of obtaining the identifier, save the request time and waiting time, fully play the parallel characteristics of the distributed mode, and improve the efficiency; the unique identifier is generated in the required service, which reduces the dependence between services and improves the robustness of the system.
[0146] It should be noted that, for the method embodiments, for the purpose of simple description, they are all described as a series of action combinations, but those skilled in the art should know that the embodiments of the present application are not limited to the action sequence described, because according to the embodiments of the present application, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should know that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily necessary for the embodiments of the present application.
[0147] Referring to Figure 4 , a structural schematic diagram of a service identifier generation device provided by an embodiment of the present application is shown, which can specifically include the following modules:
[0148] The request module 201 is configured to, for any service, when the service is started or receives a service change notification of the same service type, send a service change request to a service management center; and the service management center is configured to receive the service change request and filter out all services of the same type according to the service type of the service.
[0149] The receiving module 202 is configured 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 according to a preset sorting strategy.
[0150] The calculation module 203 is configured 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 generation module 204 is configured to generate a unique identifier according to the start value of the identifier range and the identifier number threshold in the time interval to which the current time belongs.
[0152] Optionally, the request module 201 includes:
[0153] The number of services of the same type obtaining module is configured to, when the service is started for the first time, request the service management center to register the information of the service; and the service management center is configured to record the information of the service and filter out all services of the same type according to the service type recorded in the information of the service.
[0154] Or,
[0155] When the service receives a service change notification of the same service type, the service management center is configured to send a service change request to the service management center; and the service management center is configured 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 same-type service number receiving module, configured to request a same-type service number from the service management center by the service; the service management center is configured to receive the request for the same-type service number from the service, and return a number of all same-type services filtered according to a service type of the service;
[0158] a sorting position receiving module, configured to request a sorting position from the service management center by the service after the same-type service number returned by the service management center is acquired; the service management center is configured to receive the request for the sorting position from the service, and return the sorting position obtained by sorting the service according to a preset sorting strategy.
[0159] Optionally, the generating module 204 comprises:
[0160] a setting module, configured to set a time interval corresponding to a last time when a unique identifier is generated and a sequence number at the last time when the unique identifier is generated as an initial value;
[0161] a time interval obtaining module, configured to obtain a current time of the service, and determine a time interval to which the current time of the service belongs according to the current time of the service;
[0162] a first judging module, configured to judge whether the time interval to which the current time of the service belongs is consistent with a time interval corresponding to a last time when a unique identifier is generated;
[0163] a second judging module, configured to, if the time interval to which the current time of the service belongs is consistent with the time interval corresponding to the last time when the unique identifier is generated, judge whether a sequence number at the last time when the unique identifier is generated is less than a threshold value of an identifier number of the service;
[0164] a sleep time calculating module, configured to, if the sequence number at the last time when the unique identifier is generated is greater than or equal to the threshold value of the identifier number of the service, calculate a sleep time from the current time of the service to a starting position of a next time interval;
[0165] a waking module, configured to, after the service is kept for a sleep time length, wake up the service and repeatedly execute the obtaining of the current time of the service, and the determination of 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, if the sequence number at the last time when the unique identifier is generated is less than the threshold value of the identifier number of the service, update the sequence number at the last time when the unique identifier is generated, increase the sequence number at the last time when the unique identifier is generated by a preset value, and obtain a sequence number at a new time when a unique identifier is generated;
[0167] a second updating module, configured to, if a time interval to which the service current time belongs is inconsistent with a time interval corresponding to the last time when the unique identifier is generated, update the time interval corresponding to the last time when the unique identifier is generated to the time interval to which the service current time belongs, and reset a serial number at the last time when the unique identifier is generated to an initial value to obtain a serial number at the new time when the unique identifier is generated;
[0168] an identifier generating module, configured to, the unique identifier of the service is a sum of a fixed part and a sum of the serial number at the new time when the unique identifier is generated and a start value of the service identifier range.
[0169] In the embodiment of the present application, 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 configured 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 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; and the service generates a unique identifier according to the start value of the identifier range and the identifier number threshold in the time interval to which the current time belongs. The dynamically adjusted identifier range can not only make the number of services unlimited, play the characteristics of distributed flexible expansion and contraction, but also fully utilize the identifier, avoid resource waste, make the system reach its theoretical maximum value; the distributed rather than central generation method simplifies the process of obtaining the identifier, saves the request time and waiting time, fully plays the parallel characteristics of the distributed system, and improves the efficiency; the unique identifier is generated in the required service, reduces the dependence between services, and improves the robustness of the system.
[0170] An embodiment of the present application further provides an electronic device, which can include a processor, a memory and a computer program stored on the memory and capable of running on the processor, and when the computer program is executed by the processor, the method for generating the service identifier is realized.
[0171] An embodiment of the present application further provides a computer readable storage medium, and the computer readable storage medium stores a computer program, and when the computer program is executed by the processor, the method for generating the service identifier is realized.
[0172] For the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the related parts refer to the part of the method embodiment.
[0173] Those skilled in the art will appreciate that embodiments of the application can be readily used as software, hardware, or a combination of software and hardware. In one embodiment, the present application can be implemented as a computer program product, which can include a computer-readable storage medium having stored computer program code. Embodiments of the present application can also be implemented as a system on chips (SoC).
[0174] Embodiments of the present application are described in terms of flowcharts and / or block diagrams in which several operational steps or blocks are presented, and various elements of machine according to the embodiments of the present application are described. It will be understood that each operational step or block in a flowchart, and combinations of operational steps or blocks, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general purpose computer, special purpose computer, embedded processing element or other programmable data processing terminal devices to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing terminal devices, create means for implementing the functions specified in the flowchart block(s) or a flowchart block(s) combinations. These computer program instructions can also be stored in a computer readable memory that can direct a computer or other programmable data processing terminal devices to function in a particular manner, such that the instructions stored in the computer readable memory produce an article of manufacture including instructions which implement the function specified in the flowchart block(s) or a flowchart block(s) combinations. Figure 1 Figure 1
[0175] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal devices to cause a series of operational steps to be performed on the computer or other programmable terminal devices to produce a computer implemented process such that the instructions which execute on the computer or other programmable terminal devices provide steps for implementing the functions specified in the flowchart block(s) or a flowchart block(s) combinations. Figure 1 Figure 1
[0176] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal devices to cause a series of operational steps to be performed on the computer or other programmable terminal devices to produce a computer implemented process such that the instructions which execute on the computer or other programmable terminal devices provide steps for implementing the functions specified in the flowchart block(s) or a flowchart block(s) combinations. Figure 1 Figure 1
[0177] While the preferred embodiments of the application have been described, additional variations and modifications can be made to the embodiments by those skilled in the art once they learn of the basic inventive concepts. Therefore, the appended claims are intended to cover all such modifications and variations as fall within the scope of the embodiments of the present application.
[0178] Finally, it is to be understood that the terms such as first and second, etc., are used herein solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any such actual relationship or order between such entities or actions. Moreover, the terms "comprises", "comprising", or any other variations thereof, are intended to cover a non-exclusive inclusion such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but can also include other elements not expressly listed or inherent to such process, method, article, or apparatus. An element proceeded by "comprises... a" does not, without more constraints, exclude the existence of additional identical elements in the process, method, article, or apparatus that comprises the recited element.
[0179] The above describes in detail the provided service identifier generation method and device, electronic equipment, and storage medium. The principles and implementation manners of the present application are described by using specific examples. Although the implementation manners of the present application are described above, the above description and definition are only for facilitating the understanding of the implementation manners of the present application, and are not intended to limit the present application. Any modification and change made without departing from the spirit and scope of the present application is within the protection scope of the present application. In summary, the content of the specification should not be understood as a limitation of the present application.
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 includes: For any of the aforementioned services, when the service starts 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 their sorting positions returned by the service management center; wherein, the sorting positions are obtained by the service management center sorting the services according to a preset sorting strategy; The service calculates the starting value of the identifier range and the threshold value of the identifier number based on the number of services of the same type obtained, the sorting position, and the range of rule identifiers. Within the time interval to which the current time belongs, the service generates a unique identifier based on the start value of the identifier range and the identifier count threshold. Within the time interval to which the current time belongs, the service generates unique identifiers based on the start value of the identifier range and the identifier count threshold, including: Set the time interval corresponding to the last time the unique identifier was generated and the sequence number of the last time the unique identifier was generated as the initial values; Obtain the current time of the service, and determine the time interval to which the current time of the service belongs based on the current time of the service; Determine whether the current time interval of the service is consistent with the time interval corresponding to the last time a unique identifier was generated; If the current time interval of the service is the same as the time interval corresponding to the last time a unique identifier was generated, determine whether the sequence number of the last time a unique identifier was generated is less than the threshold for the number of identifiers of the service. If the sequence number when the last unique identifier was generated is greater than or equal to the identifier number threshold of the service, then calculate the sleep time of the service from the current time to the start of the next time interval; After the service has been in a sleep state for a certain period of time, it is woken up and repeatedly executes the process of obtaining the current time of the service and determining the time interval to which the current time of the service belongs based on 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 by increasing the sequence number of the last generated unique identifier by a preset value to obtain the new sequence number of the generated unique identifier. The unique identifier of the service is the sum of the fixed part, the sequence number when the new unique identifier was generated, and the starting value of the service identifier range.
2. The method according to claim 1, characterized in that, For any of the aforementioned services, when the service starts 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 all services of the same type according to the service type of the service, including: When the service is started for the first time, it requests 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 their sorting 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 service's request 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 service's request 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 starting 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 threshold for the number of identifiers in the service identifier range is obtained by dividing the rule identifier range by the number of services of the same type, rounding down, and then multiplying by the sorting position.
5. The method according to claim 1 or 4, characterized in that, The service generates unique identifiers within the time interval to which the current time belongs, based on the start value of the identifier range and the identifier count threshold, and also includes: If the time interval to which the current time of the service belongs is different from the time interval to which the unique identifier was last generated, update the time interval to which the current time of the service belongs, reset the sequence number to the initial value, and obtain the new sequence number to which the unique identifier was last generated.
6. The method according to claim 5, characterized in that, Before generating a unique identifier within the time interval to which the current time belongs, based on the start value of the identifier range and the identifier count threshold, the service also 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: Create the thread lock; Acquire the thread lock before the service generates a unique identifier; After the service generates a unique identifier, the thread lock is released.
8. A service identifier generation apparatus, characterized in that, The device includes: The request module is used to send a service change request to the service management center when the service starts or receives a service change notification of the same service type for any of the services mentioned above; 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 mentioned above. The receiving module is used for receiving the number of services of the same type and their sorting positions returned by the service management center; wherein the sorting positions are obtained by the service management center sorting the services according to a preset sorting strategy; The calculation module is used to calculate the starting value of the identifier range and the threshold value of the identifier number based on the number of services of the same type, the sorting position, and the range of rule identifiers obtained by the service. The generation module is used to generate a unique identifier for the service within the time interval to which the current time belongs, based on the start value of the identifier range and the identifier count threshold. The generation module includes: The settings module is used to set the time interval corresponding to the last generation of the unique identifier and the sequence number of the last generation of the unique identifier as the initial values. The time interval acquisition module is used to acquire the current time of the service and determine the time interval to which the current time of the service belongs based on the current time of the service. The first judgment module is 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. The second judgment module is used to determine whether the sequence number of the last time the unique identifier was generated is less than the threshold of the number of identifiers of the service if the time interval to which the current time of the service belongs is the same as the time interval corresponding to the last time the unique identifier was generated. The sleep time calculation module is used to calculate the sleep time from the current time to the start of the next time interval of the service if the sequence number when the last unique identifier was generated is greater than or equal to the identifier number threshold of the service. The wake-up module is used to wake up the service after the service has been in a sleep period and repeatedly execute the process of obtaining the current time of the service and determining the time interval to which the current time of the service belongs based on the current time of the service. The first update module is used to update the sequence number of the last time the unique identifier was generated if the sequence number of the last time the unique identifier was generated is less than the identifier number threshold of the service, by adding a preset value to the sequence number of the last time the unique identifier was generated, so as to obtain a new sequence number of the last time the unique identifier was generated. The identifier generation module uses a unique identifier for the service that is the sum of a fixed portion, the sequence number when the new unique identifier is generated, and the starting value of the service identifier range.
9. An electronic device, characterized in that, It includes a processor, a memory, and a computer program stored in the memory and capable of running on the processor, wherein the computer program, when executed by the processor, implements the method for generating a service identifier as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, A computer program is stored on the computer-readable storage medium, which, when executed by a processor, implements the method for generating a service identifier as described in any one of claims 1 to 7.
Citation Information
Patent Citations
Data service processing method and related equipment
CN113645304A
File generation method and device in Internet of Things, computer equipment and storage medium
CN115803736A