python注册nacos(基于open-api)

python注册nacos(基于open-api),第1张

python注册nacos(基于open-api)
import json
import requests
import time


NACOS_IP = 'localhost'
NACOS_PORT = '8848'
SERVICE_NAME = 'python-service'
SERVICE_IP = 'localhost'
SERVICE_PORT = '8000'


def create_instance():
    create_instance_url = 'http://'+NACOS_IP+':'+NACOS_PORT+'/nacos/v1/ns/instance?healthy=true&port=' + 
        SERVICE_PORT+'&ip='+SERVICE_IP+'&serviceName='+SERVICE_NAME
    response = requests.post(create_instance_url)
    print('创建实例请求返回 '+response.text)


def beat():
    beat_info = {
        'ip': SERVICE_IP,
        'port': SERVICE_PORT,
        'serviceName': SERVICE_NAME
    }
    beat_url = 'http://'+NACOS_IP+':'+NACOS_PORT + 
        '/nacos/v1/ns/instance/beat?serviceName=' + 
        SERVICE_NAME+'&beat='+json.dumps(beat_info)
    while True:
        response = requests.put(beat_url)
        print('心跳请求返回 '+time.strftime("%Y-%m-%d %H:%M:%S",
              time.localtime()) + ' ' + response.text)
        time.sleep(5)


def init():
    create_instance()
    beat()


if __name__ == '__main__':
    init()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5695768.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存