批量注销consul 中无用的服务

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_41715077/article/details/101923038

批量注销consul 中无用服务的注册信息

python 3.7

#!/usr/bin/env python

# encoding: utf-8
import requests
import json

host = '172.0.10.210'
port = 8500

#consul中微服务名称
serviceNames='service1,service2'

def get_service():
    url = 'http://'+host+':'+str(port)+'/v1/agent/services'
    data = requests.get(url).json()    
    jsonStr = json.dumps(data)
    print('json '+jsonStr)
    keys = []
    for k in data:    
        serviceName=data[k]['Service']
        if serviceName in serviceNames:
             print(data[k]['Service'])
             print(data[k]['ID'])
             keys.append(k)
    return keys

def del_service(keys):
    url = 'http://'+host+':'+str(port)+'/v1/agent/service/deregister/'
    for sid in keys:
        requests.put(url+sid)
        print('删除 '+sid)

if __name__ == '__main__':
    keys = get_service()
    #del_service(keys)

猜你喜欢

转载自blog.csdn.net/weixin_41715077/article/details/101923038