本文共 2475 字,大约阅读时间需要 8 分钟。
Zeromq 是基于zeromq、gevent和 msgpack开发的分布式RPC框架zerorpc-python。这个框架简单、易用。
1. 安装zeromq
1 2 3 4 5 6 | yum -y install zeromq yum install gcc gcc-c++ libuuid-devel python-uuid uuid wget http: //download.zeromq.org/zeromq-2.1.9.tar.gz ./configure make make install |
2.安装gevent
1 | pip install gevent |
3. 安装zerorpc
1 | pip install zerorpc |
server的测试代码:
1 2 3 4 5 6 7 8 9 | import zerorpc import time class HelloRPC(object): def hello(self, name): print "this is %s %s" %(name,time.strftime( '%Y-%m-%d %H-%m-%S' ,time.localtime(time.time()))) return "Hello, %s" %time.strftime( '%Y-%m-%d %H-%m-%S' ,time.localtime(time.time())) s = zerorpc.Server(HelloRPC()) s.bind( "tcp://0.0.0.0:4242" ) s.run() |
client的测试代码
1 2 3 4 5 | import zerorpc import os,sys c = zerorpc.Client() c.connect( "tcp://127.0.0.1:4242" ) print c.hello( 'www.ruifengyun.com' ) |
也可以传送cmd的命令,然后return结果,这个有点像是 zeromq的req方法
客户端也可以独立的存在
1 | zerorpc --client --connect tcp: //127.0.0.1:4242 hello lllllllllllllllllllllllllll |
客户端显示:
服务端显示:
现在看来,如果按照mq的性能对比的话,这个结果还是令人不太满意的,但这个是应用于rpc远程调用的场景内,外部调用的话一秒中可以可以发送并接受10次的调用,内部调用的话,可以每秒钟打到300次左右,但这个结果还是可以接受的,毕竟没有谁每时每刻都在用远端的函数的。。。。。。。
这个一个老外的测试数据:
1 2 3 4 | xmlrpc client & server (single-threaded): 850 calls/sec zerorpc client & server (single-threaded): 4500 calls/sec xmlrpc client & server (multi-threaded): 1200 calls/sec zerorpc client & server (multi-threaded): 11000 calls/sec |
我很纳闷他是怎么测试出来的~
如果不需要return结果,只是类pub数据过去的话,是可以到600多,但是远达不到他们的数据。。。
很奇怪~
为了测试性能我把gevent引入了,测试的结果不稳定,以我对gevent协程框架的使用,他很少出这样的情况,问题很有可能是server的问题。。。
测试的结果:
完成的测试代码,以供大家学习测试:
server 测试代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Cooler: def add_man(self, sentence): return the result. "" " return sentence + ", man!" def add_42(self, n): result. "" " return n + 42 def boat(self, sentence): because it's cooler. "" " return "I'm on a boat!" import zerorpc s = zerorpc.Server(Cooler()) s.bind( "tcp://0.0.0.0:4242" ) s.run() |
client 测试代码:
1 2 3 4 5 6 | $ zerorpc -j tcp: //:4242 add_42 1 43 $ zerorpc tcp: //:4242 add_man 'I own a mint-condition Volkswagen Golf' "I own a mint-condition Volkswagen Golf, man!" $ zerorpc tcp: //:4242 boat 'I own a mint-condition Volkswagen Golf, man!' "I'm on a boat!" |
zerorpc 官方推荐的高可用的方法是用haproxy实现的
1 2 3 4 5 6 7 8 | haproxy.cfg的配置 $cat haproxy.cfg listen zerorpc 0.0 . 0.0 : 1111 mode tcp server backend_a localhost: 2222 check server backend_b localhost: 3333 check 检测语法 $ haproxy -f haproxy.cfg |
可以进行语句的超时处理
本文转自 rfyiamcool 51CTO博客,原文链接:http://blog.51cto.com/rfyiamcool/1254000,如需转载请自行联系原作者