发现服务客户端
discovery-client的Python项目详细描述
发现客户端
consul的发现客户端包同步/异步。
安装
使用pip安装和更新:
pip install -U discovery-client
依赖关系
仅异步客户端
用法示例
使用标准客户端
fromdiscovery.clientimportConsuldc=Consul('localhost',8500)dc.find_service('consul')
与烧瓶结合+穿线。
importjsonimportthreadingfromdiscovery.clientimportConsulfromflaskimportFlaskapp=Flask(__name__)dc=Consul('discovery',8500)dc.register('standard-client',5000)@app.route('/manage/health')defhealth():returnjson.dumps({'status':'UP'})@app.route('/manage/info')definfo():returnjson.dumps({'app':'standard-client'})@app.before_first_requestdefenable_service_registry():defprobe_discovery_connection():dc.consul_is_healthy()thread=threading.Thread(target=probe_discovery_connection)thread.start()
使用异步
使用异步的客户端
importasynciofromdiscoveryimportaioclientloop=asyncio.get_event_loop()dc=aioclient.Consul('localhost',8500,loop)search_one_task=loop.create_task(dc.find_service('consul'))search_all_task=loop.create_task(dc.find_services('consul'))loop.run_until_complete(search_one_task)loop.run_until_complete(search_all_task)
使用aiohttp
使用aiohttp+异步的服务器
importasynciofromaiohttpimportwebfromdiscovery.aioclientimportConsulasyncdefservice_discovery(app):app.loop.create_task(dc.register('aio-client',5000))asyncio.sleep(15)app.loop.create_task(dc.consul_is_healthy())asyncdefhandle_info(request):returnweb.json_response({'app':'aio-client'})asyncdefhandle_status(request):returnweb.json_response({'status':'UP'})app=web.Application()dc=Consul('discovery',8500,app.loop)app.on_startup.append(service_discovery)app.add_routes([web.get('/manage/health',handle_status),web.get('/manage/info',handle_info)])web.run_app(app,host='0.0.0.0',port=5000)
链接
- 许可证:Apache License
- 代码:https://github.com/amenezes/discovery-client
- 问题跟踪器:https://github.com/amenezes/discovery-client/issues