falsk注册etcd
部署web服务集群时,我们希望能动态调整集群大小。当一个新的节点启动时,可以将自己的信息注册给master, 让master把它加入到集群里, 关闭之后也可以把自己从集群中删除。我这里使用的是flask + etcd + confd +nginx来实现。如,新增一个flask节点时,将当前的启动的ip和端口,写到etcd中,设置超时时间如20秒,每隔一段时间如15秒刷新下etcd中的可以。confd从etcd中获取信息,生成nginx的配置文件,重载nginx来扩展或删除新的节点。
1、flask启动后台定时线程来注册etcd
如何在flask中启动后台线程可以参考这里:http://stackoverflow.com/questions/14384739/how-can-i-add-a-background-thread-to-flask
POOL_TIME = 15 #Seconds # lock to control access to variable dataLock = threading.Lock() # thread handler etcdThread = threading.Thread() #etcd node node = None def create_app(flask_name, service_name, service_port): app = Flask(flask_name) def interrupt(): global etcdThread etcdThread.cancel() def refreshEtcd(): global etcdThread, node with dataLock: refresh_etcd(node) etcdThread = threading.Timer(POOL_TIME, refreshEtcd, ()) etcdThread.start() def registerEtcd(): # Do initialisation stuff here global etcdThread, node with dataLock: node register_etcd(service_name, service_port) # 定时刷新etcd etcdThread = threading.Timer(POOL_TIME, refreshEtcd, ()) etcdThread.start() # 注册 registerEtcd() atexit.register(interrupt) return app
2、python中注册etcd
etcd的python客户端用的是python-etcd,项目地址https://github.com/jplana/python-etcd,目前仅支持etcd v2。
相关代目如下
client = None def register_etcd(service_name, port): import etcd global client etcd_ip, etcd_port = getConfig('etcd', 'client').split(':') client = etcd.Client(host=etcd_ip, port=int(etcd_port)) host_name = socket.gethostname() node_name = '/api/%s/upstream/%s' % (service_name, host_name.replace('-', '_').lower()) client.write(node_name, '%s:%s' % (host_name, port), ttl=20) #超时时间20秒 return node_name def refresh_etcd(node_name): client.refresh(node_name, ttl=20)