Discover services with Consul
Discover services with Consul
https://www.consul.io/use-cases/discover-services
Use Consul as a centralized registry that discovers, tracks, and monitors your services. Consul can be your single source of truth for cataloging and maintaining a record of all your services.
https://github.com/mhdbs/consul-python-microservices/blob/master/microservice_sample_1/app.py
import json from time import sleep import socket import consul import requests from flask import Flask app = Flask(__name__) CONSUL_URL = 'http://consul:8500' CONSUL_KEY = 'microservice_1' ADDRESS = socket.gethostbyname(socket.gethostname()) PORT = 9000 @app.route('/') def home(): return 'Hello World from {address}'.format(address=ADDRESS) @app.route('/health') def hello_world(): data = { 'status': 'healthy' } return json.dumps(data) @app.route('/register') def register(): url = CONSUL_URL + '/v1/agent/service/register' data = { 'Name': 'microservice_1', 'Tags': ['flask_app_python'], 'Address': ADDRESS, 'Port': 9000, 'Check': { 'http': 'http://{address}:{port}/health'.format(address=ADDRESS, port=PORT), 'interval': '10s' } } app.logger.debug('Reg: ', data) res = requests.put( url, data=json.dumps(data) ) c = consul.Consul() index = None index, data = c.kv.get(CONSUL_KEY, index=index) app.logger.debug("KV Value : ",data['Value']) return res.text if __name__ == '__main__': sleep(8) try: app.logger.debug(register()) except: app.logger.debug('exception happend') pass app.run(debug=True, host="0.0.0.0", port=PORT)
Demo
https://github.com/fanqingsong/consul-template-docker-compose/tree/master
https://github.com/fanqingsong/consul_demo
出处:http://www.cnblogs.com/lightsong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
标签:
DevOps
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
2023-04-14 B. Two Sets