├── README.md ├── core ├── FlaskExtRpcClient.py └── __init__.py ├── test_client.py └── test_server.py /README.md: -------------------------------------------------------------------------------- 1 | ## Flask-Ext-ZeroRpcClient 2 | 基于zero-rpc的flask-rpc扩展 3 | 4 | ##### 1.使用场景 5 | 以flask作为wsgi服务器前端作为交互,以zero-rpc作为内部集群代码交互模块 6 | 7 | ##### 1.1 例子 8 | 首先: 9 | 10 | python test_client.py 11 | 12 | python test_server.py 13 | 14 | 然后,打开浏览器: 15 | 16 | 输入:http://127.0.0.1:5001/test-zeroRpc 17 | 18 | 19 | ##### 2.环境依赖 20 | python2.7, flask, zero-rpc 21 | 22 | ##### 更多 23 | zerorpc https://github.com/0rpc/zerorpc-python 24 | flask-doc http://docs.jinkan.org/docs/flask/ 25 | -------------------------------------------------------------------------------- /core/FlaskExtRpcClient.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import threading 4 | 5 | import zerorpc 6 | 7 | import traceback 8 | 9 | try: 10 | from flask import _app_ctx_stack as stack 11 | except ImportError: 12 | from flask import _request_ctx_stack as stack 13 | 14 | global_data = threading.local() 15 | RPC_URI = ['tcp://127.0.0.1:5007'] 16 | 17 | class RpcClient(zerorpc.Client): 18 | def __init__(self, app=None, auto_close=False): 19 | self.app = app 20 | if app is not None and auto_close: 21 | self.init_app(app) 22 | 23 | def init_app(self, app): 24 | app.config.setdefault('RPC_CLI', ':memory:') 25 | # Use the newstyle teardown_appcontext if it's available, 26 | # otherwise fall back to the request context 27 | if hasattr(app, 'teardown_appcontext'): 28 | app.teardown_appcontext(self.teardown) 29 | else: 30 | app.teardown_request(self.teardown) 31 | 32 | def connect(self): 33 | if not hasattr(global_data, "cli"): 34 | global_data.cli = zerorpc.Client(connect_to=RPC_URI) 35 | print ' global_data.cli zerorpc.Client connect_to ', RPC_URI 36 | else: 37 | test = global_data.cli.is_alive() 38 | if not test: 39 | try: 40 | global_data.cli.close() 41 | except Exception, e: 42 | traceback.print_exc() 43 | global_data.cli = zerorpc.Client(connect_to=RPC_URI) 44 | print ' global_data.cli zerorpc.Client connect_to ', RPC_URI 45 | else: 46 | pass 47 | return global_data.cli 48 | 49 | def connect2(self): 50 | return zerorpc.Client(connect_to=RPC_URI,timeout=60, heartbeat=30) 51 | 52 | def __del__(self): 53 | if hasattr(global_data, "cli"): 54 | try: 55 | print ' global_data.cli zerorpc.Client __del__ close' 56 | global_data.cli.close() 57 | except Exception, e: 58 | traceback.print_exc() 59 | 60 | def teardown(self, exception): 61 | ''' 62 | 每次连接完毕都会关闭cli,消耗会比较大 63 | :param exception: 64 | :return: 65 | ''' 66 | ctx = stack.top 67 | if hasattr(ctx, 'rpc_cli'): 68 | print "DEL RPC CLIENT" 69 | ctx.rpc_cli.close() 70 | 71 | @property 72 | def connection(self): 73 | ctx = stack.top 74 | if ctx is not None: 75 | if not hasattr(ctx, 'rpc_cli'): 76 | # ctx.rpc_cli = self.connect2() 77 | ctx.rpc_cli = self.connect() 78 | return ctx.rpc_cli 79 | -------------------------------------------------------------------------------- /core/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leecodedog/Flask-Ext-ZeroRpcClient/356b20302983b63f486aede100461e17a50ea454/core/__init__.py -------------------------------------------------------------------------------- /test_client.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | 5 | from flask import Flask, send_from_directory, app 6 | from core.FlaskExtRpcClient import RpcClient 7 | 8 | app = Flask(__name__) 9 | 10 | rpc_cli = RpcClient(app) 11 | 12 | 13 | @app.route('/test-zeroRpc') 14 | def test(): 15 | cli = rpc_cli.connection 16 | word = cli.hello() 17 | return word 18 | 19 | 20 | if __name__ == '__main__': 21 | app.run(debug=True, port=5001, host='0.0.0.0', threaded=False) 22 | -------------------------------------------------------------------------------- /test_server.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import zerorpc 5 | import os 6 | import time 7 | 8 | 9 | class RpcServer(object): 10 | """docstring for TestRpcServer""" 11 | 12 | def __init__(self): 13 | super(RpcServer, self).__init__() 14 | 15 | @staticmethod 16 | def hello(): 17 | return "hello world" 18 | 19 | @staticmethod 20 | def is_alive(): 21 | return True 22 | 23 | 24 | def get_localtime(): 25 | return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) 26 | 27 | 28 | if __name__ == '__main__': 29 | urls = 'tcp://127.0.0.1:5007' 30 | s = zerorpc.Server(RpcServer()) 31 | s.bind(urls) 32 | print get_localtime(), 'start Parser RPC Server s1', urls, 'pid :', os.getpid() 33 | s.run() 34 | --------------------------------------------------------------------------------