Package: nacos-newsdk-python
Nacos-Newsdk-Python更新了鉴权问题。see:https://nacos.io/zh-cn/docs/open-API.htmlSupported Python version：Python 2.7
Python 3.6
Python 3.7Supported Nacos versionNacos 0.8.0 ~ 1.3.2Installationpipinstallnacos-newsdk-pythonGetting Startedimportnacos# Both HTTP/HTTPS protocols are supported, if not set protocol prefix default is HTTP, and HTTPS with no ssl check(verify=False)# "192.168.3.4:8848" or "https://192.168.3.4:443" or "http://192.168.3.4:8848,192.168.3.5:8848" or "https://192.168.3.4:443,https://192.168.3.5:443"SERVER_ADDRESSES="server addresses split by comma"NAMESPACE="namespace id"# no auth modeclient=nacos.NacosClient(SERVER_ADDRESSES,namespace=NAMESPACE)# auth mode#client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, ak="{ak}", sk="{sk}")# get configdata_id="config.nacos"group="group"print(client.get_config(data_id,group))Configurationclient = NacosClient(server_addresses, namespace=your_ns, ak=your_ak, sk=your_sk)server_addresses-required- Nacos server address, comma separated if more than 1.namespace- Namespace. | default:Noneak- The accessKey to authenticate. | default: nullsk- The secretKey to authentication. | default: nullExtra OptionsExtra option can be set byset_options, as following:client.set_options({key}={value})
# client.set_options(proxies={"http":"192.168.3.50:809"})Configurable options are:default_timeout- Default timeout for get config from server in seconds.pulling_timeout- Long polling timeout in seconds.pulling_config_size- Max config items number listened by one polling process.callback_thread_num- Concurrency for invoking callback.failover_base- Dir to store failover config files.snapshot_base- Dir to store snapshot config files.no_snapshot- To disable default snapshot behavior, this can be overridden by paramno_snapshotingetmethod.proxies- Dict proxy mapping, some environments require proxy access, so you can set this parameter, this way http requests go through the proxy.API ReferenceGet ConfigNacosClient.get_config(data_id, group, timeout, no_snapshot)paramdata_idData id.paramgroupGroup, useDEFAULT_GROUPif no group specified.paramtimeoutTimeout for requesting server in seconds.paramno_snapshotWhether to use local snapshot while server is unavailable.returnW
Get value of one config item following priority:Step 1 - Get from local failover dir(default:${cwd}/nacos-data/data).Failover dir can be manually copied from snapshot dir(default:${cwd}/nacos-data/snapshot) in advance.This helps to suppress the effect of known server failure.Step 2 - Get from one server until value is got or all servers tried.Content will be save to snapshot dir after got from server.Step 3 - Get from snapshot dir.Add WatchersNacosClient.add_config_watchers(data_id, group, cb_list)paramdata_idData id.paramgroupGroup, useDEFAULT_GROUPif no group specified.paramcb_listList of callback functions to add.returnAdd watchers to a specified config item.Once changes or deletion of the item happened, callback functions will be invoked.If the item is already exists in server, callback functions will be invoked for once.Multiple callbacks on one item is allowed and all callback functions are invoked concurrently bythreading.Thread.Callback functions are invoked from current process.Remove WatcherNacosClient.remove_config_watcher(data_id, group, cb, remove_all)paramdata_idData id.paramgroupGroup, use "DEFAULT_GROUP" if no group specified.paramcbCallback function to delete.paramremove_allWhether to remove all occurrence of the callback or just once.returnRemove watcher from specified key.Publish ConfigNacosClient.publish_config(data_id, group, content, timeout)paramdata_idData id.paramgroupGroup, use "DEFAULT_GROUP" if no group specified.paramcontentConfig value.paramtimeoutTimeout for requesting server in seconds.returnTrue if success or an exception will be raised.Publish one data item to Nacos.If the data key is not exist, create one first.If the data key is exist, update to the content specified.Content can not be set to None, if there is need to delete config item, use functionremoveinstead.Remove ConfigNacosClient.remove_config(data_id, group, timeout)paramdata_idData id.paramgroupGroup, use "DEFAULT_GROUP" if no group specified.paramtimeoutTimeout for requesting server in seconds.returnTrue if success or an exception will be raised.Remove one data item from Nacos.Register InstanceNacosClient.add_naming_instance(service_name, ip, port, cluster_name, weight, metadata, enable, healthy)paramservice_namerequiredService name to register to.paramiprequiredIP of the instance.paramportrequiredPort of the instance.paramcluster_nameCluster to register to.paramweightA float number for load balancing weight.parammetadataExtra info in JSON string format or dict formatparamenableA bool value to determine whether instance is enabled or not.paramhealthyA bool value to determine whether instance is healthy or not.paramephemeralA bool value to determine whether instance is ephemeral or not.returnTrue if success or an exception will be raised.Deregister InstanceNacosClient.remove_naming_instance(service_name, ip, port, cluster_name)paramservice_namerequiredService name to deregister from.paramiprequiredIP of the instance.paramportrequiredPort of the instance.paramcluster_nameCluster to deregister from.paramephemeralA bool value to determine whether instance is ephemeral or not.returnTrue if success or an exception will be raised.Modify InstanceNacosClient.modify_naming_instance(service_name, ip, port, cluster_name, weight, metadata, enable)paramservice_namerequiredService name.paramiprequiredIP of the instance.paramportrequiredPort of the instance.paramcluster_nameCluster name.paramweightA float number for load balancing weight.parammetadataExtra info in JSON string format or dict format.paramenableA bool value to determine whether instance is enabled or not.paramephemeralA bool value to determine whether instance is ephemeral or not.returnTrue if success or an exception will be raised.Query InstancesNacosClient.list_naming_instance(service_name, clusters, namespace_id, group_name, healthy_only)paramservice_namerequiredService name to query.paramclustersCluster names separated by comma.paramnamespace_idCustomized group name, defaultblank.paramgroup_nameCustomized group name , defaultDEFAULT_GROUP.paramhealthy_onlyA bool value for querying healthy instances or not.returnInstance info list if success or an exception will be raised.Query Instance DetailNacosClient.get_naming_instance(service_name, ip, port, cluster_name)paramservice_namerequiredService name.paramiprequiredIP of the instance.paramportrequiredPort of the instance.paramcluster_nameCluster name.returnInstance info if success or an exception will be raised.Send Instance BeatNacosClient.send_heartbeat(service_name, ip, port, cluster_name, weight, metadata)paramservice_namerequiredService name.paramiprequiredIP of the instance.paramportrequiredPort of the instance.paramcluster_nameCluster to register to.paramweightA float number for load balancing weight.paramephemeralA bool value to determine whether instance is ephemeral or not.parammetadataExtra info in JSON string format or dict format.returnA JSON object include server recommended beat interval if success or an exception will be raised.Subscribe Service Instances ChangedNacosClient.subscribe(listener_fn, listener_interval=7, *args, **kwargs)paramlistener_fnrequiredCustomized listener function.paramlistener_intervalListen interval , default 7 second.paramservice_namerequiredService name which subscribes.paramclustersCluster names separated by comma.paramnamespace_idCustomized group name, defaultblank.paramgroup_nameCustomized group name , defaultDEFAULT_GROUP.paramhealthy_onlyA bool value for querying healthy instances or not.returnUnsubscribe Service Instances ChangedNacosClient.unsubscribe(service_name, listener_name)paramservice_namerequiredService name to subscribed.paramlistener_namelistener_name which is customized.returnStop All Service SubscribeNacosClient.stop_subscribe()returnDebugging ModeDebugging mode if useful for getting more detailed log on console.Debugging mode can be set by:NacosClient.set_debugging()
# only effective within the current process