支持的Python版本
* Python 2.7
* Python 3.6
* Python 3.7
Nacos服务端版本
Nacos 2.x
安装依赖
shellpip install nacos-sdk-python
开始使用
import nacos
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))
```
配置中心
创建客户端
client = NacosClient(server_addresses, namespace=your_ns, username=name, password=your_pwd)
参数说明
server_addresses* - required - Nacos server address, comma separated if more than 1.
namespace* - Namespace. | default: `None`
username* - The username to authenticate. | default: null
password* - The password to authentication. | default: null
log_level* - Log level. | default: null
log_rotation_backup_count* - The number of log files to keep. | default: `7`
其他参数可以按如下方式设置
client.set_options({key}={value})
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.
proxies* - Dict proxy mapping, some environments require proxy access, so you can set this parameter, this way http requests go through the proxy.
API 参考
查询配置
NacosClient.get_config(data_id, group, timeout, no_snapshot)
`param` *data_id* Data id.
`param` *group* Group, use `DEFAULT_GROUP` if no group specified.
`param` *timeout* Timeout for requesting server in seconds.
`param` *no_snapshot* Whether to use local snapshot while server is unavailable.
`return` W Get value of one config item following priority:
Step 1 - 从本地failover文件读取(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 - 从任意远程服务节点读取 * Content will be save to snapshot dir after got from server;
Step 3 - 从 snapshot dir读取;
增加监听器
NacosClient.add_config_watchers(data_id, group, cb_list)
`param` *data_id* Data id.
`param` *group* Group, use `DEFAULT_GROUP` if no group specified.
`param` *cb_list* List of callback functions to add.
删除监听器
NacosClient.remove_config_watcher(data_id, group, cb, remove_all)
`param` *data_id* Data id.
`param` *group* Group, use "DEFAULT_GROUP" if no group specified.
`param` *cb* Callback function to delete.
`param` *remove_all* Whether to remove all occurrence of the callback or just once.
发布配置
NacosClient.publish_config(data_id, group, content, timeout)
`param` *data_id* Data id.
`param` *group* Group, use "DEFAULT_GROUP" if no group specified.
`param` *content* Config value.
`param` *timeout* Timeout for requesting server in seconds.
`return` True if success or an exception will be raised.
删除配置
NacosClient.remove_config(data_id, group, timeout)
`param` *data_id* Data id.
`param` *group* Group, use "DEFAULT_GROUP" if no group specified.
`param` *timeout* Timeout for requesting server in seconds.
`return` True if success or an exception will be raised.
注册实例
NacosClient.add_naming_instance(service_name, ip, port, cluster_name, weight, metadata, enable, healthy,ephemeral,group_name,heartbeat_interval)
`param` *service_name* required Service name to register to.
`param` *ip* required IP of the instance.
`param` *port* required Port of the instance.
`param` *cluster_name* Cluster to register to.
`param` *weight* A float number for load balancing weight.
`param` *metadata* Extra info in JSON string format or dict format
`param` *enable* A bool value to determine whether instance is enabled or not.
`param` *healthy* A bool value to determine whether instance is healthy or not.
`param` *ephemeral* A bool value to determine whether instance is ephemeral or not.
`param` *heartbeat_interval* Auto daemon heartbeat interval in seconds.
`return` True if success or an exception will be raised.
注销实例
NacosClient.remove_naming_instance(service_name, ip, port, cluster_name)
`param` *service_name* required Service name to deregister from.
`param` *ip* required IP of the instance.
`param` *port* required Port of the instance.
`param` *cluster_name* Cluster to deregister from.
`param` *ephemeral* A bool value to determine whether instance is ephemeral or not.
`return` True if success or an exception will be raised.
更新实例
NacosClient.modify_naming_instance(service_name, ip, port, cluster_name, weight, metadata, enable)
`param` *service_name* required Service name.
`param` *ip* required IP of the instance.
`param` *port* required Port of the instance.
`param` *cluster_name* Cluster name.
`param` *weight* A float number for load balancing weight.
`param` *metadata* Extra info in JSON string format or dict format.
`param` *enable* A bool value to determine whether instance is enabled or not.
`param` *ephemeral* A bool value to determine whether instance is ephemeral or not.
`return` True if success or an exception will be raised.
查询实例列表
NacosClient.list_naming_instance(service_name, clusters, namespace_id, group_name, healthy_only)
`param` *service_name* required Service name to query.
`param` *clusters* Cluster names separated by comma.
`param` *namespace_id* Customized group name, default `blank`.
`param` *group_name* Customized group name , default `DEFAULT_GROUP`.
`param` *healthy_only* A bool value for querying healthy instances or not.
`return` Instance info list if success or an exception will be raised.
查询实例详情
NacosClient.get_naming_instance(service_name, ip, port, cluster_name)
`param` *service_name* required Service name.
`param` *ip* required IP of the instance.
`param` *port* required Port of the instance.
`param` *cluster_name* Cluster name.
`return` Instance info if success or an exception will be raised.
发送实例心跳
NacosClient.send_heartbeat(service_name, ip, port, cluster_name, weight, metadata)
`param` *service_name* required Service name.
`param` *ip* required IP of the instance.
`param` *port* required Port of the instance.
`param` *cluster_name* Cluster to register to.
`param` *weight* A float number for load balancing weight.
`param` *ephemeral* A bool value to determine whether instance is ephemeral or not.
`param` *metadata* Extra info in JSON string format or dict format.
`return` A JSON object include server recommended beat interval if success or an exception will be raised.
订阅服务实例变化
NacosClient.subscribe(listener_fn, listener_interval=7, *args, **kwargs)
`param` *listener_fn* required Customized listener function.
`param` *listener_interval* Listen interval , default 7 second.
`param` *service_name* required Service name which subscribes.
`param` *clusters* Cluster names separated by comma.
`param` *namespace_id* Customized group name, default `blank`.
`param` *group_name* Customized group name , default `DEFAULT_GROUP`.
`param` *healthy_only* A bool value for querying healthy instances or not.
取消订阅
NacosClient.unsubscribe(service_name, listener_name)
`param` *service_name* required Service name to subscribed.
`param` *listener_name* listener_name which is customized.
停止所有监听器
NacosClient.stop_subscribe()