Python client for Nacos.
Project description
nacos-sdk-python
A Python implementation of Nacos OpenAPI.
see: https://nacos.io/zh-cn/docs/open-API.html
Supported Python version:
Python 2.7 Python 3.6 Python 3.7
Supported Nacos version
Nacos 0.8.0
Installation
pip install nacos-sdk-python
Getting Started
import nacos
SERVER_ADDRESSES = "server addresses split by comma"
NAMESPACE = "***"
client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE)
# get config
data_id = "config.nacos"
group = "group"
print(client.get_config(data_id, group))
Configuration
client = NacosClient(server_addresses, namespace=your_ns)
- server_addresses - required - Nacos server address, comma separated if more than 1.
- namespace - Namespace. | default:
None
Extra Options
Extra option can be set by set_options, as following:
client.set_options({key}={value})
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 param no_snapshot in get method.
API Reference
Get Config
NacosClient.get_config(data_id, group, timeout, no_snapshot)
-
paramdata_id Data id. -
paramgroup Group, useDEFAULT_GROUPif no group specified. -
paramtimeout Timeout for requesting server in seconds. -
paramno_snapshot Whether 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.
- Failover dir can be manually copied from snapshot dir(default:
-
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 Watchers
NacosClient.add_config_watchers(data_id, group, cb_list)
paramdata_id Data id.paramgroup Group, useDEFAULT_GROUPif no group specified.paramcb_list List of callback functions to add.return
Add 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 by
threading.Thread. - Callback functions are invoked from current process.
Remove Watcher
NacosClient.remove_config_watcher(data_id, group, cb, remove_all)
paramdata_id Data id.paramgroup Group, use "DEFAULT_GROUP" if no group specified.paramcb Callback function to delete.paramremove_all Whether to remove all occurrence of the callback or just once.return
Remove watcher from specified key.
Publish Config
NacosClient.publish_config(data_id, group, content, timeout)
paramdata_id Data id.paramgroup Group, use "DEFAULT_GROUP" if no group specified.paramcontent Config value.paramtimeout Timeout 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 function remove instead.
Remove Config
NacosClient.remove_config(data_id, group, timeout)
paramdata_id Data id.paramgroup Group, use "DEFAULT_GROUP" if no group specified.paramtimeout Timeout for requesting server in seconds.returnTrue if success or an exception will be raised.
Remove one data item from Nacos.
Register Instance
NacosClient.add_naming_instance(service_name, ip, port, cluster_name, weight, metadata, enable, healthy)
paramservice_name required Service name to register to.paramip required IP of the instance.paramport required Port of the instance.paramcluster_name Cluster to register to.paramweight A float number for load balancing weight.parammetadata Extra info in JSON string format.paramenable A bool value to determine whether instance is enabled or not.paramhealthy A bool value to determine whether instance is healthy or not.returnTrue if success or an exception will be raised.
Deregister Instance
NacosClient.remove_naming_instance(service_name, ip, port, cluster_name)
paramservice_name required Service name to deregister from.paramip required IP of the instance.paramport required Port of the instance.paramcluster_name Cluster to deregister from.returnTrue if success or an exception will be raised.
Modify Instance
NacosClient.modify_naming_instance(service_name, ip, port, cluster_name, weight, metadata, enable)
paramservice_name required Service name.paramip required IP of the instance.paramport required Port of the instance.paramcluster_name Cluster name.paramweight A float number for load balancing weight.parammetadata Extra info in JSON string format.paramenable A bool value to determine whether instance is enabled or not.returnTrue if success or an exception will be raised.
Query Instances
NacosClient.list_naming_instance(service_name, clusters, healthy_only)
paramservice_name required Service name to query.paramclusters Cluster names separated by comma.paramhealthy_only A bool value for querying healthy instances or not.returnInstance info list if success or an exception will be raised.
Query Instance Detail
NacosClient.get_naming_instance(service_name, ip, port, cluster_name)
paramservice_name required Service name.paramip required IP of the instance.paramport required Port of the instance.paramcluster_name Cluster name.returnInstance info if success or an exception will be raised.
Send Instance Beat
NacosClient.send_heartbeat(service_name, ip, port, cluster_name, weight, metadata)
paramservice_name required Service name.paramip required IP of the instance.paramport required Port of the instance.paramcluster_name Cluster to register to.paramweight A float number for load balancing weight.parammetadata Extra info in JSON string format.returnA JSON object include server recommended beat interval if success or an exception will be raised.
Debugging Mode
Debugging 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nacos-sdk-python-0.1.1.tar.gz.
File metadata
- Download URL: nacos-sdk-python-0.1.1.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24d5ce83b6b10cf523bf0a2956dd533f8777a1dfc205a80e4105ce4b657c67b5
|
|
| MD5 |
6cfddb93b3e7774997dca47c76de2b35
|
|
| BLAKE2b-256 |
7f0d93774753da447f8cae1e722a5ad9e3addd988f89371b7d1853710f3db22c
|
File details
Details for the file nacos_sdk_python-0.1.1-py3-none-any.whl.
File metadata
- Download URL: nacos_sdk_python-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81544c2034f45f6bbd5359b2fa71f0fbca6b43400062f179848dc591fa10d55b
|
|
| MD5 |
cbe6a0c3964a0a0c388c90f942524a1e
|
|
| BLAKE2b-256 |
d343ad53207c5a9f98058d409331e4f7001aab33728b3ac6079a565a560a6aed
|