The registration and discovery of services is an essential feature of microservices so that the system can have higher performance and higher availability. The service of the GO-MICRO framework found itself to be able to use the interface registry. You can customize your service registration and discovery as long as you implement this interface.
Go-micro does the load on the client, the typical Balancing-aware client mode.
The service side saves the address information of the service to registry, then the heartbeat check periodically, or the scheduled re-registration service. The client listens to the registry, it is best to save the service information to the local, monitor the service change, update the cache. When the interface to the server is invoked, the server is selected for communication based on the client's service list and load algorithm.
Go-micro can be used registry interface
Interface { Register (*Service, ... registeroption) Error deregister (*Service) error GetService (string) ([]* Service, error) listservices () ([]*service, error) Watch (...) Watchoption) (Watcher, error) string interface { // Next is a blockingcall Next (*Result, error) Stop ()}
This interface is still very simple and clear, see the method can probably guess the main role
The Register method and deregister are used by the server to register the service, and the Watcher interface is used by the client to listen for changes in service information.
Next I take Go-micro Etcdv3 as registry example to give you a detailed explanation of Go-micro's detailed service discovery process
Go-micro Service-Side registration service
Flow chart
The server looks like the process is still relatively simple, and when the server invokes the run () method, the service is invoked. Start () method. This, in addition to listening to the port, start the service, but also the IP port number of the service information, and all the public interface metadata information to save to our chosen register server up.
There seems to be no problem, however, if our node fails, we also need to tell register to delete our node information.
The Run () method has a call to the Go S.run (ex) method, which is to re-register the service according to the interval we set up, of course, the more insurance is the way we set the TTL of the service, so when the service crashes in the unknown situation, The Register service will also automatically delete the information when it reaches the TTL.
Set the TTL and interval for a service
// Initializing the service Service: = Micro. NewService ( Micro. Name (Common. ServiceName), Micro. Registerttl (time. Second*), Micro. Registerinterval (time. Second*), Micro. Registry (REG), )
The TTL is the expiration time of the registration service, and interval is how often to register the service again. If the system crashes, the expiration time will also remove the service. The client will certainly have to think about the judgment, the following will be explained in detail
Client Discovery Service
The client's service discovery is going to be a bit more, but it's not complicated, he involves service selection selector and service discovery register two parts.
Selector is based on service discovery, and returns the host's information based on the host selection algorithm you choose. By default, Go-micro is the time to get the server host information to register to get. But to view the source code of Cmd.go you will find the default initialized value, Selector's default flag is the cache. The cache in defaultselectors corresponds to the Initialize Cacheselector method.
But when you're executing the service. Init () method
Go-micro will replace the default selector with Cacheselector, the implementation is in the Cmd.go before method
Cacheselector will cache the host information obtained from the Register. and sets the time-out period, if the timeout is obtained again. At the time of obtaining the host information, he will run a separate process, go to watch service registration, if there are new nodes found, then add to the cache, if there is a node failure, delete the node information in the cache. When the client also chooses the host selection algorithm according to selector to obtain the host information, there are only two algorithms, the cyclic and the stochastic method. In order to increase the efficiency of execution, very client side will also set the cache connection pool, this point, will be detailed later.
So the approximate client service discovery process is the following
The main invocation process is within the call method
The main idea is
Get the Select Host policy method from selector next.
Depending on whether the service is retried by the retory, the process of invoking the service is to get the host from the next method, connect and transfer the data, retry if it fails, retry, and then re-get a new host based on the host selection policy method next.
Go Micro Service Framework Go-micro deep Learning (iii) registration and discovery of Registry services