def publishconfig(): if request.method == 'POST': try: data_id = request.form['dataid'] group = request.form['group'] content = request.form['content'] env = request.form['env'] if env == 'all': nacos_servers = servers elif env == 'stage': nacos_servers = servers[0:2] elif env == 'qa': nacos_servers = servers[-2:] else: return "Error no env select" print(request.form, env, nacos_servers) for server in nacos_servers: address = server[0] namespace = server[1] client = nacos.NacosClient(address, namespace=namespace, username="******", password="******") client.publish_config(data_id, group, content, '5') return "OK" except Exception as e: print(e) return "ERROR", 503 else: return render_template('publish.html')
def get_vesync_database_info_from_nacos(self) -> dict: """ Get database info from Nacos. :return: dict containing database info """ nacos_client = nacos.NacosClient(self.nacos_server.host, namespace=self.stage_namespace_id) self.set_nacos_client_debug(nacos_client) configs = nacos_client.get_config(settings.VESYNC_DATABASE_DATA_ID, settings.VESYNC_DATABASE_GROUP, no_snapshot=True) logger.debug( f"configs (data id: {settings.VESYNC_DATABASE_DATA_ID}, group: {settings.VESYNC_DATABASE_GROUP}): " f"{configs}") configs = common.load_properties_from_string(configs) database_info = { "host": configs[settings.KEY_TO_VESYNC_DATABASE_HOST], "port": int(configs[settings.KEY_TO_VESYNC_DATABASE_PORT]), "user": configs[settings.KEY_TO_VESYNC_DATABASE_USER], "password": configs[settings.KEY_TO_VESYNC_DATABASE_PASSWORD], "database": configs[settings.KEY_TO_VESYNC_DATABASE_NAME], "charset": "utf8", "cursorclass": pymysql.cursors.DictCursor } logger.info(f"database info used to connect: {database_info}") return database_info
def ifFirmwareInfoHasChanged(): SERVER_ADDRESSES = "34.234.176.173" NAMESPACE = "env-01" client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, username="******", password="******") data_id = "device.properties.firmwareInfo" group = "DEVICE" changed_list = [] configResult = client.get_config(data_id, group) if configResult is None: device_type_dict = get_dict_byFirmware() device_type_dict = yaml.dump(device_type_dict) client.publish_config(data_id, group, device_type_dict) elif configResult is not None: configResult = yaml.safe_load(configResult) device_type_dict_2 = get_dict_byFirmware() for diff in list(dictdiffer.diff(configResult, device_type_dict_2)): # print(diff) changed_list.append(diff) if len(changed_list) != 0: device_type_dict = yaml.dump(device_type_dict_2) client.publish_config(data_id, group, device_type_dict) m_test = DingTalkRobot( 'https://oapi.dingtalk.com/robot/send?access_token=bfe75c087830cdd46c321c5723c7706bec56fe261f6b6900dbf33a02b7ce9724' ) m_test.setParameter(f"{changed_list}") m_test.sendMessage() return changed_list
def test_long_pulling(self): client2 = nacos.NacosClient(SERVER_ADDRESSES, username=USERNAME, password=PASSWORD) d = "test1_pulling" g = "Group1" g2 = "Group2" class Share: content = None def cb(x): Share.content = x["content"] print(Share.content) client2.publish_config(d, g, "test2") client2.publish_config(d, g2, "test2") time.sleep(0.5) # test common client2.add_config_watcher(d, g, cb) client2.add_config_watcher(d, g2, cb) time.sleep(0.5) client2.publish_config(d, g, "test") client2.publish_config(d, g2, "test") time.sleep(1) self.assertEqual(Share.content, "test") client2.publish_config(d, g2, u"test2中文") time.sleep(1) self.assertEqual(Share.content, u"test2中文")
def publish_one_stage_summary(self, stage, publish_for_debug=False): """ Find summary property file and publish to namespace 'summary' if file exist. Args: stage: stage flag publish_for_debug: publish DEBUG summary to group DEBUG if set to True """ logger.debug( f"Handle publishing stage summary properties, stage: {stage}, publish for debug: {publish_for_debug}" ) nacos_client = nacos.NacosClient(self.nacos_server.host, namespace=self.summary_namespace_id) self.set_nacos_client_debug(nacos_client) summary_group = self.summary_group_debug if publish_for_debug else self.summary_group_stable summary_file_name = "+".join( [stage, summary_group, self.summary_namespace_id]) summary_file_path = os.path.join(self.nacos_snapshot_repo_dir, summary_file_name) if os.path.exists(summary_file_path): logger.debug( f"summary file for stage {stage} exists: {summary_file_path}") with open(summary_file_path, "r") as summary: content = self.sync_task_reason + "\n\n" + summary.read() nacos_client.publish_config(stage, summary_group, content) logger.success( f"Succeed to publish summary properties for stage {stage} " f"with content from file {summary_file_name}.") else: logger.debug( f"summary file for stage {stage} does not exist: {summary_file_path}" )
def init_scheduler(app, scheduler): f = open("scheduler.lock", "wb") try: #linux fcntl # fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) # 初始化微服务nacos注册 from myolap.utils.nacosutils import reg_nacos_client client = nacos.NacosClient(usingconfig.NACOS_SERVER_ADDRESSES, namespace=usingconfig.NACOS_NAMESPACE) client.default_timeout = 30 reg_nacos_client(client) # 定时发送心跳任务 scheduler.add_job(func=send_hearbeat, id='1', args=[client], trigger='interval', seconds=usingconfig.NACOS_HEARTBEAT_INTERVAL, replace_existing=True) scheduler.start() except Exception as e: print('error: %s' % (e)) pass def unlock(): # fcntl.flock(f, fcntl.LOCK_UN) f.close() atexit.register(unlock)
def __init__(self, host, port, nacoshost, appClass): self.port = port self.host = host self.app = appClass(host=host, port=port) self.appname = self.app.appname self.nacoshost = nacoshost self.client = nacos.NacosClient(self.nacoshost)
def test_get_from_snapshot(self): client2 = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, username=USERNAME, password=PASSWORD) client2.current_server = ("1.100.84.215", 8080) d = "test_snap" g = "group" key = "+".join([d, g, NAMESPACE]) files.save_file(client2.snapshot_base, key, u"yyy中文") self.assertEqual(client2.get_config(d, g), u"yyy中文") shutil.rmtree(client2.snapshot_base)
def get_key(x): ''' 获取各个组件的账号密码 :param x: 组件名 :return: 值 ''' client = nacos.NacosClient("127.0.0.1:8848", namespace="public") res = client.get_config("common.json", "DEFAULT_GROUP") return json.loads(res)[x]
def run(self): """Trigger sync when nacos.commit.message changes.""" nacos_client = nacos.NacosClient(self.nacos_server.host) self.set_nacos_client_debug(nacos_client) nacos_client.set_options(no_snapshot=True) nacos_client.add_config_watchers(self.sync_trigger_data_id, self.sync_trigger_group, [self.add, self.dispatch_sync_task]) while True: time.sleep(0.1)
def connect(self): self.client = nacos.NacosClient(self.server_addresses, namespace=self.namespace) self.client.add_naming_instance(self.service_name, self.instance_ip, self.instance_port, None, 1.0, metadata={}) self.beat()
def get_config_info(data_id, group, namespace): SERVER_ADDRESSES = "34.234.176.173" NAMESPACE = namespace client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, username="******", password="******") common_db = client.get_config(data_id, group) common_db = yaml.safe_load(common_db) return common_db
def test_server_failover(self): client2 = nacos.NacosClient("100.69.207.66:8848, %s:8848" %SERVER_1, namespace=NAMESPACE, username=USERNAME, password=PASSWORD) d = "test" g = "DEFAULT_GROUP" content = u"test中文" self.assertEqual(client2.publish_config(d, g, content), True) time.sleep(0.5) self.assertEqual(client2.get_config(d, g), content) self.assertEqual(client2.remove_config(d, g), True) time.sleep(0.5) self.assertEqual(client2.get_config(d, g), None)
def test_server_failover(self): client2 = nacos.NacosClient("100.69.207.66:8848, 100.69.207.65:8848", namespace=NAMESPACE) d = "test" g = "DEFAULT_GROUP" content = u"test中文" self.assertEqual(client2.publish_config(d, g, content), True) time.sleep(0.5) self.assertEqual(client2.get_config(d, g), content) self.assertEqual(client2.remove_config(d, g), True) time.sleep(0.5) self.assertEqual(client2.get_config(d, g), None)
def register_nacos(self, service_name, server_addresses, ip, port, cluster_name): self.service_name = service_name self.server_addresses = server_addresses self.ip = ip self.port = port self.cluster_name = cluster_name self.client = nacos.NacosClient(self.server_addresses, namespace='public') self.client.add_naming_instance(self.service_name, self.ip, self.port, self.cluster_name) self.send_heartbeat()
def sync_firmware_info_to_nacos(self, data: dict): """ Publish data from table firmware_info to Nacos. """ nacos_client = nacos.NacosClient(self.nacos_server.host, namespace=self.stage_namespace_id) self.set_nacos_client_debug(nacos_client) data = yaml.dump(data) logger.debug( f"Update Nacos " f"(data id: {settings.TABLE_FIRMWARE_INFO_DATA_ID}, group: {settings.DATABASE_SNAPSHOT_GROUP})" f"with data {data}") nacos_client.publish_config(settings.TABLE_FIRMWARE_INFO_DATA_ID, settings.DATABASE_SNAPSHOT_GROUP, data)
def __init__(self, nacos_ip, namespace, servicename): self.SERVER_ADDRESSES = nacos_ip self.SERVICENAME = servicename self.NAMESPACE = namespace try: s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.connect(('114.114.114.114',80)) self.ip=s.getsockname()[0] finally: s.close() mylog.logger.info("the machine IP is :"+self.ip) self.client = nacos.NacosClient(self.SERVER_ADDRESSES, namespace=self.NAMESPACE)
def get_device_type_snapshot_from_nacos(self): """ Get snapshot of table device_type from Nacos. """ nacos_client = nacos.NacosClient(self.nacos_server.host, namespace=self.stage_namespace_id) self.set_nacos_client_debug(nacos_client) snapshot = nacos_client.get_config(settings.TABLE_DEVICE_TYPE_DATA_ID, settings.DATABASE_SNAPSHOT_GROUP, no_snapshot=True) logger.debug(f"device_type data from Nacos: {snapshot}") if snapshot: return yaml.safe_load(snapshot) else: return None
def get_firmware_info_snapshot_from_nacos(self): """ Get snapshot of table firmware_info from Nacos. """ nacos_client = nacos.NacosClient(self.nacos_server.host, namespace=self.stage_namespace_id) self.set_nacos_client_debug(nacos_client) snapshot = nacos_client.get_config( settings.TABLE_FIRMWARE_INFO_DATA_ID, settings.DATABASE_SNAPSHOT_GROUP, no_snapshot=True) logger.debug(f"firmware_info data from Nacos: {snapshot}") if snapshot: return yaml.safe_load(snapshot) else: return None
def getconfig(): try: data_id = request.form['dataid'] group = request.form['group'] except: return "ERROR", 503 datas = [] for server in servers: address = server[0] namespace = server[1] client = nacos.NacosClient(address, namespace=namespace, username="******", password="******") datas.append( [address, data_id, group, client.get_config(data_id, group)]) return render_template('getconfig.html', datas=datas)
def download_one_namespace_configs(self, namespace_id, namespace_name, namespace_config_count, snapshot_base): """ Download all configs of specific namespace. Args: namespace_id: id of namespace, passed when initializing NacosClient. namespace_name: name of namespace, used for logging. namespace_config_count: count of configs, passed as page size. snapshot_base: Dir to store snapshot config files. Returns: None """ nacos_client = nacos.NacosClient(self.nacos_server.host, namespace=namespace_id) self.set_nacos_client_debug(nacos_client) nacos_client.set_options(snapshot_base=snapshot_base) logger.info(f"Begin to get configs from namespace: {namespace_name}") nacos_client.get_configs(page_size=namespace_config_count) logger.success( f"Succeed to get configs from namespace: {namespace_name}")
def __init__(self, server_addresses, namespace, username, password) -> None: log.basicConfig(filename=os.path.join(os.getcwd(), 'log.txt'), level=log.INFO) self.server_addresses = server_addresses self.namespace = namespace self.username = username self.password = password # no auth mode # client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE) # auth mode client = nacos.NacosClient(server_addresses, namespace=namespace, username=username, password=password) # 调试模式 # client.set_debugging() # 监听回调函数线程数 client.set_options(callback_thread_num=1) self.client = client sys.path.append( os.path.abspath(os.path.dirname(__file__) + '/' + '..')) sys.path.append("..")
from __future__ import print_function import unittest import nacos from nacos import files import time import shutil SERVER_1 = "100.69.207.65" SERVER_ADDRESSES = "%s:8848, 100.69.207.66:8848" % SERVER_1 NAMESPACE = "6cface1f-2f1b-4744-a59d-fd818b91a799" # Set the following values if authentication mode is enabled on the server USERNAME = None PASSWORD = None client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, username=USERNAME, password=PASSWORD) class TestClient(unittest.TestCase): def test_get_server(self): self.assertEqual(client.get_server(), (SERVER_1, 8848)) def test_set_get_remove_config(self): d = "test" g = "DEFAULT_GROUP" content = u"test中文" self.assertEqual(client.publish_config(d, g, content), True) time.sleep(0.5) self.assertEqual(client.get_config(d, g), content) self.assertEqual(client.remove_config(d, g), True) time.sleep(0.5)
def to_nacos(content, app_name="test", namespace="dev", base_url=NACOS_URL): """ TODO 删除操作 """ client = nacos.NacosClient(base_url, namespace=NAMESPACE.get(namespace)) return client.publish_config(app_name, '', content=content)
from loguru import logger import nacos import settings if __name__ == "__main__": # choose one from "ci", "testonline", "predeploy", "production" stage = "ci" debug = True snapshot_base = os.path.join(settings.JMETER_HOME, "bin") logger.info(f"snapshot base is set to {snapshot_base}") # get summary configs for specific stage nacos_client = nacos.NacosClient(settings.NACOS_SERVER_HOST_CI, namespace=settings.SUMMARY_NAMESPACE_ID) nacos_client.set_options(snapshot_base=snapshot_base) summary_group = settings.SUMMARY_GROUP_DEBUG if debug else settings.SUMMARY_GROUP_STABLE nacos_client.get_config(stage, summary_group) # rename summary config file os.chdir(snapshot_base) old_file = "+".join([stage, summary_group, settings.SUMMARY_NAMESPACE_ID]) new_file = f"{stage}.properties" # check if get config file successfully if os.path.isfile(old_file): # delete if already exist if os.path.isfile(new_file): os.remove(new_file) os.rename(old_file, new_file)
import nacos import settings SERVER_ADDRESSES = f"{settings.NACOS_SERVER_HOST_CI}" NAMESPACE = "env-01" namespace_id = "" data_id = "ci" group = "DEFAULT_GROUP" # no auth mode client = nacos.NacosClient("127.0.0.1", namespace=namespace_id) # client.set_options(no_snapshot=True) # auth mode #client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, username="******", password="******") # get config with open("../snapshot/core400s+DEVICE+env-01", "r") as in_file: s = in_file.read() client.publish_config(data_id, group, content=s)
""" @File : nacos_test.py @Modify Time @Author @Version @Desciption ------------ ------- -------- ----------- 2021/3/12 13:59 SeafyLiang 1.0 flask服务注册到nacos """ from flask import Flask import nacos import time # 定时任务 from apscheduler.schedulers.background import BackgroundScheduler SERVER_ADDRESSES = "192.168.10.35:8848" NAMESPACE = "public" # no auth mode client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE) """初始化flask设置""" app = Flask(__name__) def regis_server_to_nacos(): # auth mode # client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, username="******", password="******") # get config data_id = "nacos.cfg.dataId" group = "test" print(client.get_config(data_id, group)) client.add_naming_instance("algorithm", "192.168.10.35:8848", "5002") def job():
from multiprocessing import Pool import os, time, random import sys sys.path.append("../nacos-jmeter") import time import nacos import settings #SERVER_ADDRESSES = "localhost" SERVER_ADDRESSES = f"{settings.NACOS_SERVER_HOST_CI}" NAMESPACE = "env-01" # no auth mode client = nacos.NacosClient(SERVER_ADDRESSES) # auth mode #client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, username="******", password="******") # get config data_id = "nacos.commit.message" group = "DEFAULT_GROUP" def println(params): print(f"{params['data_id']}hello, properties changes detected.") def long_time_task(name): print(f"Run task {name} ({os.getpid()})") start = time.time() time.sleep(random.random() * 7) end = time.time()
def __init__(self, nacos_host, nacos_port, service_name): self.nacos_host = nacos_host self.nacos_port = nacos_port self.service_name = service_name self.client = nacos.NacosClient(self.nacos_host)
path = environ.Path(__file__) - 2 env = environ.Env() environ.Env.read_env(path('.env')) NACOS = { "Host": env.get_value("nacos_host"), "Port": env.get_value("nacos_port"), "NameSpace": env.get_value("nacos_namespace"), "User": "******", "Password": "******", "DataId": env.get_value("nacos_dataId"), "Group": env.get_value("nacos_group") } client = nacos.NacosClient(f"{NACOS['Host']}:{NACOS['Port']}", namespace=NACOS['NameSpace'], username='******', password='******') data = json.loads(client.get_config(NACOS["DataId"], NACOS["Group"])) mysql_config = data['mysql'] redis_config = data['redis'] DB = ReconnectMySQLDataBase(database=mysql_config['db'], host=mysql_config['host'], port=mysql_config['port'], user=mysql_config['user'], password=mysql_config['password']) pool = redis.ConnectionPool(host=redis_config['host'], port=redis_config['port']) Redis_client = redis.StrictRedis(connection_pool=pool)