Disclosure of Invention
In view of the above, the present invention provides a service registration method and Pod to solve the problem that a micro-service developed using a non-Java language cannot be registered in Eureka, so that the micro-service developed using the non-Java language cannot be normally used.
In order to solve the technical problems, the invention adopts the following technical scheme:
a service registration method is applied to a container combination Pod, wherein an Agent container and a micro-service container are deployed in the Pod, and an Agent program capable of calling a service registration interface of a target registration center is deployed in the Agent container; the micro service container is deployed with micro services which are developed by using a preset development language and need to be subjected to service registration; the Agent container is preset with service registration parameters corresponding to the micro-service; the micro service container is preset with an Agent proxy address of the Agent program;
the service registration method comprises the following steps:
the Agent program acquires service registration parameters corresponding to the micro-service and a Pod address of the Pod when the Pod responds to a starting instruction; the service registration parameters comprise micro service parameters and a registration center address;
and the Agent program calls the service registration interface and sends the Pod address and the micro-service parameters to a target registration center corresponding to the registration center address through the service registration interface, so that the target registration center stores the Pod address and the micro-service parameters to realize the registration of the micro-service.
Optionally, the Agent program sends the Pod address and the micro service parameter to a target registry corresponding to the registry address through the service registration interface, so that the target registry stores the Pod address and the micro service parameter, and after registration of the micro service is achieved, the method further includes:
and the Agent program sends heartbeat information to the target registration center at fixed time intervals.
Optionally, the Agent program sends the Pod address and the micro service parameter to a target registry corresponding to the registry address through the service registration interface, so that the target registry stores the Pod address and the micro service parameter, and after registration of the micro service is achieved, the method further includes:
the micro service sends a service request to the Agent program; the service request comprises a request service name, request service content and an access address;
the Agent program determines a target service corresponding to the request service name, a Pod address corresponding to the target service and a service port in the Pod address;
and the Agent program generates a service access request comprising the request service content, the access address, the Pod address corresponding to the target service and a service port in the Pod address, and sends the service access request to the service port in the Pod address corresponding to the target service, so that the target service provides the service corresponding to the request service content.
Optionally, the determining, by the Agent program, a target service corresponding to the requested service name, a Pod address corresponding to the target service, and a service port in the Pod address includes:
acquiring service list information registered in the target registration center; the service list information comprises a service name of each service, a Pod address corresponding to the service name and a service port in the Pod address;
and determining a target service corresponding to the request service name, a Pod address corresponding to the target service and a service port in the Pod address according to the service list information.
Optionally, the obtaining of the service list information registered in the target registration center includes:
and sending a service list information acquisition request to the target registration center, and receiving the service list information returned by the target registration center.
A container combination Pod is provided, wherein an Agent container and a micro-service container are deployed in the Pod, and an Agent program capable of calling a service registration interface of a target registration center is deployed in the Agent container; the micro service container is deployed with micro services which are developed by using a preset development language and need to be subjected to service registration; the Agent container is preset with service registration parameters corresponding to the micro-service; the micro service container is preset with an Agent proxy address of the Agent program;
the Pod is configured to:
the Agent program acquires service registration parameters corresponding to the micro-service and a Pod address of the Pod when the Pod responds to a starting instruction; the service registration parameters comprise micro service parameters and a registration center address;
and the Agent program calls the service registration interface and sends the Pod address and the micro-service parameters to a target registration center corresponding to the registration center address through the service registration interface, so that the target registration center stores the Pod address and the micro-service parameters to realize the registration of the micro-service.
Optionally, the Agent program is further configured to: and sending heartbeat information to the target registration center at fixed time intervals.
Optionally, the microservice is further configured to send a service request to the Agent program; the service request comprises a request service name, request service content and an access address;
the Agent program is also used for: determining a target service corresponding to the request service name, a Pod address corresponding to the target service and a service port in the Pod address;
and generating a service access request comprising the request service content, the access address, the Pod address corresponding to the target service and a service port in the Pod address, and sending the service access request to the service port in the Pod address corresponding to the target service so that the target service provides the service corresponding to the request service content.
Optionally, when the Agent program is used to determine the target service corresponding to the requested service name, the Pod address corresponding to the target service, and the service port in the Pod address, the Agent program is specifically configured to:
acquiring service list information registered in the target registration center; the service list information comprises a service name of each service, a Pod address corresponding to the service name and a service port in the Pod address;
and determining a target service corresponding to the request service name, a Pod address corresponding to the target service and a service port in the Pod address according to the service list information.
Optionally, when the Agent program is used to obtain the service list information registered in the target registration center, the Agent program is specifically configured to:
and sending a service list information acquisition request to the target registration center, and receiving the service list information returned by the target registration center.
Compared with the prior art, the invention has the following beneficial effects:
the Agent container and the micro-service container are deployed in the same Pod, so that the two containers can perform local communication and communicate with the outside through the same IP address, and normal communication of micro-services is guaranteed. On the basis that the micro-service can normally communicate, an Agent program capable of calling a service registration interface of a target registration center is deployed in the Agent container, and then the micro-service can register the micro-service through the Agent program, namely the micro-service is indirectly registered through the Agent program in the Agent container, so that the micro-service developed by using a non-Java language can be registered in Eureka, and the normal use of the micro-service developed by using the non-Java language is ensured. Furthermore, the registration of the micro-service is indirectly realized by setting the Agent program, the micro-service does not need to be adjusted, and compared with a mode of adding the registration program to the micro-service, the operation of the micro-service is simplified.
Detailed Description
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 only a part of the embodiments of the present invention, and not all of the embodiments. 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.
The Eureka is developed based on Java language, and a platform where the Eureka is located is provided with a bottom layer program supporting service registration of micro-services developed by using the Java language, so that the micro-services developed by using the Java language can realize service registration through configuration files. However, for micro-services developed by using non-Java languages, the platform where Eureka is located is not provided with corresponding underlying programs for service registration, so that the micro-services developed by using the non-Java languages cannot be registered in Eureka, and further the micro-services developed by using the non-Java languages cannot be normally used.
The inventor has found through research that if a registration program is added to the program of the micro service, the registration program can call the service registration interface of Eureka, and the registration of the micro service can be realized. However, in this case, when the number of micro services is large, it is necessary to improve the program for each micro service, which makes the operation complicated.
Further, the inventor finds that if an Agent program can be written, the Agent program can call a service registration interface of a target registration center, registration of the micro-service is indirectly realized through the Agent program, the registration program does not need to be added to the micro-service program, and the operation is simple. Moreover, the written Agent program can be suitable for a plurality of micro services, the registration operation of the plurality of micro services can be simplified, one-time writing is realized, and the technical effect of being suitable for the plurality of micro services is achieved.
Specifically, in the embodiment of the present invention, the Agent container and the micro-service container are deployed in the same Pod (in a Kubernetes cluster, a Pod is a basis of all service types, and is a combination of one or more containers), and then the two containers can perform local communication, and communicate with the outside through the same IP address, thereby ensuring normal communication of the micro-service. On the basis that the micro-service can normally communicate, an Agent program capable of calling a service registration interface of a target registration center is deployed in the Agent container, and then the micro-service can register the micro-service through the Agent program, namely the micro-service is indirectly registered through the Agent program in the Agent container, so that the micro-service developed by using a non-Java language can be registered in Eureka, and the normal use of the micro-service developed by using the non-Java language is ensured. Furthermore, the registration of the micro-service is indirectly realized by setting the Agent program, the micro-service does not need to be adjusted, and compared with a mode of adding the registration program to the micro-service, the operation of the micro-service is simplified.
On the basis of the above content, the embodiment of the invention provides a service registration method. Applied to the container assembly Pod. In practical application, an Agent container and a micro-service container are deployed in the Pod, and an Agent program capable of calling a service registration interface of a target registration center is deployed in the Agent container; the micro service container is deployed with micro services which are developed by using a preset development language (such as non-java language) and need to be registered with services.
In this embodiment, the Deployment is used to create the Pod, and to manage the life cycle of the Pod, and the like. Then, the Agent program and the micro-service are deployed in the same Pod through Kubernets (container cluster management software), so that the Agent program and the micro-service can communicate locally, share a network, communicate with the outside by using the same IP address, namely the Agent program and the micro-service are integrated, the micro-service realizes service provision, and the Agent program realizes the external communication of the micro-service. And the micro service container is preset with an Agent proxy address of the Agent program. Specifically, HTTP _ PROXY (Agent PROXY address) is set by an environment variable inside the application container, i.e., the microservice container. The Agent proxy address is a communication address for subsequently accessing the micro-service, the Agent finds out a Pod address through the name of the micro-service, and the micro-service can be accessed through the Pod address. Wherein, the setting process of the environment variable is as follows:
env:
-name:HTTP_PROXY
value:'127.0.0.1:30000
the Agent container is preset with SERVICE registration parameters corresponding to the micro-SERVICE, and specifically, the Agent container is configured with an EUREKA _ endport (registration center address), a SERVICE _ NAME, and a SERVICE _ PORT (SERVICE PORT) through environment variables. The service name and the service port are service registration parameters corresponding to the micro-service. And accessing the micro service through the Agent proxy address, and providing the service for the outside through the Pod address and the service port by the micro service.
It should be noted that one micro service container and one Agent container are arranged in one Pod, and the other micro service container and the Agent container matched with the micro service container are arranged in one Pod, that is, only one micro service container and one Agent container are arranged in the same Pod.
On the basis of the above, referring to fig. 1, the service registration method in the present invention includes:
and S11, when the Agent program responds to the starting instruction, acquiring the service registration parameter corresponding to the micro service and the Pod address of the Pod.
In practical application, a program is arranged in a Pod, a starting instruction is arranged in the program, the program is operated when the Pod is started for the first time, the starting instruction in the program is responded, and at the moment, the Agent program registers the microservice to a target registry.
Specifically, the Agent program acquires a service registration parameter preset with a micro service corresponding to the micro service and a Pod address allocated to the Pod. The service registration parameters comprise the micro-service parameters and the address of the registration center. The micro-service parameters comprise the service name and the service port, and the registration center address is the registration center address.
And S12, the Agent program calls the service registration interface and sends the Pod address and the micro-service parameter to a target registration center corresponding to the registration center address through the service registration interface, so that the target registration center stores the Pod address and the micro-service parameter and realizes the registration of the micro-service.
After the Agent program obtains the Pod address and the micro-service parameter, a service registration interface is called, the obtained data are sent to a target registration center corresponding to a registration center address through the interface, and the target registration center stores the Pod address and the micro-service parameter to realize registration of the micro-service. And after successful registration, returning successful registration information to the Agent program.
More specifically, the Agent program reads the environment variable and registers the Pod address, the service name and the service port in the registry, and the registry stores the relationship between the service name and the Pod address and the port. And after the registration is finished, the Agent program sends heartbeat information to the target registration center at regular intervals.
Specifically, the Agent sends heartbeat to the registry at regular time, so that the target registry knows whether the application, namely the microservice is normally operated or not, and sends the operation state (UP indicates operation, and DOWN indicates non-operation) and the current time of the application.
In the embodiment, the Eureka provides a service registration function, and the Agent program can access the Eureka to provide an interface, so that the development of Eureka by other language programs is simplified. Specifically, the Agent container and the micro-service container are deployed in the same Pod, so that the two containers can perform local communication and communicate with the outside through the same IP address, and normal communication of micro-services is guaranteed. On the basis that the micro-service can normally communicate, an Agent program capable of calling a service registration interface of a target registration center is deployed in the Agent container, and then the micro-service can register the micro-service through the Agent program, namely the micro-service is indirectly registered through the Agent program in the Agent container, so that the micro-service developed by using a non-Java language can be registered in Eureka, and the normal use of the micro-service developed by using the non-Java language is ensured. Furthermore, the registration of the micro-service is indirectly realized by setting the Agent program, the micro-service does not need to be adjusted, and compared with a mode of adding the registration program to the micro-service, the operation of the micro-service is simplified. Furthermore, after the Agent program is written, the Agent program can be applied to a plurality of micro services, so that the effects of one-time writing and multiple-time use are achieved.
The above embodiments describe the registration process of the micro service, and in practical applications, after the registration of the micro service, the service can be provided. Specifically, referring to fig. 2, after step S12, the method further includes:
and S21, the micro service sends a service request to the Agent program.
In practical application, one micro service may call another micro service, for example, one micro service is shopping software, the other micro service is order service, if a user wants to access his order after ordering the shopping software, the shopping software is required to call the order service software, and then the shopping software accesses an Agent program in the same Pod as the shopping software in an HTTP proxy manner, and at this time, the shopping software sends a service request to the Agent program.
The service request includes a request service name, request service content, and an access address. The name of the request service may be the order service, the content of the request service may be a query history order, and the access address is an access address in the URL.
And S22, the Agent program determines the target service corresponding to the request service name, the Pod address corresponding to the target service and the service port in the Pod address.
In practical application, an Agent program can obtain a target service corresponding to the requested service name, a Pod address corresponding to the target service and a service port in the Pod address by inquiring from service list information stored in the Agent program.
More specifically, referring to fig. 3, step S22 may include:
and S31, acquiring the service list information registered in the target registration center.
In practical application, the Agent program sends a service list information acquisition request to the target registration center and receives service list information returned by the target registration center.
The service list information comprises a service name of each service, a Pod address corresponding to the service name and a service port in the Pod address.
Specifically, after receiving registration information of the micro-service (a service registration parameter corresponding to the micro-service and a Pod address of the Pod) sent by the Agent program, the target registration center stores the registration information into service list information, so that the service list information includes registration information of each micro-service.
The Agent program can communicate with the target registration center and sends a service list information acquisition request to the target registration center, the target registration center returns the service list information to the Agent program after receiving the service list information acquisition request, and the Agent program stores the service list information in the memory.
S32, according to the service list information, determining a target service corresponding to the request service name, a Pod address corresponding to the target service and a service port in the Pod address.
Specifically, the service list information includes registration information of each micro service, and the Agent program directly queries the service list information according to the request service name to obtain a target service corresponding to the request service name, a Pod address corresponding to the target service, and a service port in the Pod address.
S23, the Agent program generates a service access request comprising the request service content, the access address, the Pod address corresponding to the target service and the service port in the Pod address, and sends the service access request to the service port in the Pod address corresponding to the target service, so that the target service provides the service corresponding to the request service content.
After the Agent program obtains the Pod address corresponding to the target service and the service port in the Pod address, a service access request, namely a URL request, including the request service content, the access address, the Pod address corresponding to the target service and the service port in the Pod address is generated, and then the URL request is sent to the service port in the Pod address corresponding to the target service, and the micro-service is found through the service port, so that the micro-service provides the service corresponding to the requested service content. Still taking the above embodiment as an example, at this time, the order service provides historical order information to the shopping software, so that the shopping software feeds back the historical order information to the user.
In this embodiment, an application sends a request to an Agent, the Agent receives the request, and the Agent forwards the request to a service provider, in actual application, there may be a plurality of service providers, but only one of the requests is requested at each time, and after receiving the application request, the Agent finds a corresponding Pod address and port according to a service name, randomly selects an instance from a plurality of pieces of information, and sends the request to the service provider. So that the service provider provides the service.
As described above, referring to fig. 4, the Agent program in the Agent container can realize functions such as registration of microservices (applications), transmission of heartbeats, and reading of services.
Optionally, on the basis of the embodiment of the service registration method, another embodiment of the present invention provides a container combination Pod, where an Agent container and a micro-service container are deployed in the Pod, and an Agent program capable of calling a service registration interface of a target registry is deployed in the Agent container; the micro service container is deployed with micro services which are developed by using a preset development language and need to be subjected to service registration; the Agent container is preset with service registration parameters corresponding to the micro-service; the micro service container is preset with an Agent proxy address of the Agent program;
the Pod is configured to:
the Agent program acquires service registration parameters corresponding to the micro-service and a Pod address of the Pod when the Pod responds to a starting instruction; the service registration parameters comprise micro service parameters and a registration center address;
and the Agent program calls the service registration interface and sends the Pod address and the micro-service parameters to a target registration center corresponding to the registration center address through the service registration interface, so that the target registration center stores the Pod address and the micro-service parameters to realize the registration of the micro-service.
Further, the Agent program is further configured to: and sending heartbeat information to the target registration center at fixed time intervals.
Further, the micro service is also used for sending a service request to the Agent program; the service request comprises a request service name, request service content and an access address;
the Agent program is also used for: determining a target service corresponding to the request service name, a Pod address corresponding to the target service and a service port in the Pod address;
and generating a service access request comprising the request service content, the access address, the Pod address corresponding to the target service and a service port in the Pod address, and sending the service access request to the service port in the Pod address corresponding to the target service so that the target service provides the service corresponding to the request service content.
Further, when the Agent program is used to determine the target service corresponding to the requested service name, the Pod address corresponding to the target service, and the service port in the Pod address, the Agent program is specifically configured to:
acquiring service list information registered in the target registration center; the service list information comprises a service name of each service, a Pod address corresponding to the service name and a service port in the Pod address;
and determining a target service corresponding to the request service name, a Pod address corresponding to the target service and a service port in the Pod address according to the service list information.
Further, when the Agent program is used to acquire the service list information registered in the target registry, the Agent program is specifically configured to:
and sending a service list information acquisition request to the target registration center, and receiving the service list information returned by the target registration center.
The Agent container and the micro-service container are deployed in the same Pod, so that the two containers can perform local communication and communicate with the outside through the same IP address, and normal communication of micro-services is guaranteed. On the basis that the micro-service can normally communicate, an Agent program capable of calling a service registration interface of a target registration center is deployed in the Agent container, and then the micro-service can register the micro-service through the Agent program, namely the micro-service is indirectly registered through the Agent program in the Agent container, so that the micro-service developed by using a non-Java language can be registered in Eureka, and the normal use of the micro-service developed by using the non-Java language is ensured. Furthermore, the registration of the micro-service is indirectly realized by setting the Agent program, the micro-service does not need to be adjusted, and compared with a mode of adding the registration program to the micro-service, the operation of the micro-service is simplified.
The previous description of the disclosed embodiments is provided to enable any person skilled in the art to make or use the present invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the generic principles defined herein may be applied to other embodiments without departing from the spirit or scope of the invention. Thus, the present invention is not intended to be limited to the embodiments shown herein but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.