• python-etcd


     Client 对象

    ['_MDELETE', '_MGET', '_MPOST', '_MPUT', '__class__', '__contains__', '__del__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_allow_reconnect', '_allow_redirect', '_base_uri', '_check_cluster_id', '_comparison_conditions', '_del_conditions', '_discover', '_get_headers', '_handle_server_response', '_machines_cache', '_next_server', '_protocol', '_read_options', '_read_timeout', '_result_from_response', '_sanitize_key', '_stats', '_use_proxies', '_wrap_request', 'allow_redirect', 'api_execute', 'api_execute_json', 'base_uri', 'delete', 'election', 'eternal_watch', 'expected_cluster_id', 'get', 'get_lock', 'host', 'http', 'key_endpoint', 'leader', 'leader_stats', 'machines', 'members', 'password', 'pop', 'port', 'protocol', 'read', 'read_timeout', 'set', 'stats', 'store_stats', 'test_and_set', 'update', 'username', 'version_prefix', 'watch', 'write']

         类属性

        http

        

      实例属性

        allow_redirect

        base_uri

        election              # Election primitives were removed from etcd 2.0

        expected_cluster_id

        host

        key_endpoint

        leader

        leader_stats

        machines

        members

        port

        protocol

        read

        read_timeout

        stats

        store_stats        

        version_prefix

      instancemethod 实例方法

        api_execute

        api_execute_json

        delete

        eternal_watch

        get

        get_lock

        pop

        set    

        test_and_set

        update

        watch

        write

     实例化对象

       函数原型:

        def __init__(
                self,
                host='127.0.0.1',         # mixed:类型-字符串(IP地址),元组((host,port),(host.port),...)  支持多个etcd服务端的连接。
                port=4001,            # 
                srv_domain=None,
                version_prefix='/v2',
                read_timeout=60,
                allow_redirect=True,
                protocol='http',
                cert=None,
                ca_cert=None,
                username=None,
                password=None,
                allow_reconnect=False,
                use_proxies=False,
                expected_cluster_id=None,
                per_host_pool_size=10
        ):

     实例: 

    import etcd
    
    # client
    = etcd.Client() # this will create a client against etcd server running on localhost on port 4001 client = etcd.Client(port=4002) client = etcd.Client(host='127.0.0.1', port=4003) client = etcd.Client(host='127.0.0.1', port=4003, allow_redirect=False) # wont let you run sensitive commands on non-leader machines, default is true client = etcd.Client( host='127.0.0.1', port=4003, allow_reconnect=True, protocol='https',)

     

    全局网络锁

     1 client = etcd.Client()
     2 lock = client.get_lock('/customer1', ttl=60)
     3  
     4 # Use the lock object:
     5 lock.acquire()
     6 lock.is_locked()  # True
     7 lock.renew(60)
     8 lock.release()
     9 lock.is_locked()  # False
    10  
    11 # The lock object may also be used as a context manager:
    12 client = etcd.Client()
    13 lock = client.get_lock('/customer1', ttl=60)
    14 with lock as my_lock:
    15     do_stuff()
    16     lock.is_locked()  # True
    17     lock.renew(60)
    18 lock.is_locked()  # False
     1 # 创建etcd客户端对象,
     2 # allow_redirect=True  这个参数是当断链的时候,etcd会再次复用connect创建可用的连接
     3 client = etcd.Client(
     4              host='127.0.0.1',
     5              port=4003,
     6              allow_reconnect=True,
     7              protocol='https',)
     8 
     9 client.write('/nodes/n1', 1)
    10
    10 # with ttl 设置几秒,几秒后这数据就没了 11 client.write('/nodes/n2', 2, ttl=4)   # sets the ttl to 4 seconds 12
    12 # create only 13 client.write('/nodes/n3', 'test', prevExist=False) prevExist 如果数据有的话,就不再插入了。 14 15 client.write('/nodes/n3', 'test2', prevValue='test1') 保证value以前是test1 16 17 # mkdir 18 client.write('/nodes/queue', dir=True) 新版本不用这个也行,最少我不用指明dir 也是可以创建mkdir的 19
    19 # Append a value to a queue dir 20 client.write('/nodes/queue', 'test', append=True) #will write i.e. /nodes/queue/11 21 client.write('/nodes/queue', 'test2', append=True) #will write i.e. /nodes/queue/12

     对于数据的修改也可以用 update

    1 result = client.read('/foo')
    2 print(result.value) # bar
    3 result.value += u'bar'
    4 updated = client.update(result)  
    5 print(updated.value)

    用read()方法 从etcd获取节点数据

    client.read('/nodes/n2').value
     
    #recursive递归的目录  
    #sorted 排序
    r = client.read('/nodes', recursive=True, sorted=True)
    for child in r.children:
        print("%s: %s" % (child.key,child.value))
     
    #相当于zookeeper的watch监听
    client.read('/nodes/n2', wait=True) #Waits for a change in value in the key before returning.
    client.read('/nodes/n2', wait=True, waitIndex=10)
  • 相关阅读:
    OpenFileDialog 类的ShowDialog() 错误的解决
    DataTable.Select()使用心得
    postgis 自相交数据检测 修复
    postgresql 添加列主键自增
    postgresql 死锁处理
    linux 安装中文字体(生成图片中文乱码解决)
    Dockerfile 部署jar
    SpringBoot学习总结文章
    怎样在Linux服务器上部署自己的项目?(超详细)
    关于连接Redis时Could not get a resource from the pool问题
  • 原文地址:https://www.cnblogs.com/doscho/p/6222994.html
Copyright © 2020-2023  润新知