Demand:
In order to accurately monitor the internal state of each server, without affecting the existing business logic, a simple centralized monitoring system for the server needs to be deployed quickly. After consideration, you can use the Redis pub/sub function to implement a monitoring system is very good and appropriate.
Of course, we can also pass this to do batch management.
Pub/sub is literally the publication (Publish) and Subscription (Subscribe), in Redis, you can set a key value for the message and message subscriptions, when a key value of the message published, all subscribed to its clients will receive the corresponding message. This function can be used as a real-time messaging system.
Publish and subscribe mechanism
When a client sends a message to the subscriber via the PUBLISH command, we call the client Publisher.
When a client uses the SUBSCRIBE or Psubscribe command to receive information, we call the client a Subscriber (subscriber).
To understand the relationship between a decoupling publisher (publisher) and a Subscriber (subscriber), Redis uses the channel (channel) as an intermediary--the publisher publishes the information directly to channel, and the channel is responsible for sending the information to the appropriate subscriber, There is no relationship between the publisher and the Subscriber, and there is no knowledge of the other.
The following figure is suitable for batch management, each client's server must have a redis-cli, install Redis naturally have, and then define sub, in the management end can be published through pub Some instructions, the client received, you can perform, you can do some other operations.
The following is suitable for monitoring, put their own business indicators pub in the past, the server can handle the corresponding, can be written to MySQL inside, can be and the page to interact.
In fact, the use of sockets can also be achieved, but Redis is already a more mature product, the internal implementation of the Epoll package, there are multiplexing features, non-blocking Io, and most of the request is pure memory operation. With these features, we don't have to bother to write a concurrent asynchronous refactoring to the socket, which is too much trouble.
Well, let's do it now.
This is a subscriber.