Disclosure of Invention
In order to overcome the defects of the traditional service calling process under the micro-service architecture, the invention can provide a service calling method, a service calling device and a computer storage medium for the micro-service architecture, thereby solving at least one problem in the prior art.
To achieve the above technical object, the present invention discloses a service invocation method for micro service architecture, which may include, but is not limited to, at least one of the following steps.
A target service corresponding to the service is selected.
And acquiring an entry address list of the container according to the condition that the service is an out-of-container service and the target service is an in-container service.
And screening out the target address from the entry address list.
Modifying a remote procedure call address of a target service based on the target address.
And calling the target service by using the modified remote procedure call address.
Further, the service calling method further comprises the step of performing service registration in advance.
The step of performing service registration in advance includes:
and judging whether the service to be registered is started in the container or not when the service to be registered is started.
Writing a first parameter into metadata for registration when the service to be registered is started in a container; or writing a second parameter into the metadata for registration when the service to be registered is started outside the container.
The first parameter is used for representing that the currently registered service is an in-container service, and the second parameter is used for representing that the currently registered service is an out-of-container service.
Further, determining that the service is an out-of-container service and the target service is an in-container service comprises:
and acquiring first metadata generated when the service is registered in advance and second metadata generated when the target service is registered in advance.
And determining that the service is an out-of-container service and the target service is an in-container service according to the first metadata having the second parameter and the second metadata having the first parameter.
Further, the determining whether the service to be registered is started in the container when starting includes:
and acquiring the environment variable of the service to be registered when the service is started.
And judging whether the environment variable has a container identifier.
And determining that the service to be registered is started in the container according to the container identifier in the environment variable, or determining that the service to be registered is started outside the container according to the container identifier in the environment variable.
Further, the modifying the remote procedure call address of the target service based on the target address comprises:
and modifying the remote procedure call address of the target service into an address formed by splicing the service identification number and the target address.
Further, the service calling method further includes:
and directly calling the target service by using the access address of the target service according to the condition that the service is an out-of-container service and the target service is an out-of-container service.
Further, the service calling method further includes:
and calling the target service by using the access address of the target service in the container according to the condition that the service is the in-container service and the target service is the in-container service.
Further, the service calling method further includes:
and calling the target service by using the access address provided by the target service during registration according to the condition that the service is an in-container service and the target service is an out-of-container service.
Still other embodiments of the present invention disclose a service invocation apparatus for a microservice architecture, which may include, but is not limited to, a target service selection module, an address list acquisition module, a target address screening module, a call address modification module, and a target service invocation module.
And the target service selection module is used for selecting the target service corresponding to the service.
And the address list acquisition module is used for acquiring an entry address list of the container according to the condition that the service is an out-of-container service and the target service is an in-container service.
And the target address screening module is used for screening out a target address from the entry address list.
And the calling address modification module is used for modifying the remote procedure calling address of the target service based on the target address.
And the target service calling module is used for calling the target service by using the modified remote process calling address.
The present invention can also provide a computer storage medium having computer-readable instructions stored thereon which, when executed by one or more processors, cause the one or more processors to perform the steps of the service invocation method for a microservice architecture described in any of the embodiments of the present invention.
The invention has the beneficial effects that:
the invention provides a brand-new solution for service calling under a micro-service architecture, which can modify a Remote Procedure Call (RPC) address of a target service according to an actual calling condition, realize calling of an out-of-container service to an in-container service, and has the outstanding advantages of easy realization, easy management, high maintenance efficiency and the like.
Compared with the prior art, the invention thoroughly breaks the isolation between the network in the container and the network outside the container. By identifying the service in the container in the registration center, the invention determines the condition of service calling in the container in the remote procedure calling process, and can correspondingly modify the URL of the service provider according to different calling conditions, so that the service consumer can use the modified URL to carry out remote procedure calling.
In addition, the invention is helpful for developing operation and maintenance personnel to quickly position whether a certain service is an in-container service or an out-container service according to the parameter information of the registration center, and the invention can ensure that the maintenance process of the micro-service is more efficient.
Detailed Description
The service calling method, device and computer storage medium for micro service architecture provided by the invention are explained and explained in detail below with reference to the drawings of the specification.
As shown in FIG. 1, the present invention provides a service invocation method for micro-service architecture, which can be used to "get through" the internal and external networks of a container. Specifically, the service calling method provided by the invention can include, but is not limited to, at least one of the following steps.
As shown in fig. 2, service registration is performed in advance. This step may include: judging whether a service to be registered is started in a container or not when the service to be registered is started; specifically, the first parameter may be written into the metadata for registration when the service to be registered is started in the container; or writing a second parameter into the metadata for registration when the service to be registered is started outside the container; thereby realizing that the relevant information for indicating the environment to which the service itself belongs is added to the metadata at the time of registration.
Wherein, the first parameter (i.e. the metadata information with the in-container identifier) is used for representing that the currently registered service is the in-container service, and the second parameter (i.e. the metadata information with the out-container identifier) is used for representing that the currently registered service is the out-container service.
It can be understood that, for the service inside the container or the service outside the container, the invention can also write information such as IP, port, application name and the like into the metadata during the service registration process.
More specifically, the determining whether the service to be registered is started in the container when the service to be registered is started includes: the method comprises the steps of obtaining an environment variable of a service to be registered when the service is started, judging whether the environment variable is provided with a container identifier or not, determining that the service to be registered is started in a container according to the container identifier in the environment variable, or determining that the service to be registered is started outside the container according to the container identifier in the environment variable.
As shown in fig. 3, the service consumer can acquire a service list formed by prior registration of the service provider through the registry, and select a target service corresponding to the service in the service list. The basis for selecting the target service by the invention can be a load balancing strategy. The method in which the load balancing policy is implemented may include, but is not limited to, at least one of a random algorithm, a round robin algorithm, a weighted round robin algorithm, a minimum active number algorithm, and the like.
It can be judged first whether the service of the service consumer is in the container, if so, there are two cases: (1) the target service is inside the container, and (2) the target service is outside the container. According to the invention, the service is an in-container service and the target service is an in-container service, and the target service is called by using the access address of the target service in the container; or calling the target service by using the access address provided by the target service during registration according to the condition that the service is an in-container service and the target service is an out-of-container service.
If the service of the service consumer is outside the container, if the target service is outside the container, and belongs to the network access outside the container, the invention can directly call the target service by using the access address of the target service according to the condition that the service is the service outside the container and the target service is the service outside the container.
After the determination, according to the fact that the service is an out-of-container service and the target service is an in-container service, the configuration of an entry (Ingress) of the container is read, and an entry address (Ingress IP) list of the container can be obtained.
Wherein determining that the service is an out-of-container service and the target service is an in-container service comprises: the method comprises the steps of obtaining first metadata generated when the service is registered firstly and second metadata generated when a target service is registered firstly, and determining that the service is an out-of-container service and the target service is an in-container service according to the second parameters in the first metadata and the first parameters in the second metadata.
And screening the target address from the entry address list. The invention can select one address from a plurality of addresses in the entry address list as the target address, and the screening mode can be a load balancing strategy, for example.
After the target address is determined, the present invention modifies the Remote Procedure Call (RPC) address of the target service based on the target address. Some embodiments of the present invention modify a remote procedure call address of a target service based on a target address, comprising: the remote procedure call address of the target service is modified into an address formed by splicing the service identification number (ID) and the target address, so that the RPC address is corrected by using the target address obtained from the entry address list, namely, the URL (Uniform Resource Locator) of the target service can be modified as required.
The service consumer calls the target service provided by the service provider by using the modified remote procedure call address, so that the invention can better realize the mutual communication between the service outside the container and the service inside the container and thoroughly break the isolation between the network inside the container and the network outside the container.
It is understood that under the micro-service architecture, the services to which the present invention relates are specifically micro-services. As shown in FIG. 4, the IPs outside the container of the present invention may generally include a physical machine IP and a virtual machine IP.
As shown in fig. 4 and 5, the present invention can also provide a service invocation device for micro-service architecture, which may include, but is not limited to, a target service selection module, an address list acquisition module, a target address screening module, a call address modification module, and a target service invocation module.
And the target service selection module is used for selecting the target service corresponding to the service.
And the address list acquisition module is used for acquiring an entry address list of the container according to the condition that the service serves as the container outside and the target service serves as the container inside. The address list obtaining module is specifically configured to obtain first metadata generated when the service is registered first and second metadata generated when the target service is registered first, and to determine that the service is an out-of-container service and the target service is an in-container service according to the first metadata having the second parameter and the second metadata having the first parameter.
And the target address screening module is used for screening out a target address from the entry address list.
And the calling address modification module is used for modifying the remote procedure calling address of the target service based on the target address. The call address modification module is specifically configured to modify the remote procedure call address into an address formed by splicing the service identification number and the target address.
And the target service calling module is used for calling the target service by using the modified remote procedure calling address. The target service calling module can be used for directly calling the target service according to the access address of the target service, which is used by the service outside the container and is used by the service outside the container, or can be used for calling the target service according to the access address of the service inside the container, which is used by the service inside the container and is used by the service inside the container, or can be used for calling the target service according to the access address, which is provided by the service inside the container and is used by the service outside the container when the service outside the container is registered, of the target service.
It is understood that the target service selection module, the address list acquisition module, the target address screening module, the calling address modification module, and the target service calling module can all be integrated on the service consumer (caller of the service). I.e. the service invocation means comprises the service consumer.
The service calling device of the invention also comprises a service provider (called party of the service) and a registration center, wherein the service provider can be used for providing a target service to be called.
The registry can be used for judging whether the service to be registered is started in the container when being started, and is used for writing a first parameter into the metadata for registration according to the service to be registered, or writing a second parameter into the metadata for registration according to the service to be registered and starting outside the container. The first parameter is used for representing that the currently registered service is an in-container service, and the second parameter is used for representing that the currently registered service is an out-of-container service. The registration center can be specifically configured to control the start of the micro service, to obtain an environment variable of the service when the service to be registered is started, to determine whether the environment variable has a container identifier, to determine that the service to be registered is started in the container according to the container identifier included in the environment variable, or to determine that the service to be registered is started outside the container according to no container identifier included in the environment variable.
The registry to which the present invention relates is a service registration discovery component, which may be implemented, for example, by Nacos (a dynamic service discovery, configuration and service management platform).
As shown in fig. 4, a schematic diagram of an application scenario for implementing a container internal and external network access scheme is provided.
Services within the Kubernetes environment register to Nacos, with service C and service D specifying metadata information for service registration at registration time.
2. Services within the normal virtual machine environment are registered with the Nacos, and service A and service B may not need to specify any information at the time of registration.
3. Under the condition that the service A calls the service B in the virtual machine environment, an IP list of the corresponding service can be directly obtained through the registration center and can be directly called through a load balancing strategy.
And 4, calling the service C by the service D under the Kubernetes environment, acquiring an IP list of a container where the corresponding service is located through the registration center, and calling the service through a load balancing strategy.
And 5, the service C in the Kubernetes environment calls the service A in the virtual machine environment, the IP list of the service A is obtained through the registration center, and the service A is directly called through a load balancing strategy.
6. The service B in the virtual machine environment calls the service D in the Kubernetes environment, and the service D comprises the following steps:
6.1 service B obtains IP list corresponding to Ingress under Kubernetes environment configured by registration center.
6.2 service B changes the service D address into the Ingress address through the load balancing strategy and requests to access.
6.3Ingress routes the request for access to the corresponding service cluster through configuration.
The present invention can also provide a computer storage medium having computer-readable instructions stored thereon which, when executed by one or more processors, cause the one or more processors to perform the steps of the service invocation method for the microservice architecture of any of the embodiments of the present invention. The service calling method includes but is not limited to at least one of the following steps: service registration is performed first: judging whether a service to be registered is started in a container or not when the service to be registered is started, and writing a first parameter into metadata for registration according to the service to be registered which is started in the container; or writing a second parameter into the metadata for registration when the service to be registered is started outside the container; the judging whether the service to be registered is started in the container or not comprises the following steps: the method comprises the steps of obtaining an environment variable of a service to be registered when the service is started, judging whether the environment variable is provided with a container identifier or not, determining that the service to be registered is started in a container according to the container identifier in the environment variable, or determining that the service to be registered is started outside the container according to the container identifier in the environment variable. The first parameter is used for representing that the currently registered service is an in-container service, and the second parameter is used for representing that the currently registered service is an out-of-container service. Next, a target service corresponding to the service is selected. And acquiring an entry address list of the container according to the condition that the service is an out-of-container service and the target service is an in-container service. Wherein determining that the service is an out-of-container service and the target service is an in-container service comprises: the method comprises the steps of obtaining first metadata generated when the service is registered firstly and second metadata generated when a target service is registered firstly, and determining that the service is an out-of-container service and the target service is an in-container service according to the second parameters in the first metadata and the first parameters in the second metadata. And screening out a target address from the entry address list, and modifying the remote procedure call address of the target service based on the target address. Some embodiments of the present invention modify a remote procedure call address of a target service based on a target address, comprising: the remote procedure call address can be modified into an address formed by splicing the service identification number and the target address. And calling the target service by using the modified remote procedure call address. In addition, or according to the fact that the service is an out-of-container service and the target service is an out-of-container service, the target service is directly called by using the access address of the target service; or calling the target service by using the access address of the target service in the container according to the condition that the service is the in-container service and the target service is the in-container service; or calling the target service by using the access address provided by the target service during registration according to the condition that the service is an in-container service and the target service is an out-of-container service.
The logic and/or steps represented in the flowcharts or otherwise described herein, such as an ordered listing of executable instructions that can be considered to implement logical functions, can be embodied in any computer-storage medium for use by or in connection with an instruction execution system, apparatus, or device, such as a computer-based system, processor-containing system, or other system that can fetch the instructions from the instruction execution system, apparatus, or device and execute the instructions. For the purposes of this description, a "computer storage medium" can be any means that can contain, store, communicate, propagate, or transport the program for use by or in connection with the instruction execution system, apparatus, or device. More specific examples (a non-exhaustive list) of the computer storage medium would include the following: an electrical connection (electronic device) having one or more wires, a portable computer cartridge (magnetic device), a Random Access Memory (RAM), a Read-Only Memory (ROM), an Erasable Programmable Read-Only Memory (EPROM-Only Memory, or flash Memory), an optical fiber device, and a portable Compact Disc Read-Only Memory (CDROM). Additionally, the computer storage medium may even be paper or another suitable medium upon which the program is printed, as the program can be electronically captured, via for instance optical scanning of the paper or other medium, then compiled, interpreted or otherwise processed in a suitable manner if necessary, and then stored in a computer memory.
It should be understood that portions of the present invention may be implemented in hardware, software, firmware, or a combination thereof. In the above embodiments, the various steps or methods may be implemented in software or firmware stored in memory and executed by a suitable instruction execution system. For example, if implemented in hardware, as in another embodiment, any one or combination of the following techniques, which are known in the art, may be used: a discrete logic circuit having a logic Gate circuit for implementing a logic function on a data signal, an application specific integrated circuit having an appropriate combinational logic Gate circuit, a Programmable Gate Array (PGA), a Field Programmable Gate Array (FPGA), or the like.
In the description herein, references to the description of the term "the present embodiment," "one embodiment," "some embodiments," "an example," "a specific example," or "some examples," etc., mean that a particular feature, structure, material, or characteristic described in connection with the embodiment or example is included in at least one embodiment or example of the present invention. In this specification, the schematic representations of the terms used above are not necessarily intended to refer to the same embodiment or example. Furthermore, the particular features, structures, materials, or characteristics described may be combined in any suitable manner in any one or more embodiments or examples. Furthermore, various embodiments or examples and features of different embodiments or examples described in this specification can be combined and combined by one skilled in the art without contradiction.
Furthermore, the terms "first", "second" and "first" are used for descriptive purposes only and are not to be construed as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one such feature. In the description of the present invention, "a plurality" means at least two, e.g., two, three, etc., unless specifically limited otherwise.
The above description is only for the purpose of illustrating the preferred embodiments of the present invention and is not to be construed as limiting the invention, and any modifications, equivalents and simplifications made in the spirit of the present invention are intended to be included in the scope of the present invention.