CN115202901A - Method and device for determining microservice instance identifier, electronic equipment and storage medium - Google Patents

Method and device for determining microservice instance identifier, electronic equipment and storage medium Download PDF

Info

Publication number
CN115202901A
CN115202901A CN202210730396.XA CN202210730396A CN115202901A CN 115202901 A CN115202901 A CN 115202901A CN 202210730396 A CN202210730396 A CN 202210730396A CN 115202901 A CN115202901 A CN 115202901A
Authority
CN
China
Prior art keywords
instance
enqueued
queue
micro
pseudo
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202210730396.XA
Other languages
Chinese (zh)
Inventor
何俊桦
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Maipu Communication Technology Co Ltd
Original Assignee
Maipu Communication Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Maipu Communication Technology Co Ltd filed Critical Maipu Communication Technology Co Ltd
Priority to CN202210730396.XA priority Critical patent/CN115202901A/en
Publication of CN115202901A publication Critical patent/CN115202901A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/54Interprogram communication
    • G06F9/546Message passing systems or structures, e.g. queues
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/90Details of database functions independent of the retrieved data types
    • G06F16/901Indexing; Data structures therefor; Storage structures
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2209/00Indexing scheme relating to G06F9/00
    • G06F2209/54Indexing scheme relating to G06F9/54
    • G06F2209/548Queue

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Databases & Information Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Data Mining & Analysis (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The invention provides a method and a device for determining micro-service instance identification, electronic equipment and a storage medium, and relates to the technical field of software development. The method comprises the following steps: firstly, if an idle queue position exists in a current micro-service instance queue, determining an identifier of an instance to be enqueued according to the idle queue position; if no idle queue position exists in the current micro-service instance queue, acquiring the existence state and the life cycle of each enqueued instance in the micro-service instance queue; then, if the existence state of the enqueued instance is destroyed, determining the identifier of the instance to be enqueued according to the queue position of the destroyed enqueued instance; if the existing state of each enqueued instance is alive, determining a practically destroyed pseudo survival instance from all the enqueued instances according to the survival cycle of each enqueued instance, and determining the identifier of the instance to be enqueued according to the queue position of the pseudo survival instance, thereby determining the unique identifier for different instances under the same microservice.

Description

Method and device for determining micro-service instance identifier, electronic equipment and storage medium
Technical Field
The invention relates to the technical field of software development, in particular to a method and a device for determining micro-service instance identification, electronic equipment and a storage medium.
Background
Micro-services are a variant of a Service-Oriented Architecture (SOA) that advocates dividing a single application into a set of small services that coordinate and interwork with each other to provide the ultimate value to the user.
In a distributed cluster deployment scenario, multi-level microservice splitting is necessarily involved in a system, and deployment of one microservice on different machines brings huge challenges to the system, for example, how to distinguish different instances under the same microservice, and determine a unique identifier for each instance.
Disclosure of Invention
In order to overcome the defects of the prior art, embodiments of the present invention provide a method and an apparatus for determining an identifier of a micro-service instance, an electronic device, and a storage medium, so as to distinguish different instances under the same micro-service and determine a unique identifier for each instance.
Embodiments of the invention may be implemented as follows:
in a first aspect, an embodiment of the present invention provides a method for determining a micro-service instance identifier, where the method includes:
if no idle queue position exists in the micro service instance queue at the current moment, acquiring the existence state and the life cycle of each enqueued instance in the micro service instance queue;
if the existence state of each enqueued instance is alive, determining an actually destroyed pseudo survival instance from all the enqueued instances according to the survival cycle of each enqueued instance;
and determining the identifier of the to-be-enqueued instance according to the queue position of the pseudo survival instance.
Optionally, the step of determining an actually destroyed pseudo survival instance from all the enqueued instances according to the lifetime of each enqueued instance includes:
acquiring the global life cycle of the current moment;
and taking the enqueued instance with the lifetime inconsistent with the global lifetime as the pseudo lifetime instance.
Optionally, the step of determining the identifier of the to-be-enqueued instance according to the queue position of the pseudo live instance includes:
if the pseudo survival instance is one, taking the index of the queue position of the pseudo survival instance as the identifier of the to-be-enqueued instance;
and if the number of the pseudo survival instances is multiple, taking the index of the most front queue position in the queue positions of all the pseudo survival instances as the identifier of the to-be-enqueued instance.
Optionally, after the step of obtaining the existence status and the lifetime of each enqueued instance in the microservice instance queue, the method further comprises:
and if the existence state of at least one enqueue instance is destroyed, taking the index of the most front queue position in the queue positions of all destroyed enqueue instances as the identifier of the to-be-enqueue instance.
Optionally, the method further comprises:
and if the idle queue position exists in the micro-service instance queue at the current moment, taking the index of the most front idle queue position as the identifier of the to-be-enqueued instance.
Optionally, before the step of obtaining the existence status and the lifetime of each enqueued instance in the microservice instance queue if there is no idle queue location in the microservice instance queue at the current time, the method further includes:
acquiring an enqueuing request of the to-be-enqueued instance;
setting the existing state of the to-be-enqueued instances as survival based on the enqueuing request, and judging whether the number of the enqueued instances in the micro-service instance queue at the current moment is less than a preset value or not;
if the number of the enqueue instances is smaller than a preset value, judging that an idle queue position exists in the micro-service instance queue;
and if the number of the enqueued examples is equal to a preset value, judging that no idle queue position exists in the microservice example queue.
Optionally, the method further comprises:
updating a global life cycle so that the life cycle of each enqueued instance in the micro-service instance queue is synchronized with the global life cycle according to a preset mechanism;
and adding the instances to be enqueued into the micro service instance queue according to the identifiers of the instances to be enqueued.
In a second aspect, an embodiment of the present invention further provides a micro service instance identifier determining apparatus, where the apparatus includes:
the acquiring module is used for acquiring the existence state and the life cycle of each enqueued instance in the micro service instance queue if the idle queue position does not exist in the micro service instance queue at the current moment;
a processing module to:
if the existence state of each enqueued instance is alive, determining an actually destroyed pseudo survival instance from all the enqueued instances according to the survival cycle of each enqueued instance;
and determining the identifier of the to-be-enqueued instance according to the queue position of the pseudo survival instance.
In a third aspect, an embodiment of the present invention provides an electronic device, including a memory and a processor, where the memory stores a computer program, and the processor implements the method for determining the micro-service instance identifier in the first aspect when executing the computer program.
In a fourth aspect, the present invention provides a computer-readable storage medium, which stores a computer program, and the computer program, when executed by a processor, implements the method for determining microservice instance identifier as in the first aspect.
Compared with the prior art, the method, the device, the electronic equipment and the storage medium for determining the micro-service instance identifier provided by the embodiment of the invention have the advantages that firstly, the micro-service instance queue and the global life cycle are predefined, the global life cycle needs to be updated before each micro-service instance enters the micro-service instance queue, each micro-service instance entering the micro-service instance queue can be kept synchronous with the global life cycle at regular intervals, and if the micro-service instance is not in a living state actually, the micro-service instance identifier cannot be kept synchronous with the global life cycle; then, if no idle queue position exists in the micro service instance queue at the current moment, acquiring the existence state and the life cycle of each enqueued instance in the micro service instance queue; secondly, if the existing state of each enqueued instance is alive, determining a practically destroyed pseudo-survival instance from all the enqueued instances according to the survival cycle of each enqueued instance; and finally, determining the identifier of the to-be-enqueued instance according to the queue position of the pseudo survival instance. According to the embodiment of the invention, under the condition that no idle queue position exists in the micro-service instance queue and the existing state of each enqueued instance is in existence, the actual destroyed pseudo survival instance is found out according to the life cycle of each enqueued instance, and the identifier of the to-be-enqueued instance is determined according to the queue position of the pseudo survival instance, so that different instances under the same micro-service are distinguished, and the unique identifier is determined for each instance.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings needed to be used in the embodiments will be briefly described below, it should be understood that the following drawings only illustrate some embodiments of the present invention and therefore should not be considered as limiting the scope, and for those skilled in the art, other related drawings can be obtained according to the drawings without inventive efforts.
Fig. 1 is a schematic structural diagram of a distributed cluster according to an embodiment of the present invention.
Fig. 2 is a first flowchart illustrating a method for determining an identifier of a micro-service instance according to an embodiment of the present invention.
Fig. 3 is a schematic diagram of a micro-service instance queue according to an embodiment of the present invention.
Fig. 4 is a first example of a method for determining an identifier of a micro-service instance according to an embodiment of the present invention.
Fig. 5 is a schematic flowchart of a second method for determining an identifier of a micro-service instance according to an embodiment of the present invention.
Fig. 6 is a second example of a method for determining an identifier of a micro-service instance according to an embodiment of the present invention.
Fig. 7 is a third example of a method for determining an identifier of a micro-service instance according to an embodiment of the present invention.
Fig. 8 is a third schematic flowchart of a method for determining an identifier of a micro service instance according to an embodiment of the present invention.
Fig. 9 is an example four of a method for determining an identifier of a micro-service instance according to an embodiment of the present invention.
Fig. 10 is a functional unit block diagram of a micro-service instance identifier determining apparatus according to an embodiment of the present invention.
Fig. 11 is a schematic block diagram of a structure of an electronic device according to an embodiment of the present invention.
An icon: 100-micro service instance identification determination means; 101-an acquisition module; 102-a processing module; 200-an electronic device; 210-a memory; 220-processor.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all embodiments of the present invention. The components of embodiments of the present invention generally described and illustrated in the figures herein may be arranged and designed in a wide variety of different configurations.
Thus, the following detailed description of the embodiments of the present invention, presented in the figures, is not intended to limit the scope of the invention, as claimed, but is merely representative of selected embodiments of the invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
It should be noted that: like reference numbers and letters refer to like items in the following figures, and thus, once an item is defined in one figure, it need not be further defined and explained in subsequent figures.
It should be noted that the features of the embodiments of the present invention may be combined with each other without conflict.
"microservices" are a variant of a Service-Oriented Architecture (SOA) that advocates dividing a single application into a set of small services that coordinate and interwork with each other to provide ultimate value to the user. Each service runs in an independent process, and a lightweight communication mechanism, such as RESTful API based on HTTP, is adopted between the services to realize mutual communication. Each service is built around a specific business and can be deployed independently to a production environment, a production-like environment, and the like. In addition, a unified and centralized service management mechanism should be avoided as much as possible, and for a specific service, a suitable language and tool should be selected and constructed according to the context. When the same micro-service is deployed on multiple devices, the micro-service program deployed on each device may be referred to as a micro-service instance (hereinafter referred to as an "instance").
As shown in fig. 1, the distributed cluster includes a plurality of Remote dictionary servers (Remote dictionary servers, hereinafter referred to as Redis), for any Redis, a plurality of nodes are set on the Redis, and each node is deployed with an instance. The client can communicate with the Redis, can create, destroy and call the instance at any node on the Redis, and when the client creates the instance at any node, the existence state of the instance is set to be 'alive', and when the client destroys the instance at any node, the existence state of the instance is updated from 'alive' to 'destroyed'.
The client may be a server, a personal computer, a notebook computer, or the like, or may be one or more program modules on one device, or a virtual machine or a container running on one device, or may be a cluster formed by multiple devices, for example, may be a general term for multiple program modules distributed on multiple devices.
In the distributed cluster, a plurality of instances of one microservice are mutually copies and have uniqueness, so that a unique identifier needs to be determined for each instance, so that a client can quickly call a target instance according to the identifier.
In view of this, embodiments of the present invention provide a method for determining an identifier of a microservice instance, so as to determine a unique identifier for different instances of the same microservice in a distributed cluster environment, which will be described in detail below.
Referring to fig. 2, fig. 2 is a schematic flow chart of a method for determining a micro-service instance identifier according to an embodiment of the present invention, where the method for determining a micro-service instance identifier is applied to the client shown in fig. 1 and may include steps S101 to S103.
S101, if no idle queue position exists in the micro service instance queue at the current moment, the existence state and the life cycle of each enqueued instance in the micro service instance queue are obtained.
The method comprises the steps that a micro-service instance queue is pre-established in Redis by a client and comprises a plurality of queue positions, when an instance on any node is started successfully, the Redis sends an enqueue request about the instance to the client, the client determines the queue position in the micro-service instance queue occupied by the instance by responding to the enqueue request, and an index of the occupied queue position is used as an identifier of the instance.
Alternatively, the micro-service instance queue may be circular, such that the number of queue locations is limited. Meanwhile, in order for the client to quickly find the instance of the call, the index of the queue position may be set to an integer. As shown in fig. 3, the microservices instance queue has four queue positions indexed by 1, 2, 3, and 4, respectively.
The method for determining the micro service instance identifier provided by the embodiment of the invention is described below based on a ring-shaped micro service instance queue.
The enqueued instance refers to an instance occupying a queue position in the micro service instance queue, and it can be understood that a queue position not occupied by an instance in the micro service instance queue is an idle queue position.
As shown in FIG. 3, queue position-1 is occupied by an instance, queue position-2 is also occupied by an instance, and queue positions-3 and-4 are not occupied by an instance, then the instances occupying queue position-1 and queue position-2 are enqueued instances, and queue position-3 and queue position-4 are free queue positions.
The enqueued instance may have the following attributes: identity, presence status, and life cycle.
In an embodiment of the present invention, the identification may be an index of a queue position in the microservice instance queue occupied by the enqueue instance. As shown in FIG. 3, the index of queue location-1 is 1, and the identity of the instance occupying queue location-1 is 1.
The presence state of the enqueued instance may be "alive" or "destroyed". As shown in FIG. 3, the presence state of the enqueued instance occupying queue position-1 is "alive", and the presence state of the enqueued instance occupying queue position-2 is "destroyed".
The lifetime begins when an enqueued instance is created and ends when an enqueued instance is destroyed, whose value may be represented by the system timestamp of Redis.
Since the clock of Redis may lag behind the normal time and may also lead the normal time, the lifetime of the enqueued instance is inaccurate. Thus, the client creates a global lifetime counter in Redis to count the lifetime of the enqueued instances. Before any instance enters the micro service instance queue, the value of the global life cycle counter is triggered to be updated (for example, one is added, and a person skilled in the art can easily think that the updating mode is not limited to this), meanwhile, after any instance successfully enters the micro service instance queue, the client starts a Watchdog service (Watchdog), synchronizes the life cycles of all the enqueued instances in the micro service instance queue to the value of the global life cycle counter according to a fixed period, but if the enqueued instance is destroyed for any reason, the life cycle of the enqueued instance lags behind the other enqueued instances whose existing state is "alive".
S102, if the existence state of each enqueued instance is survival, determining the actually destroyed pseudo survival instance from all the enqueued instances according to the survival cycle of each enqueued instance.
The pseudo survival instance refers to an enqueued instance whose existence state is "alive", but which has actually been destroyed. The pseudo-live instance is generated because the enqueued instance is restarted or the node where the enqueued instance is located is abnormally powered off, and the existence state of the enqueued instance cannot be updated from "live" to "destroyed", but actually is destroyed.
It will be appreciated that the pseudo-live instance has actually been destroyed and its lifetime cannot be synchronized with the value of the global lifetime counter, so that the pseudo-live instance can be determined from all enqueued instances based on the lifetime of each enqueued instance.
As a possible implementation manner, the step S102 of determining, according to the lifetime of each enqueued instance, a pseudo-lifetime instance that is actually destroyed from all enqueued instances may include:
firstly, acquiring the global life cycle of the current moment;
then, the enqueued instance with the lifetime inconsistent with the global lifetime is taken as the pseudo-lifetime instance.
As an implementation manner of the embodiment of the present invention, the value of the global lifetime counter is continuously increased, and the lifetime of the pseudo lifetime instance is smaller than the lifetime of other normally alive enqueued instances because the lifetime of the pseudo lifetime instance cannot be kept synchronous with the value of the global lifetime counter. Therefore, an enqueued instance with a lifetime inconsistent with the global lifetime may be used as a pseudo-lifetime instance.
It can be understood that there may be one or more enqueued instances deployed nodes where a restart occurs or an abnormal power failure occurs, meaning that there may be one or more enqueued instances whose lifetime is inconsistent with the global lifetime, and then there may be one or more determined pseudo-lifetime instances.
S103, determining the identifier of the to-be-enqueued instance according to the queue position of the pseudo survival instance.
The to-be-enqueued instance can be a newly created instance, and the to-be-enqueued instance occupies the queue position of the eliminated pseudo live instance, so that the to-be-enqueued instance is identified as the index of the queue position of the eliminated pseudo live instance.
For example, referring to fig. 4, four queue positions of the micro service instance queue are occupied, wherein an enqueue instance occupying queue position-3 is determined as a pseudo-live instance, the enqueue instance is eliminated from the micro service instance queue, and then an index of the free queue position-3 is allocated to the to-be-enqueue instance, so that the to-be-enqueue instance is identified as 3.
Since there may be one or more pseudo survival instances determined according to the survival period, for these two cases, the embodiments of the present invention provide two possible implementation manners of step S103.
The first method is as follows: if the pseudo survival instance is one, taking the index of the queue position of the pseudo survival instance as the identifier of the to-be-enqueued instance;
the second method comprises the following steps: and if the number of the pseudo-survival instances is multiple, taking the index of the most front queue position in the queue positions of all the pseudo-survival instances as the identification of the to-be-enqueued instance.
It can be understood that if the pseudo live instance is one, the pseudo live instance out-of-queue micro service instance queue can be directly eliminated, and the vacated queue position will be occupied by the to-be-enqueued instance, so that the index of the vacated queue position can be used as the identification of the to-be-enqueued instance.
In the embodiment of the invention, the queue position with the minimum index value in the micro-service instance queue is taken as a starting point, the queue position with the maximum index value is taken as an end point, and the closer the random queue position is to the starting point, the closer the random queue position is to the front of the random queue position. Therefore, if a plurality of pseudo survival instances exist, the pseudo survival instance with the most front queue position can be eliminated from the micro-service instance queue, the to-be-enqueued instance occupies the empty queue position, and the identifier of the to-be-enqueued instance is the index of the queue position occupied by the to-be-enqueued instance.
Since the time when the multiple enqueued instances restart or the time when the multiple nodes deployed with the enqueued instances abnormally power off may be different, the sizes of the life cycles of the multiple pseudo life instances may also be different, and it can be understood that the smaller the life cycle of the pseudo life instance is, the earlier it exists in the micro service instance queue.
Therefore, in a possible implementation manner, if there are multiple pseudo survival instances, the pseudo survival instance with the smallest life cycle and the most front queue position may be eliminated from the micro-service instance queue, the to-be-enqueued instance will occupy the empty queue position, and the identifier of the to-be-enqueued instance is an index of the queue position occupied by the to-be-enqueued instance.
In a possible situation, when the identifier is determined for the to-be-enqueued instance, there may be an already-enqueued instance with the presence state of "destroyed" in the micro service instance queue, and at this time, the identifier of the to-be-enqueued instance may be determined according to the queue position of the already-enqueued instance with the presence state of "destroyed".
Therefore, referring to fig. 5 on the basis of fig. 2, after step S101, the method for identifying a micro service instance provided by the embodiment of the present invention may further include step S104.
S104, if the existence state of at least one already enqueued instance is destroyed, taking the index of the most front queue position in the queue positions of all destroyed already enqueued instances as the identifier of the to-be-enqueued instance.
When the enqueued instance is destroyed by the client, the client updates the existence state of the enqueued instance from 'alive' to 'destroyed'.
When the micro-service instance queue has an enqueue instance with a destroyed state, if the number of the enqueue instances is one, the enqueue instance can be directly eliminated from the micro-service instance queue, if the number of the enqueue instances is multiple, the micro-service instance queue with the most front queue position can be eliminated, and the index of the empty queue position can be used as the identifier of the to-be-enqueued instance.
For example, referring to fig. 6, four queue positions of the micro-service instance queue are all occupied, wherein the existence status of the enqueue instance occupying queue position-2 is "destroyed", the micro-service instance queue is eliminated, the index of the queue position-2 which is free is allocated to the to-be-enqueued instance, and then the identifier of the to-be-enqueued instance is 2.
In another possible scenario, when determining the identifier for the to-be-enqueued instance, there may be a free queue position in the micro service instance queue, and at this time, the identifier of the to-be-enqueued instance may be determined according to the free queue position.
Referring to fig. 5 again, the method for identifying a micro-service instance provided in the embodiment of the present invention further includes step S105 parallel to the flow of steps S101 to S103.
And S105, if the idle queue position exists in the micro service instance queue at the current moment, taking the index of the idle queue position at the most front as the identifier of the to-be-enqueued instance.
Wherein, when there is a free queue position in the micro-service instance queue, it means that there is no need to eliminate any enqueue instance. And if the number of the idle queue positions is multiple, the index of the queue position is directly used as the identifier of the to-be-enqueued instance, and if the number of the idle queue positions is multiple, the index of the most front queue position is used as the identifier of the to-be-enqueued instance.
For example, referring to FIG. 7, of the four queue positions of the micro-service instance queue, queue position-1 and queue position-2 are occupied and queue position-3 and queue position-4 are unoccupied. Since queue position-3 is closest to the beginning of the queue (queue position-1) and is the most front of the two free queue positions, the index of queue position-3 is assigned to the to-be-enqueued instance, and the to-be-enqueued instance is identified as 3.
Since the client needs to respond to the enqueue request of the to-be-enqueued instance, after the identification of the to-be-enqueued instance is determined, the to-be-enqueued instance needs to be added into the microservice instance queue.
Referring to fig. 5 again, after step S103, step S104 or step S105, the method for identifying a micro service instance provided in the embodiment of the present invention further includes steps S106 to S107.
And S106, updating the global life cycle so that the life cycle of each enqueued instance in the micro-service instance queue is synchronized with the global life cycle according to a preset mechanism.
The preset mechanism means that the client starts the Watchdog, and synchronizes the survival cycles of all enqueued instances in the micro-service instance queue into the value of the global survival cycle counter according to a fixed cycle.
Before adding the to-be-enqueued instance into the micro-service instance queue, the global lifetime needs to be updated, for example, the value of the global lifetime counter is triggered to be increased by one, which is convenient for the Watchdog to perform synchronous operation on the lifetime of each enqueued instance in the micro-service instance queue.
It will be appreciated that the lifetime of an enqueued instance whose presence state is "alive", which has not actually been destroyed, may remain synchronized with the value of the global lifetime counter, while the lifetime of an enqueued instance whose presence state is "present", which has actually been destroyed (i.e., a dummy live instance), and whose presence state is "destroyed", may not remain synchronized with the value of the global lifetime counter.
S107, adding the to-be-enqueued instance into the microservice instance queue according to the identifier of the to-be-enqueued instance.
Because the identifier of the to-be-enqueued instance is the index of one queue position in the micro-service instance queue, the to-be-enqueued instance can be added into the corresponding queue position in the micro-service instance queue according to the identifier.
From the above, a unique and bounded integer identification can be determined for each enqueue instance by an integer index of the queue positions free in the pre-established circular micro-service instance queue. When the identifier is determined for the instance, a free queue position may exist in the micro-service instance queue, or the free queue position may not exist. Thus, prior to determining the identification for the instance, a determination may be made as to whether there is a free queue location in the microservice instance queue.
Therefore, referring to fig. 8 on the basis of fig. 5, before steps S101 and S105, steps S201 to S204 of the identification method for micro-service instance provided by the embodiment of the present invention are performed.
S201, an enqueue request of an instance to be enqueued is obtained.
When the to-be-enqueued instance is started successfully, redis sends an enqueue request about the to-be-enqueued instance to the client.
S202, based on the enqueue request, setting the existence state of the to-be-enqueued instance as survival, and judging whether the number of enqueued instances in the microservice instance queue at the current moment is less than a preset value.
The preset value refers to the total number of queue positions in the microservice instance queue.
Since one enqueued instance occupies one queue position, the number of enqueued instances is equal to the number of occupied queue positions, while the number of occupied queue positions cannot be greater than the total number of queue positions, it can be understood that the number of enqueued instances is less than or equal to a preset value.
When the existence state of the to-be-enqueued instance is set to be 'living', whether an idle queue position exists can be judged according to whether the number of already-enqueued instances in the micro-service instance queue at the current moment is smaller than a preset value. If the number of enqueued instances in the micro-service instance queue at the current moment is less than the preset value, step S203 is executed. If the number of enqueued instances in the micro-service instance queue at the current moment is equal to the preset value, step S204 is executed.
S203, judging that the queue position of the micro-service instance queue is free.
And S204, judging that no idle queue position exists in the micro-service instance queue.
It should be noted that the to-be-enqueued instance may not only be a newly created instance, but also be an instance that is newly brought online due to restart or abnormal power failure of the server, and in order to avoid a phenomenon that the same integer identifier is occupied by multiple to-be-enqueued instances when the above method flow is applied to multiple to-be-enqueued instances at the same time, in the embodiment of the present invention, the above method flow is executed by using a Redis Lua script.
The method for determining the micro-service instance identifier is further described below by using an application example shown in fig. 9 by taking a Redis Lua script as an example.
As shown in FIG. 9, the client deploys different nodes of three instances on Redis, namely worker-1, worker-2 and worker-3, and sets the number of queue positions in the micro service instance queue on Redis to be 3. During the initialization phase, the value of the global lifetime counter on Redis is 0.
Because the Redis Lua script is adopted, the correctness of a processing result cannot be influenced no matter three workers are started simultaneously or sequentially, so that the method for determining the micro-service embodiment identifier provided by the embodiment of the invention is explained by adopting the sequential starting of worker-1, worker-2 and worker-3.
Firstly, when worker-1 is started, the client sets the existence state of the client to be 'in existence', the survival period value is 0, and at this time, an integer identifier is not distributed yet.
If worker-1 is started successfully, because all queue positions in the micro-service instance queue are idle, worker-1 occupies the most front queue position-1, which is identified as the index of the queue position-1, before worker-1 enters the micro-service queue, the value of the global life cycle counter is accumulated by 1, the client starts watchdog at regular time to update the life cycle of worker-1 to the value of the global life cycle counter, and at this time, the life cycle of worker-1 is 1.
If the worker-1 fails to start, the restart is waited, and the starting sequence is arranged at the last bit.
And similarly, the worker-2 and the worker-3 are started according to the flow until all three workers successfully enter the micro-service instance queue, the value of the global life cycle counter is accumulated to 3, and the client regularly updates the life cycle of the worker in the survival state to the value of the global life cycle counter through the watchdog.
Suppose that worker-1, worker-2 and worker-3 occupy queue position-1, queue position-2 and queue position-3 in the micro service instance queue, respectively, and the life cycle is 3.
If the worker-2 is in unexpected power failure and the worker-3 is normally closed, the existence state of the worker-2 is destroyed without being updated to be destroyed, while in the micro-service instance queue, the existence state of the worker-2 is still alive, the existence state of the worker-3 can be updated, and in the micro-service instance queue, the existence state of the worker-3 is destroyed.
At the moment, if the worker-2 and the worker-3 are started again at the same time, in order to facilitate distinguishing, the worker-2 and the worker-3 which are started again are named as worker-4 and worker-5 again, and under the action of the Redis Lua script, the identifications of the worker-4 and the worker-5 cannot be determined at the same time.
Firstly, for worker-4, because three queue positions in the micro service instance queue are occupied, a destroyed instance needs to be eliminated, and the worker-4 can enter the queue. At this time, the existence state of worker-3 is 'destroyed', then it is eliminated preferentially, and worker-4 occupies the queue position-3 which is free in the micro service instance queue to complete the enqueue, and the identifier is the index of the queue position-3.
Before the worker-4 enters the micro service queue, the value of a global life cycle counter on Redis is accumulated to 4, after the worker-4 enters the queue, the client regularly updates the life cycles of the worker-1 and the worker-4 to 4 through watchdog, the worker-2 is actually destroyed, and the life cycle of the worker-2 cannot be kept synchronous with the value of the global life cycle counter and still is 3.
Then, for worker-5, as three queue positions in the micro service instance queue are occupied, a destroyed instance needs to be eliminated, and the worker-5 can enter the queue. At the moment, the existence states of worker-1, worker-2 and worker-4 are all 'existence', because the worker-2 is actually destroyed, the life cycle of the worker-2 can not keep synchronous with the value of the global life cycle counter on the Redis and is smaller than the life cycles of the worker-1 and the worker-4, the worker-2 is eliminated, the worker-5 occupies the idle queue position-2 in the service instance queue to complete the enqueue, and the identifier of the worker-2 is the index of the queue position-2.
Compared with the prior art, the embodiment of the invention has the following effects:
(1) An annular micro-service instance queue is established in Redis in advance, and when an instance needing to be enqueued exists, an index of an idle queue position in the micro-service instance queue, or a queue position occupied by an enqueued instance in a destroyed state, or a queue position occupied by a pseudo-survival instance is used as an instance identifier, so that unique and bounded identifiers are determined for each instance.
(2) In order to enable the client to call the instances quickly, the index of the queue position in the micro-service instance queue is set to be an integer, and the identification allocated to each instance is an integer identification.
(3) A global life cycle counter is set in Redis, before an instance on any node enters a micro service queue, the value of the global life cycle counter is triggered to be increased by one, and the life cycle of each instance in the survival state is kept synchronous with the value of the global life cycle counter through watchdog, so that the error of system time on the Redis is avoided, and the life cycle of the instance is inaccurate.
(4) And when no idle queue position exists in the micro-service instance queue and the existence state of each enqueued instance is in existence, finding out the actual destroyed pseudo survival instance by comparing the survival cycle of each enqueued instance.
In order to execute the corresponding steps in the above method embodiments and various possible embodiments, an implementation manner of the micro-service example identifier determining apparatus is given below. Referring to fig. 10, the micro-service instance identifier determining apparatus 100, applied to the client in fig. 1, may include an obtaining module 101 and a processing module 102.
An obtaining module 101, configured to obtain a presence state and a lifetime of each enqueued instance in a microservice instance queue if an idle queue position does not exist in the microservice instance queue at a current time;
the processing module 102 is configured to determine, according to a lifetime of each enqueued instance, a pseudo-survivor instance that has been actually destroyed from all the enqueued instances if the existence status of each enqueued instance is alive; and determining the identifier of the to-be-enqueued instance according to the queue position of the pseudo survival instance.
Optionally, the processing module 102 is specifically configured to obtain a global life cycle at the current time; and taking the enqueued instance with the life cycle inconsistent with the global life cycle as a pseudo life instance.
Optionally, the processing module 102 is further specifically configured to, if the pseudo live instance is one, use an index of a queue position of the pseudo live instance as an identifier of the to-be-enqueued instance; and if the number of the pseudo-survival instances is multiple, taking the index of the most front queue position in the queue positions of all the pseudo-survival instances as the identification of the to-be-enqueued instance.
Optionally, the processing module 102 is further configured to, if the existence status of at least one already enqueued instance is destroyed, use an index of a top queue position of all queue positions of the already destroyed already enqueued instances as an identifier of the to-be-enqueued instance.
Optionally, the processing module 102 is further configured to, if there is a free queue position in the micro-service instance queue at the current time, use an index of the top free queue position as an identifier of the to-be-enqueued instance.
Optionally, the obtaining module 101 is further configured to obtain an enqueue request of an instance to be enqueued; setting the existence state of the to-be-enqueued instance as survival based on the enqueuing request, and judging whether the number of enqueued instances in the microservice instance queue at the current moment is less than a preset value or not; if the number of the enqueue instances is smaller than a preset value, judging that an idle queue position exists in the micro-service instance queue; and if the number of the enqueue instances is equal to the preset value, judging that no idle queue position exists in the micro-service instance queue.
Optionally, the processing module 102 is further configured to update the global lifetime, so that the lifetime of each enqueued instance in the micro-service instance queue is synchronized with the global lifetime according to a preset mechanism; and adding the instances to be enqueued into the microservice instance queue according to the identifiers of the instances to be enqueued.
It can be clearly understood by those skilled in the art that, for convenience and brevity of description, the specific working process of the apparatus for determining the identifier of the micro service instance described above may refer to the corresponding process in the foregoing method embodiment, and is not described herein again.
Further, an embodiment of the present invention also provides an electronic device 200, which may be a client in fig. 1. Referring to fig. 11, fig. 11 is a schematic block diagram of a structure of an electronic device 200 according to an embodiment of the present invention, where the electronic device 200 may include a memory 210 and a processor 220.
The processor 220 may be a general-purpose Central Processing Unit (CPU), a microprocessor, an Application-Specific Integrated Circuit (ASIC), or one or more Integrated circuits for controlling the execution of the program of the method for determining the micro-service instance identifier provided by the above method embodiments.
The MEMory 210 may be, but is not limited to, a ROM or other type of static storage device that can store static information and instructions, a RAM or other type of dynamic storage device that can store information and instructions, an Electrically Erasable programmable Read-Only MEMory (EEPROM), a compact-Read-Only MEMory (CD-ROM) or other optical disk storage, optical disk storage (including compact disk, laser disk, optical disk, digital versatile disk, blu-ray disk, etc.), magnetic disk storage or other magnetic storage devices, or any other medium that can be used to carry or store desired program code in the form of instructions or data structures and that can be accessed by a computer. The memory 210 may be self-contained and coupled to the processor 220 via a communication bus. Memory 210 may also be integrated with processor 220. Memory 210 is used to store, among other things, machine-executable instructions for performing aspects of the present application. Processor 220 is operative to execute machine executable instructions stored in memory 210 to implement the method embodiments described above.
Embodiments of the present invention further provide a computer-readable storage medium containing a computer program, where the computer program can be used to execute relevant operations in the method for determining the identity of a micro-service instance provided in the foregoing method embodiments.
To sum up, the method, the apparatus, the electronic device, and the storage medium for determining the micro-service instance identifier according to the embodiments of the present invention first predefine a micro-service instance queue and a global life cycle, before each micro-service instance enters the micro-service instance queue, the global life cycle needs to be updated first, each micro-service instance entering the micro-service instance queue will be synchronized with the global life cycle periodically, and if the micro-service instance is not in a live state, the micro-service instance cannot be synchronized with the global life cycle; then, if no idle queue position exists in the micro service instance queue at the current moment, acquiring the existence state and the life cycle of each enqueued instance in the micro service instance queue; then, if the existence state of each enqueued instance is alive, determining a pseudo survival instance which is actually destroyed from all the enqueued instances according to the survival cycle of each enqueued instance; and finally, determining the identifier of the to-be-enqueued instance according to the queue position of the pseudo survival instance. According to the embodiment of the invention, under the condition that no idle queue position exists in the micro-service instance queue and the existence state of each enqueued instance is in existence, the actual destroyed pseudo survival instance is found out according to the life cycle of each enqueued instance, and the identifier of the to-be-enqueued instance is determined according to the queue position of the pseudo survival instance, so that different instances under the same micro-service are distinguished, and the unique identifier is determined for each instance.
The above description is only for the specific embodiments of the present invention, but the scope of the present invention is not limited thereto, and any changes or substitutions that can be easily conceived by those skilled in the art within the technical scope of the present invention are included in the scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.

Claims (10)

1. A method for determining a micro-service instance identifier, the method comprising:
if no idle queue position exists in the micro-service instance queue at the current moment, acquiring the existence state and the life cycle of each enqueued instance in the micro-service instance queue;
if the existence state of each enqueued instance is alive, determining an actually destroyed pseudo survival instance from all the enqueued instances according to the survival cycle of each enqueued instance;
and determining the identifier of the to-be-enqueued instance according to the queue position of the pseudo survival instance.
2. The method of claim 1, wherein said step of determining an actual destroyed pseudo-live instance from all of said enqueued instances based on a life cycle of each of said enqueued instances comprises:
acquiring the global life cycle of the current moment;
and taking the enqueued instance with the lifetime inconsistent with the global lifetime as the pseudo-lifetime instance.
3. The method of claim 1, wherein the step of determining an identity of an instance to enqueue based on the queue position of the pseudo survivor instance comprises:
if the pseudo survival instance is one, taking the index of the queue position of the pseudo survival instance as the identifier of the to-be-enqueued instance;
and if the number of the pseudo survival instances is multiple, taking the index of the most front queue position in the queue positions of all the pseudo survival instances as the identifier of the to-be-enqueued instance.
4. The method of claim 1, wherein after the step of obtaining the presence status and lifetime of each enqueued instance in the microservice instance queue, the method further comprises:
and if the existence state of at least one enqueue instance is destroyed, taking the index of the most front queue position in the queue positions of all destroyed enqueue instances as the identifier of the to-be-enqueue instance.
5. The method of claim 1, wherein the method further comprises:
and if the queue of the micro-service instance at the current moment has an idle queue position, taking the index of the most front idle queue position as the identifier of the to-be-enqueued instance.
6. The method of claim 1, wherein prior to the step of obtaining a presence status and a lifetime of each enqueued instance in the microservice instance queue if there is no free queue location in the microservice instance queue at the current time, the method further comprises:
acquiring an enqueuing request of the to-be-enqueued instance;
setting the existing state of the to-be-enqueued instance as survival based on the enqueuing request, and judging whether the number of the enqueued instances in the micro-service instance queue at the current moment is less than a preset value;
if the number of the enqueue instances is smaller than a preset value, judging that an idle queue position exists in the micro-service instance queue;
and if the number of the enqueued examples is equal to a preset value, judging that no idle queue position exists in the microservice example queue.
7. The method of any one of claims 1-6, further comprising:
updating a global life cycle so that the life cycle of each enqueued instance in the micro-service instance queue is synchronized with the global life cycle according to a preset mechanism;
and adding the instances to be enqueued into the micro service instance queue according to the identifiers of the instances to be enqueued.
8. An apparatus for microservice instance identity determination, the apparatus comprising:
the acquiring module is used for acquiring the existence state and the life cycle of each enqueued instance in the micro service instance queue if the idle queue position does not exist in the micro service instance queue at the current moment;
a processing module to:
if the existence state of each enqueued instance is alive, determining an actually destroyed pseudo survival instance from all the enqueued instances according to the survival cycle of each enqueued instance;
and determining the identifier of the to-be-enqueued instance according to the queue position of the pseudo survival instance.
9. An electronic device, comprising a memory storing a computer program and a processor that implements the microservice instance identity determination method according to any of claims 1-7 when executing the computer program.
10. A computer-readable storage medium, in which a computer program is stored which, when being executed by a processor, carries out the microservice instance identity determination method according to any one of claims 1 to 7.
CN202210730396.XA 2022-06-24 2022-06-24 Method and device for determining microservice instance identifier, electronic equipment and storage medium Pending CN115202901A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210730396.XA CN115202901A (en) 2022-06-24 2022-06-24 Method and device for determining microservice instance identifier, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210730396.XA CN115202901A (en) 2022-06-24 2022-06-24 Method and device for determining microservice instance identifier, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN115202901A true CN115202901A (en) 2022-10-18

Family

ID=83577749

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210730396.XA Pending CN115202901A (en) 2022-06-24 2022-06-24 Method and device for determining microservice instance identifier, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN115202901A (en)

Similar Documents

Publication Publication Date Title
CN109194538B (en) Testing method, device, server and storage medium based on distributed coordination
CN107733708B (en) Equipment parameter configuration method and device, computer equipment and storage medium
CN112214330A (en) Method and device for deploying master nodes in cluster and computer-readable storage medium
CN113037794B (en) Method, device and system for computing resource allocation scheduling
CN112865992B (en) Method and device for switching master nodes in distributed master-slave system and computer equipment
CN107341054B (en) Task execution method and device and computer readable storage medium
WO2015057188A1 (en) Package dependency maps for distributed computing
CN111416839B (en) Cluster environment timing task processing method, system, device and storage medium
CN106844055B (en) Task execution method and device
CN110619226A (en) Platform-based data processing method, system, equipment and storage medium
CN113204353B (en) Big data platform assembly deployment method and device
CN106250487B (en) Database concurrency control method and device
US9444885B2 (en) Workflow processing in a distributed computing environment
CN113656147A (en) Cluster deployment method, device, equipment and storage medium
CN111190823A (en) UI automation test method, electronic device and computer readable storage medium
CN108536541B (en) Process engine object processing method and device
CN112865995B (en) Distributed master-slave system
CN115632944B (en) Node configuration method, device, equipment, readable storage medium and server
CN115202901A (en) Method and device for determining microservice instance identifier, electronic equipment and storage medium
CN113342499B (en) Distributed task calling method, device, equipment, storage medium and program product
CN111414242B (en) Method, device, server, system and storage medium for operating state machine
CN114625515A (en) Task management method, device, equipment and storage medium
CN111221847B (en) Monitoring data storage method, device and computer readable storage medium
CN108196962B (en) Method and device for realizing calling of interfaces of different versions
CN112631727B (en) Monitoring method and device for pod group pod

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication