异步的consul包装器

aioconsul的Python项目详细描述


AIO领事

Consul有多个组件,但作为一个整体,它是一个在您的基础结构中发现和配置服务的工具,例如:

  • 服务发现
  • 健康检查
  • 密钥/值存储
  • 多数据中心

这个库提供了几个与它的api交互的特性。它构建在asyncioaiohttp的顶部。它与python>;=3.3一起工作,目前仍在进行中。

documentation有更多的细节,但这只是如何使用它的一部分。

安装

pip install aioconsul

使用量

大多数函数都是协程,因此必须将其嵌入到异步任务中:

from aioconsul import Consul
client = Consul()

@asyncio.coroutine
def main():
    node_name = yield from client.agent.config().node_name
    print('I am %s!' % node_name)

loop = asyncio.get_event_loop()
loop.run_until_complete(main)
loop.run_until_complete(future)

代理检查

目前,这个库可以处理简单的检查:

from aioconsul import Consul
client = Consul()

# list all checks
for check in (yield from client.agent.checks()):
    print(check.id)

# look for a check
check = yield from client.agent.checks.get('my-check')

# register a script check
check = yield from client.agent.checks.register_script('my-script-check',
                                                       script='~/script.sh',
                                                       interval='5m')

# register a http check
check = yield from client.agent.checks.register_ttl('my-http-check',
                                                    http='http://example.com',
                                                    interval='10h')

# register a ttl check
check = yield from client.agent.checks.register_ttl('my-ttl-check',
                                                    ttl='10s')

# mark ttl check passing
yield from client.agent.checks.passing(check, note='Make it so')

# deregister any check
yield from client.agent.checks.deregister(check)

代理服务

目前,这个库可以处理简单的检查:

from aioconsul import Consul
client = Consul()

# list all services
for srv in (yield from client.agent.services()):
    print(srv.id)

# search a service by its name
srv = yield from client.agent.services.get('my-service')

# disable a service
yield from client.agent.services.maintenance(srv,
                                             enable=False,
                                             reason='Migrating stuff')

# and reenable it
yield from client.agent.services.maintenance(srv,
                                             enable=True,
                                             reason='Done')

目录

此库可以查阅目录:

from aioconsul import Consul
client = Consul()

# listing all nodes from catalog
for node in (yield from client.catalog.nodes()):
    print(node.name)
    print(node.address)

# getting a node with all of its service
node = yield from client.catalog.get('my-node')
print(node.services)

# getting all nodes that belong to a service
nodes = yield from client.catalog.nodes(service='my-service')
print(nodes)

以及注册支票、服务和节点:

from aioconsul import Consul
client = Consul()

node = {'name': 'my-local-node',
        'address': '127.0.0.1'}
check = {'name': 'baz',
         'state': 'passing',
         'service_id': 'bar'}
service={'name': 'bar'}

resp = yield from client.catalog.register(node, check=check, service=service)
assert resp

resp = yield from client.catalog.deregister(node, check=check, service=service)
assert resp

事件

from aioconsul import Consul
client = Consul()

# send an event
event = yield from client.event.fire('my-event', node_filter='.*')

# list all events
for event in (yield from client.event.items()):
    print(event.name)

健康

from aioconsul import Consul
client = Consul()

# checks for a node
for check in (yield from client.health.node('my-local-node')):
    assert check.status == 'passing'

# health of a node
for check in (yield from client.health.node('my-local-node')):
    assert check.status == 'passing'

# health of a check id
for check in (yield from client.health.checks('serfHealth')):
    assert check.status == 'passing'

# health of a check id
for check in (yield from client.health.checks('serfHealth')):
    assert check.status == 'passing'

# health of a service
for node in (yield from client.health.service('foo', state='any')):
    for check in node.checks:
        if check.id == 'service:foo':
            assert check.status == 'passing'

# passing checks
for check in (yield from client.health.state('passing')):
    assert check.status == 'passing'

Kv和会话

简单示例:

from aioconsul import Consul
client = Consul()

# set a k/v
yield from client.kv.set('my.key', 'my.value')

# fetch a k/v
obj = yield from client.kv.get('my.key')

# fetched values have a special attribute `consul`
assert obj.key.name == 'my.key'

# delete a k/v
yield from client.kv.delete('my.key')

许多k/v:

# list many k/v
for key, value in (yield from client.kv.items('')):
    print(key, value)

短暂的k/v:

session = yield from client.sessions.create(behavior='delete')
yield from client.kv.create('my.key', 'my.key')
yield from client.sessions.delete(session)

try:
    # try to fetch previous k/v
    obj = yield from client.kv.get('my.key')
except client.kv.NotFound:
    # but it was destroyed with the session
    pass

前交叉韧带

from aioconsul import Consul, PermissionDenied
client = Consul(token=master_token)

# create a token
token = (yield from client.acl.create('my-acl', rules=[
    ('key', '', 'read'),
    ('key', 'foo/', 'deny'),
]))

# access to kv with the fresh token
node = Consul(token=token)
yield from node.kv.get('foo')
with pytest.raises(PermissionDenied):
    yield from node.kv.set('foo', 'baz')
with pytest.raises(node.kv.NotFound):
    yield from node.kv.get('foo/bar')

测试

测试依赖于Consul二进制和py.test

  1. 安装consun binary,它必须可以在$PATH中访问。

  2. 安装测试要求:

    pip install -r requirements-tests.txt
    
  3. 然后运行测试:

    py.test --cov-report html --cov aioconsul tests
    

发布到pypi

python setup.py sdist bdist_wheel upload

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
在扩展类中将Java重写为抽象的非抽象方法   java可滚动视图   java Android:隐藏操作栏,保持标签   java如何在Hibernate中使用@Qualifier   java如何在spring MVC中进行Http会话管理以获取数据库中的数据   java如何为TictaToe游戏创建HashMap   java在消息资源文件中查找未使用的值   从源代码构建Kafka时发生java错误   c中的java调用optaplanner DLL#   无法通过Java API访问orientdb函数   任务的java执行失败“:app:ProcessDebuggGoogleService”   java在整个模拟过程中保持代理之间的距离不变   如何在Java中使用BouncyCastle PGP实用程序实现增量加密?   java在安卓中计算画布点的距离   Java回文修订   java在Firebase数据库中存储变量的必要性   java如何使用gquery手势插件在页面上启用文本突出显示?   java如何在Apache camel中使用POST调用REST?