ZMQ 各种 socket 介绍

    • router – dealer 模式

为什么有  router – dealer 模式 ?      处理多client , 多service 的情况


1个client,多个service , 最简单的设计方法是: connect each client socket to multiple service endpoints


One client socket can connect to multiple service sockets, and the REQ socket will then distribute requests among these services. Let’s say you connect a client socket to three service endpoints; A, B, and C. The client makes requests R1, R2, R3, R4. R1 and R4 go to service A, R2 goes to B, and R3 goes to service C.

This design lets you add more clients cheaply. You can also add more services. Each client will distribute its requests to the services. But each client has to know the service topology. If you have 100 clients and then you decide to add three more services, you need to reconfigure and restart 100 clients in order for the clients to know about the three new services.

不好的地方是: client必须知道所有的service , 当新增若干个service,每一个client 都要修改并重启

router 或者 dealer 就是在client 和 service 之间的中间层  , 用 zmq_poll() 实现: What we want is something sitting in between clients and services that centralizes all knowledge of the topology. Ideally, we should be able to add and remove services or clients at any time without touching any other part of the topology.



In this simple extended request-reply pattern, REQ talks to ROUTER and DEALER talks to REP.


  • 来源文档:


How to use ZMQ

  • simple example :

server side :

client side : 


  • server side code

  • client code :


  • server pom.xml


How to search a certain word through whole project in eclipse

same problem from  stackoverflow is :

How to search in Eclipse through project for some word ? When I start CTRL-h I need to choose field, method or …., but I need to find all occurences in project no metter method, field or .. ! How to achieve that ?


Answer is :


CTRL + H -> File Search -> Containing Text ‘some word’
make sure the scope is set to workspace or enclosing projects. Choosing Selected Resources will narrow the result set to the folder or file you selected in the explorer.

Further if you know the file type that might contain the text you can use the File Name patterns with values like *.php, *.phtml, *.html

Thrift introduce

  • data type

bool               A boolean value, true or false
byte               A signed byte
i16                  A 16-bit signed integer
i32                  A 32-bit signed integer
i64                 A 64-bit signed integer
double         A 64-bit floating point number
string           An encoding-agnostic text or binary string

  • struct

A struct is essentially equivalent to a class in object oriented
programming languages. A struct has a set of strongly typed
fields, each with a unique name identifier. The basic syntax for
defining a Thrift struct looks very similar to a C struct definition.
Fields may be annotated with an integer field identifier (unique to
the scope of that struct) and optional default values. Field identifiers
will be automatically assigned if omitted, though they are strongly
encouraged for versioning reasons discussed later.

Example of struct


  • Service

Services are defined using Thrift types. Definition of a service is
semantically equivalent to defining an interface (or a pure virtual
abstract class) in object oriented programming. The Thrift compiler
generates fully functional client and server stubs that implement the
interface. Services are defined as follows:


An example is  :


Note :  an async modifier keyword may be added to a void function,  which will generate code that does not wait for a response from the server.


  • Servers and Multithreading

Thrift services require basic multithreading to handle simultaneous
requests from multiple clients


****  Important 

Now I will create a RouteServer which supply a “QueryRoute” function , client send Anumber and Bnumber as parameter , server will reply a struct include 2 int and 1 string ,  how to write this service ?

  • First define the struct in thrift file

  • then define the interface


Full thrift file is :    name is “RouteServer.thrift”


goto folder :   /home/liuyang/thrift-0.11.0/tutorial

run command :  rm -r gen-csharp    ,   delete  existing folder

then run command :   thrift -r –gen csharp RouteServer.thrift


Then goto /home/liuyang/thrift-0.11.0/tutorial/gen-csharp , copy “RouteServer.cs” and “InvalidOperation.cs” and “Ret.cs” to windows machine console application ,  add to project .

For server side “Program.cs” ,change it to


For client “Program.cs” ,change it to


Now client can call server function and get reply .