Open-source comments: zeromq Introduction

Source: Internet
Author: User

It seems that I haven't written technical posts for two months (political posts have been quite written ). Today, I would like to introduce to programmers a network communication-related open-source project called zeromq (hereinafter referred to as zmq ). This post is onlyIntroductionDoes not involve too many in-depth topics.

★What is zmq?

Generally speaking, zmq is an open-source, cross-language, very concise, very high-performance, and very flexible network communication library.
Its official website is "here" and Wikipedia is "here" (Chinese Wikipedia entries are unavailable for the time being ).
This is not a long time to launch. It seems that 1.0.1 was launched in the second half of the year. I started to contact it last year. It feels really good. This year it has been used in the company's products. Recently, the popularity of zmq has been increasing, so I have come to catch up with the trend, and I will be fooled in my blog.
Next, let's talk about the major features of zmq.

★Simple

The primary feature of zmq is simplicity (it can also be seen by its name ).

◇ Encapsulation causes simplicity
Compared with the original socket API, zmq encapsulates a lot of things, saving developers a lot of trouble.
For example, the traditional TCP is based on byte streams for sending and receiving. Therefore, programmers often have to process the boundary between the data block and the data block by themselves, zmq sends and receives messages. It ensures that each time you send/receive messages, it is a message block. In this way, a lot of code is saved.
For example, for TCP Communication Based on socket APIs, you need to handle many network exceptions (such as abnormal connection interruptions and reconnection) by yourself. Even experienced programmers may not be able to write strictly. In zmq, you don't need to worry about these trivial matters.
For example, if you want to improve the communication performance using traditional socket APIs, you may need to do some asynchronous (non-blocking), buffer, multithreading and other tricks. Zmq also helps you encapsulate these things.
All in all, zmq encapsulates many underlying details, making your network program code simple and easy to write.

◇ Simple API
Zmq has few API interfaces and is similar to BSD socket in style. If you have used the socket API to write programs, it is very easy to get started with zmq. If you are a Java programmer and have worked on JMS APIs (such as activemq), you will find that the APIs of the two are quite different. By the way, I complained: Java's JMS API is really complicated!

◇ Specific examples
To increase persuasiveness, the echo server code (echo server is the simplest server program) implemented in python is given below. It returns the received information to the client as it is ).

# Server programs
Import zmq
Context = zmq. Context ()
Socket = context. socket (zmq. REP)
Socket. BIND ("TCP: // FIG: 1234 ")

While true:
MSG = socket. Recv ()
Socket. Send (MSG)

# Client programs
Import zmq
Context = zmq. Context ()
Socket = context. socket (zmq. req)
Socket. Connect ("TCP: // 127.0.0.1: 1234 ")
Msg_send = "XXX" socket. Send (msg_send)
Print "Send:", msg_send
Msg_recv = socket. Recv ()
Print "receive:", msg_recv

From the sample code above, everyone should feel that zmq is very simple to use.

★Flexible

The so-called flexibility mainly refers to the following two aspects.

◇ Applicable to multiple communication environments
Zmq supports multiple communication environments (in-process, cross-process, and cross-host) flexibly ). Zmq APIs are well designed so that your code can be applied to different communication environments only when you make small changes (or even no changes.
In the preceding example, socket. Connect ("TCP: // 127.0.0.1: 1234") is used "). "TCP: // 127.0.0.1: 1234" indicates the address string of the Communication peer. Zmq uses the following format for agreed address strings:Transport: // endpoint. The transport in front of the address string indicates the communication type. Currently, inproc, IPC, TCP, and PGM are supported, supports multicasting.
For programmers, if you save the communication address string to the configuration file, you can use a set of code to handle multiple communication methods!

◇ Supports multiple communication modes
Zmq summarizes common communication scenarios and summarizes the following modes.
Pub and sub
REQ and rep
REQ and Router
Dealer and rep
Dealer and Router
Dealer and dealer
Router and Router
Push and pull
Pair and pair
Due to the limited length, I will not go into details about each mode. If you are interested, please refer to the official documentation ("here ").

★Cross-language

Why should I emphasize cross-language features? Generally, software systems that require network communication libraries are distributed systems to some extent. If the developed distributed system is complex, it is difficult to use a programming language. Therefore, it is common to use multiple programming languages in a slightly complex distributed system (at least in my experience ). Therefore, zmq'sCross-languageFeatures are very important.
The documents on the official website provide the following examples of many programming languages (links are "here "). To avoid competition for programming language rankings, the following are listed in alphabetical order.
Ada, basic, C #, C, C ++, Common LISP, Erlang, go, Haskell, haxe, Java, JavaScript (node. JS), Lua, objective-C, PHP, Perl, Python, racket, Ruby, Scala
The list of languages is too full. I have never heard of two languages. The zmq encapsulation library can be found in common programming languages.

★High Performance

Speaking of performance, this is the main highlight of zmq. First, zmq is developed with C/C ++ (the performance of C/C ++ is recognized as a drop). Second, the Protocol format of zmq itself is very simple (comparatively speaking, the Protocol format in the JMS specification is much more complicated ). Therefore, its performance is much higher than that of other message queue software. It can even be said that the performance of zmq is comparable to that of traditional socket APIs.
In order to give everyone a perceptual knowledge, I specially found the performance evaluation of Message Queue software. This is a post written by a foreigner ("here"). If you do not understand foreign languages, you can read "here ". If you are too lazy to read the Post, you can directly read it.



We can see that zmq is a great choice compared to several other MQ! The performance is not at the same level.

★Summary

In general, zmq is a network communication library worth trying. Even if you cannot use it in your work, it is good to play in your spare time.
After this post is published, if you are interested in a large number of people, I will talk about some in-depth topics based on the feedback.

Copyright Notice
All original articles in this blog are copyrighted by the author. This statement must be reprinted to keep this article complete, and the author's programming preferences and original addresses in the form of hyperlinks should be noted:
Http://program-think.blogspot.com/2011/08/opensource-review-zeromq.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.