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()

你可能感兴趣的:(实践,python,开发语言,后端)