python中的soa框架
vyked的Python项目详细描述
vyked是面向服务架构的基于异步的python框架。
要求
- python>;=3.3
- 异步https://pypi.python.org/pypi/asyncio
安装
$ pip install vyked
vyked使用jsonstreamer,一个用于python的快速流式json解析器 使用yajl c库生成类似sax的事件。安装yajl:
$ git clone git@github.com:lloyd/yajl.git $ cd yajl $ ./configure && make install
开始
服务注册
当vyked服务启动时,它们通过公布它们的名称、ip、端口以及它们可能需要运行的其他vyked服务的名称,在服务注册中心注册。然后,注册表协调所有vyked服务,甚至平衡每个服务的多个实例的负载。
vyked使用redis作为pub sub和注册表存储,在启动vyked注册表之前,必须启动redis实例。
您必须在任何服务之前启动vyked注册表,例如:
$ python -m vyked.registry
或:
fromvykedimportRegistryregistry=Registry('127.0.0.1',4500)registry.start()
服务
vyked允许您托管http和tcp服务。
TCP服务
vyked tcp服务通过用decorator装饰典型的类方法来提供类似rpc的api。有两种类型的API可用,
- 使用@api提供这种api的请求-响应和从远程客户端使用它的@request
- 发布-订阅使用@publish自动发布事件,使用@subscribe在远程客户端接收事件
下面的基本示例演示了服务和远程客户端的这些装饰器。
TCP服务示例:
fromasyncioimportsleepfromvykedimportHost,TCPService,api,publishclassIdentityTCPService(TCPService):def__init__(self,ip,port):super(IdentityTCPService,self).__init__("IdentityService",1,ip,port)@apidefcreate(self,user_name,password):result=yield fromsleep(5)#long running taskifuser_nameisNone:raiseException('username cannot be none')returnresult@publishdefpassword_changed(self,user_name):""" calling this method from within your code will cause a 'password_changed' event to be published to all subscribing services """# @publish decorated methods must return a dict of values to be publishedreturnlocals()# easy way to return a dict containing all the params - in this case, user_name.
http服务示例:
vyked使用aiohttp设置http服务器。
fromvykedimportHost,HTTPService,get,post,Response,RequestclassIdentityHTTPService(HTTPService):def__init__(self,ip,port):super(IdentityHTTPService,self).__init__("IdentityService",1,ip,port)@get(path='/users/{username}')defget(self,request:Request):username=request.match_info.get('username')returnResponse(status=200,body=("Hello {}".format(username)).encode())@post(path='/users/{username}')defcreate(self,request:Request):data=yield fromrequest.json()returnResponse(status=200,body=(json.dumps(data)).encode())
启动服务:
if__name__=='__main__':http=IdentityHTTPService('0.0.0.0',4501)tcp=IdentityTCPService('0.0.0.0',4502)Host.registry_host='127.0.0.1'Host.registry_port=4500Host.pubsub_host='127.0.0.1'Host.pubsub_port=6379Host.name='Identity'Host.attach_service(http)Host.attach_service(tcp)Host.run()
客户
到目前为止,这些示例只涉及独立服务。但服务可能与其他服务交互。 为了实现这种交互,vyked提供了一个tcp和http客户端,分别与tcp和http服务交互。
我们在上面的示例中看到的identityservice的tcp客户机示例:
fromvykedimportHost,TCPService,TCPServiceClient,api,publish,request,subscribeimportasyncioclassIdentityClient(TCPServiceClient):def__init__(self):super(IdentityClient,self).__init__("IdentityService",1)@requestdefcreate(self,user_name,password):returnlocals()#@request requires a dict containing params describing the request payload@subscribedefpassword_changed(self,user_name):print("Password changed event received")yield fromasyncio.sleep(4)
示例http客户端:
classHello(HTTPServiceClient):def__init__(self):super(Hello,self).__init__('Hello',1)@get()defperson(self,name):path='/{}'.format(name)params={'key':'value'}headers={'Content-Type':'application/json'}app_name='test'returnlocals()
文档
在read-the-docs查看文档
许可证
vyked根据麻省理工学院的许可证提供。
源代码
github存储库中提供了最新的开发人员版本: https://github.com/kashifrazzaqui/vyked