python应用在Nacos中注册服务

本文介绍了如何使用Python实现通过Nacos API进行服务实例的注册和续期,以及定期发送心跳以维护服务状态。重点在于request_heart函数,它负责定时发送请求更新服务实例信息。

主要是通过request进行续期:

# --*-- coding=utf-8 --*--
# author BillFang

import time
import threading
import requests
from urllib.parse import urlencode
from sanic import Sanic
import json
from sanic.response import json as js

app = Sanic("python ap")


@app.get("/api/python")
def home(request):
    print("你调用到这个地方了。。。{}")
    return js({"helloe": "world"})

def add_instance(username, password, service_name, ip, port, cluster_name=None,
                  weight=1.0, metadata=None, ephemeral=True, group_name="DEFAULT_GROUP"):
    params = {
        "ip": ip,
        "port": port,
        "serviceName": service_name,
        "weight": weight,
        "enable": True,
        "healthy": True,
        "clusterName": cluster_name,
        "ephemeral": ephemeral,
        "groupName": group_name
    }
    nacos_config_url = "http://localhost:8848/nacos/v1/ns/instance"
    resp = requests.post(url=nacos_config_url, data=params)
    if resp.status_code == 200:
        request_heart(us
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值