python获取nacos配置
下包:
nacos-sdk-python
github地址:https://github.com/nacos-group/nacos-sdk-python
# 获取服务列表单个服务ip
def get_service_ip(self, service_name):
try:
SERVER_ADDRESSES = "ip:port" # nacos的ip:port
NAMESPACE = "xxx" # 命名空间的id: namespace id
client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, username="xxx", password="xxxx")
service_info = client.list_naming_instance(service_name=service_name, clusters="DEFAULT", group_name="DEFAULT_GROUP")
if service_info:
service_ip = dict(service_info)["hosts"][0]["ip"]
return service_ip
else:
return "未找到服务信息"
except Exception as e:
self.logger.info(f"获取服务ip异常: {e}")
raise e