- python-consul
import consul
c = consul.Consul(host="192.168.111.111")
c.kv.put("key","value")
a = c.kv.get("key")
print a
('107', {u'LockIndex': 0, u'ModifyIndex': 107, u'Value': 'value', u'Flags': 0, u'Key': u'key', u'CreateIndex': 107})
a = c.session.create(name="mhc",behavior='release',ttl=25)
c.kv.put("key111", "v2222", acquire=a)
b = c.kv.get("key111")[1]
print b
{u'LockIndex': 1, u'ModifyIndex': 332, u'Value': 'v2222', u'Session': u'63d76352-f2ee-8cf0-8c01-b91581d22c1a', u'Flags': 0, u'Key': u'key111', u'CreateIndex': 332}
a = c.session.create(name="mhc",behavior='release',ttl=25)
c.kv.put("key111", "v2222", acquire=a)
time.sleep(2)
print c.kv.put("key111", "done", release=a)
b = c.kv.get("key111")[1]
print b
True
{u'LockIndex': 3, u'ModifyIndex': 368, u'Value': 'done', u'Flags': 0, u'Key': u'key111', u'CreateIndex': 332}
------------------------------------------------------------------------------------------------------
watch
w.py
#!/bin/python
import sys
import json
if __name__ == "__main__":
print "1111"
a = sys.stdin.readline().strip()
a = json.loads(a)
print a
print a["Value"]
-------------------------------------
[root@localmesos ~]# consul watch -type=key -key=key /root/PycharmProjects/test/w.py
1111
{"Key":"key","CreateIndex":11,"ModifyIndex":80,"LockIndex":0,"Flags":0,"Value":"MzMz","Session":""}
1111
{"Key":"key","CreateIndex":11,"ModifyIndex":98,"LockIndex":0,"Flags":0,"Value":"MjIy","Session":""}
consul watch -type=service -service=consul /root/PycharmProjects/test/w.py
1111
[{u'Node': {u'Node': u'localmesos', u'Datacenter': u'dc1', u'TaggedAddresses': {u'wan': u'192.168.111.111', u'lan': u'192.168.111.111'}, u'ModifyIndex': 6, u'Meta': {}, u'Address': u'192.168.111.111', u'CreateIndex': 5, u'ID': u'cbefa43c-4343-8ee7-dee1-4944e230a1b1'}, u'Checks': [{u'Node': u'localmesos', u'CheckID': u'serfHealth', u'Name': u'Serf Health Status', u'ServiceName': u'', u'Notes': u'', u'Status': u'passing', u'ServiceID': u'', u'ServiceTags': [], u'Output': u'Agent alive and reachable'}], u'Service': {u'Service': u'consul', u'Tags': [], u'ModifyIndex': 6, u'EnableTagOverride': False, u'ID': u'consul', u'Address': u'', u'CreateIndex': 5, u'Port': 8300}}]
------------------------------------------------------------------------------
consul agent -data-dir=/consul/data -config-dir=/consul/config -server -client=0.0.0.0 -bootstrap -ui
consul agent -data-dir=/consul/data2 -config-dir=/consul/config2 -rejoin -retry-join 109.105.4.65 -retry-max 10 -retry-interval 10s
consul agent -data-dir=/consul/data2 -config-dir=/consul/config2 -rejoin -retry-join 109.105.4.65:8301 -retry-max 10 -retry-interval 10s -config-file=/consul/config2/default.json -node-id=f4dc3632-51eb-c95b-af68-0aff63567ebf
docker 启动consul
docker run -d -h master -v /mnt:/data -p $ip:8300:8300 -p $ip:8301:8301 -p $ip:8301:8301/udp -p $ip:8302:8302 -p $ip:8302:8302/udp -p $ip:8400:8400 -p 8500:8500 consul:0.7.1 agent -server -client=0.0.0.0 -bootstrap -ui
本地 consul
consul agent -server -client=0.0.0.0 -bootstrap -ui -data-dir=/tmp/consul -advertise=192.168.111.111