zookeeper
Service Discovery Via Zookeeper#
Zookeeper service discovery needs to rely on the apisix-seed project.
How apisix-seed Works#
Click to Preview
apisix-seed completes data exchange by watching the changes of etcd and zookeeper at the same time.
The process is as follows:
APISIXregisters an upstream and specifies the service discovery type aszookeepertoetcd.apisix-seedwatches the resource changes ofAPISIXinetcdand filters the discovery type and obtains the service name.apisix-seedbinds the service to theetcdresource and starts watching the service in zookeeper.- The client registers the service with
zookeeper. apisix-seedgets the service changes inzookeeper.apisix-seedqueries the boundetcdresource information through the service name, and writes the updated service node toetcd.- The
APISIXworker watchesetcdchanges and refreshes the service node information to the memory.
Setting apisix-seed and Zookeeper#
The configuration steps are as follows:
- Start the Zookeeper service
docker run -itd --rm --name=dev-zookeeper -p 2181:2181 zookeeper:3.7.0- Download and compile the
apisix-seedproject.
git clone https://github.com/api7/apisix-seed.gitcd apisix-seedgo build- Modify the
apisix-seedconfiguration file, config pathconf/conf.yaml.
etcd: # APISIX ETCD Configure host: - "http://127.0.0.1:2379" prefix: /apisix timeout: 30
discovery: zookeeper: # Zookeeper Service Discovery hosts: - "127.0.0.1:2181" # Zookeeper service address prefix: /zookeeper weight: 100 # default weight for node timeout: 10 # default 10s- Start
apisix-seedto monitor service changes
./apisix-seedSetting APISIX Route and Upstream#
Set a route, the request path is /zk/*, the upstream uses zookeeper as service discovery, and the service name
is APISIX-ZK.
curl http://127.0.0.1:9080/apisix/admin/routes/1 \-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '{ "uri": "/zk/*", "upstream": { "service_name": "APISIX-ZK", "type": "roundrobin", "discovery_type": "zookeeper" }}'Register Service and verify Request#
- Service registration using Zookeeper CLI
- Register Service
# Login Containerdocker exec -it ${CONTAINERID} /bin/bash# Login Zookeeper Clientoot@ae2f093337c1:/apache-zookeeper-3.7.0-bin# ./bin/zkCli.sh# Register Service[zk: localhost:2181(CONNECTED) 0] create /zookeeper/APISIX-ZK '{"host":"127.0.0.1:1980","weight":100}'- Successful Response
Created /zookeeper/APISIX-ZK- Verify Request
- Request
curl -i http://127.0.0.1:9080/zk/hello- Response
HTTP/1.1 200 OKConnection: keep-aliveContent-Type: text/html; charset=utf-8Date: Tue, 29 Mar 2022 08:51:28 GMTServer: APISIX/2.12.0Transfer-Encoding: chunked
hello