I have heard of zeromq for a long time.
I was not very concerned about this project.
Later, colleague kasicass
I have studied and shared this project.
, Began to pay attention to this project.
Later, I saw this blog post: <zeromq: the best communication library in the cloud Era
>, Make up your mind to see this legendary artifact.
In the beginning, the question is: What is the difference between zeromq and libevent, Ace, and so on?
1) libevent encapsulates the processing of network I/O, signals, timers, etc. It can be developed at the network layer based on it.
2) Ace encapsulates system calls on different platforms and provides several network programming models.
However, zeromq is neither libevent nor ace, because it has the following main features: message-oriented communication. Therefore, it provides better performance than libevent, where Ace is in the Network
Higher-level components in the letter. When using it, programmers no longer need the libevent and ACE libraries mentioned above. If programmers want to use zeromq, they only need to do the following:
Things:
1) inform the used Patten, such as request-reply, pub-sub, push-pull, etc. (this pattern will be explained in detail below ).
2) indicates whether it is used for communication between machines, processes, and threads.
Then, encapsulate the data to be sent to the MSG struct that comes with zeromq and send it out. The user cares about how to serialize/deserialize the data, then how to deal with the data is the user's business.
In this way, the things that users need to pay attention to are "high", and most of their energy can be placed on the business logic.In short, it enables the user to focus on the communication mode and business logic, rather than the lower layer of the network layer.
The following describes several network pattern commonly used by zeromq:
1) Request-reply
In a general C/S architecture, the client and server communicate in a Q & A mode, such as the most classic echo service. note that the server must have a response when the client sends a request.
This pattern is not a bright spot.
2) publish-subscribe
The server serves as the publish end, and any client connected to the server will become the subscribe end. That is to say, the server will send all the current messages to the Client Connected to the current publish.
3) push-pull
Server as the push end, and client as the pull end. if multiple clients are connected to the server at the same time, the server performs an Internal Server Load balancer and uses an average allocation algorithm to publish all the messages to the client.
Looks very common? The next highlights are coming.
2), 3) in both modesStart
The actual number of clients is unknown, which means that a service built using zeromq can be "Hot updated ".
Consider the following scenario. A server acts as a proxy at the top of a group of Server clusters to perform load balancing and distribute requests to different Server clusters according to the corresponding Server clusters below.
The client can process only two machines at a time. As the load increases, three machines may be required. If zeromq's push-pull
If the proxy is set up, you do not need to shut down the server and client. You only need to start a new client connection, and the proxy layer will automatically
The previous machine assigned an average dispatch task. Cool.
In fact, these models are not new things, but zeromq encapsulates users, rather than libevent, Ace, and so on. It focuses on the communication layer, communication Mode.
Personal feeling: zeromqPartially Solved
Problem solved by Erlang: communication and task dispatch among multiple machines is a powerful tool for Distributed communication, but it is limited to language restrictions, it cannot do the same perfection as Erlang (Erlang can be regarded as a simple and micro OS), but in many cases, it seems enough to only use this part of the function.
The amount of zeromq code. As of the 2.0.10-stable version I have read so far, there are less than 2 million lines of code. The provided API is also extremely simple, but the internal implementation ratio is
Compared with "Wrap", zeromq is one of the few projects that I have read that depend heavily on debugging tools to follow up the code to understand the code process. At the same time, there are many inheritance layers of classes in the code, reading is not like
The APIS it provides are as simple and straightforward as they are. Some difficulties will be analyzed in the future.