Etcd watch mechanism

First, start etcd with docker

docker run -d\
  -p 2379:2379 \
  -p 2380:2380 \
  --name etcd quay.io/coreos/etcd:latest \
  /usr/local/bin/etcd \
  --data-dir=/etcd-data --name node1 --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://0.0.0.0:2379 --listen-peer-urls=http://0.0.0.0:2380 --initial-advertise-peer-urls=http://0.0.0.0:2380

We add a key value pair to etcd

curl http://127.0.0.1:2379/v2/keys/key1 -XPUT -d value="Hello world"

Etcd’s watch monitoring can be divided into two types: one-time monitoring and continuous monitoring

One time monitoring

If an event is listened to, JSON data will be returned. After returning, the listener exits, and the listener needs to be started again later. Recursive is to listen for changes of all child nodes.

curl http://127.0.0.1:2379/v2/keys/key1?wait=true&recursive=true

Then let’s change the value of key1

curl http://127.0.0.1:2379/v2/keys/key1 -XPUT -d value="changed value"

When the value of key1 changes, you can see that the previous wait process is over and listen for the return

Permanent monitoring. If an event is monitored, it will not exit and continue to return data. It is more reliable than one-time monitoring

curl http://127.0.0.1:2379/v2/keys/key1\?wait\=true\&recursive\=true\&stream\=true
Send a Message