领事馆的客户图书馆
consulate的Python项目详细描述
领事馆是一个python客户端库,是领事馆的一组应用程序 服务发现和配置系统。
安装
领事馆可经由pypi到达 并且可以使用easy_install或pip进行安装:
pip install consulate
如果您需要通过unix套接字与consun通信,则有一个额外的 通过以下方式安装的依赖项:
pip install consulate[unixsocket]
命令行实用程序
领事馆配备了两个命令行实用程序,可以与领事馆一起工作 从管理角度来说更容易。consulate应用程序提供 用于执行的常见任务的cli包装器。
passport应用程序已移动到独立应用程序,并且 可在https://github.com/gmr/passport找到。
领事馆
领事馆应用程序提供用于注册服务的cli接口, 备份和恢复KV数据库的内容,以及获取的操作, 从KV数据库中设置和删除密钥。
usage: consulate [-h][--api-scheme API_SCHEME][--api-host API_HOST][--api-port API_PORT][--datacenter DC][--token TOKEN]{register,deregister,kv,run_once} ... CLI utilities for Consul optional arguments: -h, --help show this help message and exit --api-scheme API_SCHEME The scheme to use for connecting to Consul with --api-host API_HOST The consul host to connect on --api-port API_PORT The consul API port to connect to --datacenter DC The datacenter to specify for the connection --token TOKEN ACL token Commands: {register,deregister,kv,run_once} register Register a service for this node deregister Deregister a service for this node kv Key/Value Database Utilities run_once Lock command If the CONSUL_RPC_ADDR environment variable is set, it will be parsed and used for default values when connecting.
服务注册帮助:
usage: consulate register [-h][-a ADDRESS][-p PORT][-s SERVICE_ID][-t TAGS] name {check,httpcheck,no-check,ttl} ... positional arguments: name The service name optional arguments: -h, --help show this help message and exit -a ADDRESS, --address ADDRESS Specify an address -p PORT, --port PORT Specify a port -s SERVICE_ID, --service-id SERVICE_ID Specify a service ID -t TAGS, --tags TAGS Specify a comma delimited list of tags Service Check Options: {check,httpcheck,no-check,ttl} check Define an external script-based check httpcheck Define an HTTP-based check no-check Do not enable service monitoring ttl Define a duration based TTL check
KV数据库实用程序帮助:
usage: consulate kv [-h]{backup,restore,ls,mkdir,get,set,rm} ... optional arguments: -h, --help show this help message and exit Key/Value Database Utilities: {backup,restore,ls,mkdir,get,set,rm} backup Backup to stdout or a JSON file restore Restore from stdin or a JSON file ls List all of the keys mkdir Create a folder get Get a key from the database set Set a key in the database rm Remove a key from the database
锁定操作帮助:
usage: consulate [-h] run_once [-i INTERVAL] prefix command positional arguments: prefix the name of the lock which will be held in Consul. command the command to run optional arguments: -h, --help show this help message and exit -i, --interval hold the lock for INTERVAL seconds
API使用示例
下面的例子强调了领事馆的用法,而不是文档 整个领事馆api的范围。
使用领事馆和领事馆KV数据库:
consul=consulate.Consul()# Set the key named release_flag to Trueconsul.kv['release_flag']=True# Get the value for the release_flag, if not set, raises AttributeErrortry:should_release_feature=consul.kv['release_flag']exceptAttributeError:should_release_feature=False# Delete the release_flag keydelconsul.kv['release_flag']# Find all keys that start with "fl"consul.kv.find('fl')# Find all keys that start with "feature_flag" terminated by "/" separatorconsul.kv.find('feature_flag',separator='/')# Check to see if a key called "foo" is setif"foo"inconsul.kv:print'Already Set'# Return all of the items in the key/value storeconsul.kv.items()
与领事馆合作。代理api:
consul=consulate.Consul()# Get all of the service checks for the local agentchecks=consul.agent.checks()# Get all of the services registered with the local agentservices=consul.agent.services()# Add a service to the local agentconsul.agent.service.register('redis',port=6379,tags=['master'],ttl='10s')
正在从领事处获取健康信息:
consul=consulate.Session()# Get the health of a individual nodehealth=consul.health.node('my-node')# Get all checks that are criticalchecks=consul.heath.state('critical')
有关更多示例,请查看领事馆文档。