异步的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在gnu-trove库中是否有任何有序映射?   java Servlet应该有映射,但找不到Servlet   java时间和第二期显示1:10,而不是13:10   java Play Framework 1.2.7 Heroku更新崩溃   线程“main”java中的opencsv异常。lang.NoClassDefFoundError:org/apache/commons/lang3/ObjectUtils   selenium在java中隐藏警告消息   java使用ID引用将JSON实体反序列化为POJO   java无法在JRE 8中加载字体   一个线程中的异常/错误会使整个应用程序停止吗?   java访问重复子规则的元素标签;e、 g.用ANTLR解析(1,2,3)中的a   java如何从平移旋转中找到新坐标   使用HTML Java小程序托管jar文件存在安全问题   java如何按频率而不是字母顺序排列字符串数组   java清除bufferedReader和块以获得更多输入   java解密SAML2断言