Simple chat service discovery (redis, zk, etcd, consul) (reprint)

 

 

Service discovery is not how inscrutable, its principles could not be easier. Just too many articles on the market will be more difficult to demonize service discovery, the reader is wound foggy, feel their own low IQ Not for me.

What the service provider is, simply put, it is a HTTP server, provides an API service, an IP address as a service port. What services consumers, it is a simple process, you want to access services provided by the service provider to do some things. HTTP server can be both a service provider to provide services, the consumer may also be in need of services provided by other service providers, which is service dependent, without you I would not be myself. Complex services even more dependent on services.

There are three roles service discovery, service providers, service consumers and service intermediary. Service broker is to contact the service provider and the service consumer of the bridge. The service provider will provide their own service address registered to the service broker, service consumers find the address of the service they want from the service broker there, then enjoy this service. Service Broker provides multiple services, each corresponding to multiple service providers.

 
 

Service Broker is a dictionary, the dictionary, there are many key / value key-value pairs, key is the service name, value is the address of the service provider list. Service registration is calling the dictionary method Put the plug things, is to call the service looks Get a dictionary to get something.

When the service provider node hang, requiring registration service can be canceled in time, than they notify consumers regain service address.

When adding a new service provider, intermediary service requirements can promptly inform the service consumer, do you want to try new services.

Redis as a service intermediary

Redis there are a wealth of data structures used to store service dictionary appropriate. For each service name, we use a set structure for storing IP services: Port strings. If the service provider to join, call sadd order to join the service address, if the service hang up, call srem command to remove the service address. Use smembers service consumers get all the instructions and then randomly pick a service address in the consumption process, the instructions direct access to or use srandmemember random service address.

This time you might be skeptical, service discovery really so simple? The answer is still a little shy, on top of this solution has several problems.

The first problem is the service provider if the process is to kill kill -9 violence, can not take the initiative to call srem command how to do?

This time the list of services in more than a black point to address non-existent services and consumers do not know, this time the service agency became a black intermediary. Then how to do it?

We keep alive the introduction of the service and inspection regimes and replace data structure. Service providers need to report every 5 seconds to survive mediation service, the service broker and a service address value recorded in the report time data and score zset structure. Intermediary service need every 10 seconds to check zset data structures, kicked report serious time behind the service address entries. This allows near real-time to ensure the validity of the list of services service address.

The second problem is how to inform consumers when the service list changes. There are two solutions.

The first is a poll, consumers need to query the list of services every few seconds whether there is a change. If the service a lot, great service list, many consumers, redis there will be some pressure. So this time the version number of mechanisms can be introduced into the list of services, and provide a key value to set the version number of the service / to each service, while the service list is changed, incrementing the version number. Consumers only need to change the version number of this poll to know whether the service list changes occur. Because the list of services is relatively stable, frequent changes will occur only in the case of severe network jitter, so redis almost no pressure.

The second is the use of pubsub. This timely manner significantly better than polling. The disadvantage is that each consumer pubsub takes up a thread and an extra redis connection. In order to reduce the waste of threads and connections, we use a single change in the global pubsub broadcast version number. The so-called global version number is an arbitrary list of services changes have taken place, the version number is incremented. Receiving the consumer version changes go check whether their list of services dependent on the version number changed. Such global version number can also be used to poll the first embodiment.

The third problem is redis is a single point, if the hang of how to do?

This is a big problem. It is because of the existence of this problem, the popular service discovery systems are using a distributed database zookeeper / etcd / consul and other intermediary services as they are multi-node distributed, and hung up a node it does not matter, the system can still properly jobs.

What if the whole cluster hang zk would happen then? In fact, every service consumers will maintain a current list of services in the local memory, even if the service broker cluster hang up, you can also use the current list of services working properly.

As a service agency that redis really do not fly yet? In fact, there is a redis-sentinel can eliminate single points of redis problem, redis-sentinel may hang when the master node, the node automatically upgrade from the master node. So take redis dry it is possible. Dry with redis service discovery is indeed very simple, although this approach is very unpopular.

The service provider does not just HTTP service

The above-mentioned service provider is simply HTTP server, in fact, a variety of services. It can be a database service that can be RPC service can be UDP services.

If the database is MySQL, MySQL service that how it registered with the service broker? Native MySQL can not provide this functionality. Agent general practice is to provide a proxy to register. In addition to the proxy service address registration services to intermediaries, but also need to monitor the health of MySQL, in order to be able to switch to a new service address when MySQL MySQL down in time. The General Agent in order to save resources and to monitor more than one database, it can monitor multiple databases at the same time, or even multiple databases.

 

Service Configuration Reload

Service discovery is generally only used to register and to find a list of services such relatively simple function. But modern service discovery system will integrate service configuration management capabilities. This allows for real-time reload service configuration. Also very simple principle, that is, for each service item, intermediary service also stores a single key / value is used to store the configuration information services. When this configuration item is modified in the background, intermediary service will immediately notify the server configuration information changes. For example, a database address changes, modifications and other business parameters.

 

Service management background

In order to facilitate service management, service discovery general management background will provide a service for managers to view the status of the cluster service. Provide redundant configuration information if the service registration and reporting, service management background will be presented more detailed service information. Service management background also can have all the services that depend organized, showing a beautiful service dependency tree.

Description link

Further reading:

Talking about the service discovery

Guess you like

Origin www.cnblogs.com/-wenli/p/11420968.html