python3.8使用nacos 加入watcher 后报错

来源:29-6 service层集成nacos

qq_慕的地1326115

2022-07-12

图片描述
不加watcher 是正常的

写回答

3回答

石小咚丶

2023-03-08

参考 [同学的建议]https://coding.imooc.com/learn/questiondetail/Dg8m5YZLkja6Wa4B.html , 我觉得可以尝试使用重载的方式解决这个问题,Python 3.9

import json
import platform
from multiprocessing import Process
from threading import Thread

import nacos
from loguru import logger
from nacos import NacosException
from nacos.params import group_key
from nacos.commons import synchronized_with_attr
from nacos.client import process_common_config_params, WatcherWrap


class NacosClient(nacos.NacosClient):
    """"""

    @synchronized_with_attr("pulling_lock")
    def add_config_watchers(self, data_id, group, cb_list, content=None):
        if not cb_list:
            raise NacosException("A callback function is needed.")
        data_id, group = process_common_config_params(data_id, group)
        logger.info("[add-watcher] data_id:%s, group:%s, namespace:%s" % (data_id, group, self.namespace))
        cache_key = group_key(data_id, group, self.namespace)
        wl = self.watcher_mapping.get(cache_key)
        if not wl:
            wl = list()
            self.watcher_mapping[cache_key] = wl
        if not content:
            content = self.get_config(data_id, group)
        last_md5 = NacosClient.get_md5(content)
        for cb in cb_list:
            wl.append(WatcherWrap(cache_key, cb, last_md5))
            logger.info("[add-watcher] watcher has been added for key:%s, new callback is:%s, callback number is:%s" % (
                cache_key, cb.__name__, len(wl)))

        if self.puller_mapping is None:
            logger.debug("[add-watcher] pulling should be initialized")
            self._init_pulling()

        if cache_key in self.puller_mapping:
            logger.debug("[add-watcher] key:%s is already in pulling" % cache_key)
            return

        for key, puller_info in self.puller_mapping.items():
            if len(puller_info[1]) < self.pulling_config_size:
                logger.debug("[add-watcher] puller:%s is available, add key:%s" % (puller_info[0], cache_key))
                puller_info[1].append(cache_key)
                self.puller_mapping[cache_key] = puller_info
                break
        else:
            logger.debug("[add-watcher] no puller available, new one and add key:%s" % cache_key)
            key_list = self.process_mgr.list()
            key_list.append(cache_key)
            sys_os = platform.system()
            if sys_os == 'Windows' or sys_os == "Darwin":
                puller = Thread(target=self._do_pulling, args=(key_list, self.notify_queue))
                puller.setDaemon(True)
            else:
                puller = Process(target=self._do_pulling, args=(key_list, self.notify_queue))
                puller.daemon = True
            puller.start()
            self.puller_mapping[cache_key] = (puller, key_list)


使用重载的方式在以后更换环境或者服务上线的时候会更加方便一些


addr = ""
group = ""
data_id = ""
namespace = ""

def dispose():
    pass

client = NacosClient(addr, namespace=namespace, username="nacos", password="nacos")

if __name__ == "__main__":
    client.add_config_watcher(data_id, group, dispose)
0
0

城中城

2022-07-13

别人已经解决了这个问题  你可以看看别人的

https://coding.imooc.com/learn/questiondetail/Dg8m5YZLkja6Wa4B.html

nacos最新的版本 已经没有这个问题了  我是3.9.1


0
0

qq_慕的地1326115

提问者

2022-07-12

回退到3.7版本是正常的,nacos 有的功能应该是目前不支持3.8版本

0
1
bobby
可以看看城中城同学的回复
2022-07-15
共1条回复

Go+Python打造电商系统 自研微服务框架 抓紧高薪机遇

快速转型Go工程师,成为具备双语言后端能力的开发者

508 学习 · 530 问题

查看课程