Unique machine code dynamic calculation method
By introducing dynamic machine code generators and dynamic computer IDs into distributed systems, the problem that traditional static configuration methods cannot adapt to dynamic changes in nodes is solved, and the uniqueness of machine code and high-performance dynamic allocation are achieved.
Patent Information
- Application Number
- CN202411724688.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-28
- Publication Date
- 2025-03-11
AI Technical Summary
In a distributed elastic multi-instance architecture, traditional static configuration methods are difficult to adapt to the dynamic increase and decrease of nodes, resulting in machine code being no longer unique and unable to meet the needs of dynamic systems.
A unique machine code dynamic calculation method is adopted. By introducing a dynamic machine code generator in the application instance of each application service, the machine ID pool is initialized when the instance is started, and the used ID value is eliminated by parsing the registration metadata information, and finally an unused machine ID is selected.
The ability to dynamically calculate unique machine code in distributed systems is realized, ensuring that the generated IDs are always unique, and have better performance than the existing technology, and are extremely fast and do not require additional access to the database.
Smart Images

Figure CN119597369A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of electronic information technology, and in particular to a unique machine code dynamic calculation method. Background Art
[0002] In actual projects, many application scenarios involve the management of unique machine codes. In the early days of monolithic applications, machine codes could be statically assigned through environment variables. However, with the popularity of containerization and microservices, most current projects use a distributed elastic multi-instance architecture. In this environment, traditional static configuration methods (such as manually assigning a unique ID to each node) are no longer applicable. Therefore, a method for dynamically calculating machine codes is needed to cope with the dynamic increase and decrease of nodes. For example, in the automatic expansion and contraction scenario of Kubernetes, the allocation of machine codes must be able to respond to changes in nodes in real time to ensure that the generated ID remains unique throughout the distributed system. Summary of the invention
[0003] The purpose of the present invention is to provide a unique machine code dynamic calculation method, which can calculate the unique machine ID lightly, quickly and dynamically when the instance is started.
[0004] The technical solution adopted by the present invention is: A unique machine code dynamic calculation method comprises the following steps: Step 1, reference a dynamic machine code generator in each application instance of each application service; Step 2: When the application instance starts registration, the dynamic machine code generator initializes and generates a machine ID pool, which is stored in the device memory; Step 3: The dynamic machine code generator obtains and parses the registration metadata information of all application instances of the current application service in the registration service center, and then removes the ID values already used by other application instances from the machine ID pool; Step 4: After the elimination is completed, a machine ID is selected from the machine ID pool as the machine ID of the current application instance; Step 5: Update the selected machine ID to the metadata of the current application instance in the service registration center.
[0005] Furthermore, in step 1, the dynamic machine code generator is introduced into the application instance in the form of a program dependency package; Furthermore, the ID value range of the machine ID pool in step 2 defaults to 0-31.
[0006] Furthermore, step 3 specifically includes the following steps: Step 3-1, the dynamic machine code generator obtains instance information of all application instances that have been registered with the current application service from the service registration center through the interface (API) provided by the middleware of the service registration center; each instance information includes the registered metadata information; Specifically, the middleware of the service registration center is NACOS.
[0007] Step 3-2, cyclically parse the metadata of all application instances, and determine whether the currently parsed application instance has a wokerid attribute; if so, delete the ID value corresponding to the currently parsed application instance wokerid attribute from the machine ID pool of the current application instance; otherwise, ignore the corresponding application instance; Furthermore, in step 4, the first ID is selected from the machine ID pool as the machine ID of the target application instance.
[0008] Specifically, in step 5, the target application instance is updated in the wokerid attribute field of the metadata in the service registration center.
[0009] Furthermore, the service registration center manages more than two application services, and each application service has more than one application instance.
[0010] The present invention adopts the above technical solution to fix the candidate machine code in the code, obtain the used machine code when registering the service, eliminate it in the memory, and finally select the smallest unused machine code. The present invention has better performance by adopting the above technical means; there is no additional overhead, no need to access the database additionally, but to pull the machine code of other existing instances when registering the service; the whole process is pure memory operation, and the speed is extremely fast. Compared with the situation in the prior art that needs to read the database, the performance of the present invention is at least one order of magnitude better. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] The present invention is further described in detail below with reference to the accompanying drawings and specific embodiments; Figure 1 A schematic diagram of the principle architecture of a unique machine code dynamic calculation method of the present invention; Figure 2 The figure is a flow chart of a unique machine code dynamic calculation method of the present invention. DETAILED DESCRIPTION
[0012] In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application.
[0013] Machine code: Machine code refers to an identifier used to uniquely identify each computing node (such as a server, virtual machine, or container) in a computer system. In a distributed system, the main function of the machine code is to distinguish different computing nodes.
[0014] Machine codes usually have the following characteristics: Uniqueness: The machine code of each computing node should be unique to prevent duplicate identifiers in the same system. Static or dynamic: Machine codes can be statically assigned (e.g., fixed IDs) or dynamically generated (automatically assigned based on the current environment or configuration). Distributed applications: In distributed systems, the main role of machine codes is to ensure that the generated IDs are unique across multiple nodes, even if the number of nodes changes dynamically.
[0015] like Figure 1 As shown in FIG. 2 , the present invention discloses a unique machine code dynamic calculation method, which includes the following steps: Step 1, reference a dynamic machine code generator in each application instance of each application service; further, as an optional implementation, in step 1, the dynamic machine code generator is introduced into the application instance in the form of a program dependency package.
[0016] Step 2: When the application instance starts registration, the dynamic machine code generator initializes and generates a machine ID pool, which is stored in the device memory; Specifically, when the application instance is started, the dynamic machine code generator starts working after the spring context is initialized. The dynamic machine code generator first initializes the machine ID pool, which defaults to 0-31.
[0017] Step 3: The dynamic machine code generator obtains and parses the registration metadata information of all application instances of the current application service in the registration service center, and then removes the ID values already used by other application instances from the machine ID pool; Furthermore, step 3 specifically includes the following steps: Step 3-1, the dynamic machine code generator obtains instance information of all application instances that have been registered with the current application service from the service registration center through the interface (API) provided by the middleware of the service registration center; each instance information includes the registered metadata information; Specifically, the middleware of the service registration center is NACOS. The present invention only needs to rely on NACOS and maintains the distributed characteristics, which is light and practical.
[0018] Step 3-2, cyclically parse the metadata of all application instances, and determine whether the currently parsed application instance has a wokerid attribute; if so, delete the ID value corresponding to the currently parsed application instance wokerid attribute from the machine ID pool of the current application instance; otherwise, ignore the corresponding application instance; In step 4, after the elimination is completed, a machine ID is selected from the machine ID pool as the machine ID of the current application instance; further, as a feasible implementation method, in step 4, the first ID is selected from the machine ID pool as the machine ID of the target application instance.
[0019] Step 5: Update the selected machine ID to the metadata of the current application instance in the service registration center.
[0020] Specifically, in step 5, the target application instance is updated in the wokerid attribute field of the metadata in the service registration center.
[0021] Furthermore, the service registration center manages more than two application services, and each application service has more than one application instance.
[0022] In the technical solution of the present invention, each application instance has a built-in dynamic machine code generator, which is released in the form of a program dependency package, and all application instances are introduced into the application instance project through the dependency package.
[0023] The present invention adopts the above technical solution to fix the candidate machine code in the code, obtain the used machine code when registering the service, eliminate it in the memory, and finally select the smallest unused machine code. The present invention has better performance by adopting the above technical means; there is no additional overhead, no need to access the database additionally, but to pull the machine code of other existing instances when registering the service; the whole process is pure memory operation, and the speed is extremely fast. Compared with the situation in the prior art that needs to read the database, the performance of the present invention is at least one order of magnitude better.
[0024] Obviously, the described embodiments are part of the embodiments of the present application, rather than all of the embodiments. In the absence of conflict, the embodiments in the present application and the features in the embodiments can be combined with each other. The components of the embodiments of the present application generally described and shown in the drawings here can be arranged and designed in various different configurations. Therefore, the detailed description of the embodiments of the present application is not intended to limit the scope of the application claimed for protection, but merely represents the selected embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians of the art without making creative work are within the scope of protection of the present application.
Claims
1. A unique machine code dynamic calculation method, characterized in that: It includes the following steps: Step 1, reference a dynamic machine code generator in each application instance of each application service; Step 2: When the application instance starts registration, the dynamic machine code generator initializes and generates a machine ID pool, which is stored in the device memory; Step 3: The dynamic machine code generator obtains and parses the registration metadata information of all application instances of the current application service in the registration service center, and then removes the ID values already used by other application instances from the machine ID pool; Step 4: After the elimination is completed, a machine ID is selected from the machine ID pool as the machine ID of the current application instance; Step 5: Update the selected machine ID to the metadata of the current application instance in the service registration center.
2. The unique machine code dynamic calculation method according to claim 1, characterized in that: In step 1, the dynamic machine code generator is introduced into the application instance in the form of a program dependency package.
3. The unique machine code dynamic calculation method according to claim 1, characterized in that: The default ID value range of the machine ID pool in step 2 is 0-31.
4. The unique machine code dynamic calculation method according to claim 1, characterized in that: Step 3 specifically includes the following steps: Step 3-1, the dynamic machine code generator obtains instance information of all application instances that have been registered with the current application service from the service registration center through the interface provided by the middleware of the service registration center; each instance information includes the registered metadata information; Step 3-2, loop through the metadata of all application instances to determine whether the currently parsed application instance has a wokerid attribute; if so, delete the ID value corresponding to the currently parsed application instance's wokerid attribute from the machine ID pool of the current application instance; otherwise, ignore the corresponding application instance.
5. The unique machine code dynamic calculation method according to claim 4, characterized in that: The middleware of the service registration center in step 3-1 is NACOS.
6. The unique machine code dynamic calculation method according to claim 1, characterized in that: In step 4, the first ID is selected from the machine ID pool as the machine ID of the target application instance.