Detailed Description
In order to make the objects, technical solutions and advantages of the present invention clearer, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings, and it is obvious that the described embodiments are some, but not all embodiments of the present 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.
In the description of the present invention, it should be noted that, unless otherwise explicitly specified or limited, the terms "mounted," "connected," and "connected" are to be construed broadly, e.g., as meaning either a fixed connection, a removable connection, or an integral connection; can be mechanically or electrically connected; they may be connected directly or indirectly through intervening media, or they may be interconnected between two elements. The specific meanings of the above terms in the present invention can be understood in specific cases to those skilled in the art.
It should be noted that the service in the present invention may refer to a software device running on a network, available for use, and based on a distributed program. The service provider of the present invention may include a plurality of servers, and the plurality of servers may provide services through a network, for example: database services or web applications, etc.
Fig. 1 is a schematic flow chart of a service invocation method provided by the present invention. The service invocation method of the present invention is described below in conjunction with FIG. 1. As shown in fig. 1, the method includes:step 101, determining a first target server based on the cached information of each server.
The information of the server comprises: the service name, port identification, and IP address of the service that the server can provide.
It should be noted that the execution subject in the embodiment of the present invention is a service invocation device.
Specifically, for each service provider, after the service provider is started, information such as a service name, an IP address, and a port identifier of a service that can be provided by a number of servers included in the service provider may be sent to the zookeeper cluster, and service registration may be performed on one node in the zookeeper cluster.
The zookeeper cluster can receive and store information of service names, IP addresses, port identifications and the like of services which can be provided by the servers and are sent by each service provider.
It should be noted that a plurality of service providers may provide the same service through the network.
The service invocation means may obtain the information of each server in a number of ways.
For example: after the service calling device is started, the information of each server can be acquired from the zookeeper cluster through inquiry.
After the service invocation device obtains the information of each server, the information of each server can be cached. After the service invocation device receives the request for invoking the target service, the cached information of each server can be obtained by querying the cache content.
It should be noted that, when the zookeeper cluster is not available at present, the service invocation device obtains the cached information of each server by querying the cached content, so that it can be avoided that the information of each server cannot be obtained from the zookeeper cluster by querying when the zookeeper cluster is not available.
It should be noted that before the service invocation apparatus determines the first target server, it may also determine whether the zookeeper cluster is currently available.
If the zookeeper cluster is judged to be available currently, the service calling device can directly acquire the information of the servers from the zookeeper cluster by inquiring after receiving the request for calling the target service, and also can acquire the cached information of each server by inquiring the cache content.
If the zookeeper cluster is judged to be unavailable at present, cached information of each server can be obtained by querying cache contents.
It should be noted that whether the zookeeper cluster is currently available may be determined according to preset conditions, for example: the preset condition may be that the leader node in the zookeeper cluster is not down and/or more than half of the nodes in the zookeeper cluster are not down. If the zookeeper cluster can meet a preset condition, it may be determined that the zookeeper cluster is currently available.
After the service calling device acquires the cached information of each server, the first target server can be determined through a preset algorithm.
It should be noted that the first target server may provide the target service.
It should be noted that the preset algorithm may be determined according to actual requirements, for example: a load balancing algorithm or a greedy algorithm, etc. The specific algorithm is not limited in the embodiments of the present invention.
And step 102, calling a target service provided by the first target server.
After the service calling device determines the first target server, the service calling device can access the first target server according to information such as the IP address and the port identification of the first target server, and can further call the target service provided by the first target server.
According to the embodiment of the invention, the target service provided by the first target server is called after the first target server is determined based on the cached service name, the cached port identification and the cached IP address of the service provided by each server, the service can be called without depending on a zookeeper cluster, the high availability of each service can be ensured, the failure rate of calling the service can be reduced, and the robustness of the service can be better ensured.
Based on the content of the foregoing embodiments, before determining the first target server based on the cached information of each server, the method further includes: and receiving the information of each server sent by the zookeeper cluster.
Specifically, after the service invocation device is started, a query instruction may be sent to the zookeeper cluster.
After receiving the query instruction, the zookeeper cluster may send the found information of each server to the service invocation device.
The service invocation means may receive information of each server transmitted by the zookeeper cluster.
The zookeeper cluster may send information of each server to the service invocation apparatus in various ways, for example: the zookeeper cluster may generate a server list including information of each server based on the information of each server, and transmit the server list to the service invocation apparatus.
And caching the information of each server.
Specifically, after the service invocation device receives the information of each server sent by the zookeeper cluster, the service invocation device may send the information of each server to other cache devices for caching; alternatively, the service invocation device may locally cache the information of each server.
It should be noted that the manner of caching the information of each server may include, but is not limited to: redis cache or mysql cache, etc.
After receiving the request for calling the target service, the service calling device may obtain the cached information of each server by querying the cached content, and may further determine the first target server according to the cached information of each server.
According to the embodiment of the invention, after the information of each server sent by the zookeeper cluster is received, the information of each server is cached, so that the first target server can be determined based on the cached information of each server under the condition of not depending on the zookeeper cluster, the target service provided by the first target server is called, the failure rate of calling the service can be reduced, the high availability of each service can be better ensured, and the robustness of the service can be better ensured.
Based on the content of the foregoing embodiments, after caching information of each server, the method further includes: and receiving the information of each second target server which is sent by the zookeeper cluster and changed.
Specifically, for each service provider, if any number of servers included in the service provider are down or offline, the down or offline server may be used as a second target server; or, if a plurality of servers are added to the service provider, the newly added servers can be used as second target servers; alternatively, if the service that can be provided by any of a plurality of servers included in the service provider is changed, the server that has changed the service provided may be the second destination server.
The service provider may send the changed information of each second target server to the zookeeper cluster, and the zookeeper cluster may remove, add, or change the stored information of each server according to the information of each second target server.
Based on a zookeeper cluster observation mechanism (Watcher) mechanism, the zookeeper cluster may send information of each second target server to the service invocation device.
The service invocation device may receive information of each second target server sent by the zookeeper cluster.
And updating the cached information of each server based on the information of each second target server.
After the service calling device receives the information of each second target server sent by the zookeeper cluster, the cached information of each server can be removed, newly added or changed according to the information of each second target server.
It should be noted that, after receiving the request for invoking the target service, the service invocation apparatus may determine the first target server based on the updated server information in the cache content.
It should be noted that the first target server may be a second target server newly added in the service provider.
According to the embodiment of the invention, after the information of each second target server which is sent by the zookeeper cluster and changed is received, the cached information of each server is updated based on the information of each second target server, so that the first target server can be determined based on the updated information of each server in the cache content under the condition of not depending on the zookeeper cluster, the target service provided by the first target server can be called, the failure rate of calling the service can be reduced, the high availability and high consistency of each service can be better ensured, and the robustness of the service can be better ensured.
Based on the content of the foregoing embodiments, the caching service call list specifically includes: and sending the information of each server to a redis caching device for caching.
Specifically, after receiving information of all servers capable of providing the target service, which is transmitted by the zookeeper cluster, the service invocation device may transmit the information of each server to the redis cache device.
After receiving the information of each Server, the redis caching device may cache the information of each Server through a Remote Dictionary service (Remote Dictionary Server).
It should be noted that redis has the advantages of fast storage speed (110000 times/s for reading and 81000 times/s for writing), supporting data storage of more data types (e.g., Strings, Lists, Hashes, Sets, Ordered Sets, etc. data types of binary cases), supporting data persistence, and supporting data backup (i.e., master-slave mode data backup). Therefore, in the embodiment of the invention, the information of each server is cached through the redis.
According to the embodiment of the invention, the information of each server is sent to the redis caching device for redis caching, so that the caching speed is higher, and the information of each server with more formats can be cached.
Based on the content of the foregoing embodiments, determining the first target server based on the cached information of each server specifically includes: and determining each third target server capable of providing the target service based on the cached information of each server.
Specifically, in the case where the information of each server is cached, the service invocation apparatus may query the cached information of each server according to the service name of the target service, and set all servers capable of providing the target service as the third target server.
It should be noted that, in the case that the zookeeper cluster is currently available, the service invocation apparatus may also query the zookeeper cluster according to the service name of the target service, and directly acquire information of each third target server. After the service calling device acquires the information of each third target server, the information of each third target server can be cached. After receiving the request for calling the target service, the service calling device can query the zookeeper cluster according to the service name of the target service and directly acquire the information of each third target server; and the cached content can be queried to obtain the cached information of each third target server.
And determining one or more third target servers as the first target servers through a load balancing algorithm.
Specifically, the load balancing algorithm may include a round robin method, a random method, a source address hashing method, a weighted round robin method, a weighted random method, and a minimum connection number method.
In the embodiment of the present invention, based on the information of each third target server, the first target server may be determined by at least one of load balancing algorithms. The first target server may be one or more third target servers based on different load balancing algorithms.
For example: the service calling device may allocate the requests to the third target servers in turn in sequence by a polling method based on the information of the third target servers, and determine each third target server as the first target server in sequence; or, the service invoking device may further randomly select one of the servers as the first target server based on the information of each third target server through a preset random algorithm.
It should be noted that, the specific manner of determining the first target server by using the load balancing algorithm based on the information of each third target server is not limited in the embodiment of the present invention.
According to the embodiment of the invention, the first target server is determined in each third target server through the load balancing algorithm, so that the first target server can be determined more quickly based on the information of each server.
Fig. 2 is a flowchart of a service invocation method provided by the present invention. The service invocation method of the present invention is described below in conjunction with fig. 2. As shown in fig. 2, the method includes:step 201, receiving information of each server sent by each server.
It should be noted that the execution subject of the embodiment of the present invention is a zookeeper cluster.
Specifically, for each service provider, after the service provider is started, information such as a service name, an IP address, and a port identifier of a service that can be provided by a number of servers included in the service provider may be sent to the zookeeper cluster, and service registration may be performed on one node in the zookeeper cluster.
It should be noted that a plurality of service providers may provide the same service through the network.
The zookeeper cluster can receive and store information of service names, IP addresses, port identifications and the like of services which can be provided by the servers and are sent by each service provider.
Step 202, sending the information of each server to the service invoking device, so that the service invoking device caches the information of each server, determining a first target server based on the cached information of each server, and invoking a target service provided by the first target server.
The information of the server comprises: the service name, port identification, and IP address of the service that the server can provide.
After the zookeeper cluster receives and stores the information of each server sent by each service provider, the zookeeper cluster can send a query request according to the service calling device and send the searched information of each server to the service calling device.
The zookeeper cluster may send information of each server to the service invocation apparatus in various ways, for example: the zookeeper cluster may generate a server list including information of each server based on the information of each server, and may transmit the server list and the like to the service invocation apparatus.
After receiving the information of each server sent by the zookeeper cluster, the service invocation device may further cache the information of each server, determine a first target server based on the cached information of each server, and invoke a target service provided by the first target server.
It should be noted that, for the specific processes of caching the information of each server, determining the first target server based on the cached information of each server, and invoking the target service provided by the first target server, the service invoking device may refer to the contents of the foregoing embodiments, and details are not described here.
It should be noted that, after the service provider starts, when the zookeeper cluster performs service registration, if the network is unavailable, service registration of the service provider will fail. In the event that the network is unavailable resulting in a failure of the service provider registration, the service provider may be assured of completing the service registration by a registration retry mechanism. The registration retry mechanism may include: if the service registration failure of the service provider is judged and known, the service provider can perform service registration on the zookeeper cluster again after the target time delay until each service provider completes the service registration in the zookeeper cluster.
According to the embodiment of the invention, after the service name, the port identification and the IP address of the service which can be provided by each server and sent by each server are received, the information of each server is sent to the service calling device, the service calling device determines the first target server based on the cached information of each server and calls the target service provided by the first target server, the service can be called under the condition of not depending on a zookeeper cluster, the high availability of each service can be ensured, the failure rate of calling the service can be reduced, and the robustness of the service can be better ensured.
Based on the content of the foregoing embodiments, after sending the information of each server to the service invocation device, the method further includes: when a plurality of second target servers in each server are changed, the information of each changed second target server is sent to the service calling device, so that the service calling device updates the cached information of each server based on the information of each second target server.
Specifically, for each service provider, if any number of servers included in the service provider are down or offline, the down or offline server may be used as a second target server; or, if a plurality of servers are added to the service provider, the newly added servers can be used as second target servers; alternatively, if the service that can be provided by any of a plurality of servers included in the service provider is changed, the server that has changed the service provided may be the second destination server.
The service provider may send the changed information of each second target server to the zookeeper cluster, and the zookeeper cluster may remove, add, or change the stored information of each server according to the information of each second target server.
Based on a zookeeper cluster's observation mechanism (Watcher) mechanism, the zookeeper cluster may send the information of the second target server to the service invocation device.
The service calling device may receive information that the zookeeper cluster transmits to each second target server.
It should be noted that, the specific process of the service invoking device receiving the information of each second target server and updating the cached information of each server based on the information of each second target server may refer to the contents in the foregoing embodiments, and details are not described here.
According to the embodiment of the invention, the changed information of each second target server is sent to the service calling device, and after the service calling device receives the information of each second target server sent by the zookeeper cluster, the cached information of each server is updated based on the information of each second target server, so that the first target server can be determined based on the updated information of each server in the cache content under the condition of not depending on the zookeeper cluster, the target service provided by the first target server is called, the failure rate of calling the service can be reduced, the high availability and high consistency of each service can be better ensured, and the robustness of the service can be better ensured.
To facilitate understanding of the service invocation method of the present invention, the service invocation method of the present invention is described below by way of an example. Fig. 3 is a third schematic flowchart of the service invocation method provided by the present invention. As shown in fig. 3, n service providers form a service provider cluster. After each service provider starts, service registration can be performed at one node in the zookeeper cluster.
After the service invocation device is started, a target server list including information of all third target servers capable of providing the target service can be acquired from the zookeeper cluster. The zookeeper cluster may also send information of each second target server that has been changed to the service invocation apparatus.
After receiving the target server list, the service invocation device sends the target server list to the redis caching device for caching, and can update the cached target server list based on the received information of each second target server.
After receiving the request for invoking the target service, the service invoking device may obtain the updated target server list from the redis cache device, remotely determine the first target server through a load balancing algorithm based on the target server list, and remotely invoke the target service provided by the first target server in the service provider cluster.
It should be noted that, before the service invocation is performed by using the service invocation method provided by the present invention, the method further includes: and building a server environment. Wherein the operating environment may be any one of linux, windows, linux version 6.4 or more, and windows7 version or more.
Zookeeper cluster, tomcat container and redis cache database are deployed in server environment.
A service provider cluster is prepared that includes multiple instance service providers.
After deploying the service provider cluster and the service invocation device in the tomcat container, the tomcat container is started.
And carrying out service calling test.
Fig. 4 is a schematic structural diagram of a service invocation device provided by the present invention. The service calling method provided by the present invention is described below with reference to fig. 4, and the service calling apparatus described below and the service calling method described above may be referred to correspondingly. As shown in fig. 4, the apparatus includes: aconfirmation module 401 and acalling module 402.
The confirmingmodule 401 is configured to determine the first target server based on the cached information of each server.
A callingmodule 402, configured to call a target service provided by a first target server.
The information of the server comprises: the service name, port identification, and IP address of the service that the server can provide.
Specifically, theconfirmation module 401 and thecalling module 402 are electrically connected.
Theconfirmation module 401 may determine, through a preset algorithm, a first target server providing a target service based on the cached information of each server.
The callingmodule 402 may access the first target server according to information such as an IP address and a port identifier of the first target server, and may further call a target service provided by the first target server.
It should be noted that the service invocation device may further include a communication module and a cache module.
The communication module may be configured to receive information of each server sent by the zookeeper cluster.
The caching module can be used for caching information of each server.
The communication module may be further configured to receive information of each second target server that is sent by the zookeeper cluster and has been changed.
It should be noted that the service invocation device may further include an update module.
And the updating module is used for updating the cached information of each server based on the changed information of each second target server.
It should be noted that the communication module may also be configured to send information of each server to the redis caching device for caching.
It should be noted that theconfirmation module 401 may be further configured to determine, based on the cached information of each server, each third target server capable of providing the target service; and determining one or more third target servers as the first target servers through a load balancing algorithm.
According to the embodiment of the invention, the target service provided by the first target server is called after the first target server is determined based on the cached service name, the cached port identification and the cached IP address of the service provided by each server, the service can be called without depending on a zookeeper cluster, the high availability of each service can be ensured, the failure rate of calling the service can be reduced, and the robustness of the service can be better ensured.
Fig. 5 is a schematic structural diagram of a node in a zookeeper cluster provided by the present invention. The service calling method provided by the present invention is described below with reference to fig. 5, and the zookeeper cluster described below and the service calling method described above may be referred to correspondingly. As shown in fig. 5, the zookeeper cluster includes: a plurality of nodes.
Each node, comprising: a receivingmodule 501 and a sendingmodule 502.
The receivingmodule 501 is configured to receive information of a server sent by the server.
A sendingmodule 502, configured to send information of each server to the service invocation device, so that the service invocation device caches the information of each server, determines a first target server based on the cached information of each server, and invokes a target service provided by the first target server;
the information of the server comprises: the service name, port identification, and IP address of the service that the server can provide.
The receivingmodule 501 may receive information of a number of servers included in a service provider, which is sent by the service provider after the service provider is started.
The sendingmodule 502 may send information of several servers included by the service provider to the service invocation device.
In addition, each of the sendingmodules 502 may be further configured to send information of each of the changed second destination servers to the service invocation apparatus when a plurality of second destination servers among the servers are changed.
According to the embodiment of the invention, after the service name, the port identification and the IP address of the service which can be provided by each server and sent by each server are received, the information of each server is sent to the service calling device, the service calling device determines the first target server based on the cached information of each server and calls the target service provided by the first target server, the service can be called under the condition of not depending on a zookeeper cluster, the high availability of each service can be ensured, the failure rate of calling the service can be reduced, and the robustness of the service can be better ensured.
Fig. 6 illustrates a physical structure diagram of an electronic device, which may include, as shown in fig. 6: a processor (processor)610, a communication Interface (Communications Interface)620, a memory (memory)630 and acommunication bus 640, wherein theprocessor 610, thecommunication Interface 620 and thememory 630 communicate with each other via thecommunication bus 640. Theprocessor 610 may call logic instructions in thememory 630 to perform a service call method comprising: determining a first target server based on the information of each server; calling a target service provided by a first target server; the information of the server comprises: and determining a first target server based on the cached information of each server. The method further comprises the following steps: receiving information of each server sent by each server; and sending the information of each server to the service calling device so that the service calling device caches the information of each server, determining a first target server based on the cached information of each server, and calling the target service provided by the first target server.
In addition, the logic instructions in thememory 630 may be implemented in the form of software functional modules and stored in a computer readable storage medium when the software functional modules are sold or used as independent products. Based on such understanding, the technical solution of the present invention may be embodied in the form of a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and other various media capable of storing program codes.
In another aspect, the present invention also provides a computer program product comprising a computer program stored on a non-transitory computer-readable storage medium, the computer program comprising program instructions which, when executed by a computer, enable the computer to perform the service invocation method provided by the above methods, the method comprising: determining a first target server based on the information of each server; calling a target service provided by a first target server; the information of the server comprises: and determining a first target server based on the cached information of each server. The method further comprises the following steps: receiving information of each server sent by each server; and sending the information of each server to the service calling device so that the service calling device caches the information of each server, determining a first target server based on the cached information of each server, and calling the target service provided by the first target server.
In yet another aspect, the present invention also provides a non-transitory computer readable storage medium having stored thereon a computer program which, when executed by a processor, is implemented to perform the service invocation methods provided above, the method comprising: determining a first target server based on the information of each server; calling a target service provided by a first target server; the information of the server comprises: and determining a first target server based on the cached information of each server. The method further comprises the following steps: receiving information of each server sent by each server; and sending the information of each server to the service calling device so that the service calling device caches the information of each server, determining a first target server based on the cached information of each server, and calling the target service provided by the first target server.
The above-described embodiments of the apparatus are merely illustrative, and the units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the devices can be selected according to actual needs to achieve the purpose of the solution of the embodiment. One of ordinary skill in the art can understand and implement it without inventive effort.
Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware. With this understanding in mind, the above-described technical solutions may be embodied in the form of a software product, which can be stored in a computer-readable storage medium such as ROM/RAM, magnetic disk, optical disk, etc., and includes instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the methods described in the embodiments or some parts of the embodiments.
Finally, it should be noted that: the above examples are only intended to illustrate the technical solution of the present invention, but not to limit it; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.