This article is more than one year old. Older articles may contain outdated content. Check that the information in the page has not become incorrect since its publication.
In the mordern distrbuted applications, there are multiple coordination problems between nodes and nodes, including: leader election, group service, locking, configuration management, naming and synchronization. Apache Zookeeper, as its name implied, is a distributed, open-source coordination service framwork to address these demand.
In order to ensure the high performance, highly available and strictly ordered access, the performance aspects of ZooKeeper means it can be used in large, distributed systems and can also be deployed in cluster mode, which called ‘ZooKeeper ensemble’. In ZooKeeper ensemble, all write requests from clients are forwarded to a single server, called the leader, through the ZAB(Zookeeper Atomic Broadcast Protocol) to make sure the message in each nodes are same. Clients can access any one of the clusters to read and write data without worrying about inconsistencies in the data.
Image Credit : ebook -Zookeeper-Distributed Process Coordination from O’Reilly
The method to store the data in Zookeeper is similar as the standard UNIX file system, as a data model styled after the familiar directory tree structure of file systems. When we talking about ZooKeeper data nodes, we call it Znodes to clarify it.
Image Credit : ebook -Zookeeper-Distributed Process Coordination from O’Reilly
You could donwload and install Zookeeper directly1.
Or you could use Homebrew 2 brew install zookeeper
to install Zookeeper in Mac OS.
Considering the versatility, we run the Zookeeper by using docker in this blog. If you have not installed the docker yet, please prepare the docker environment first. 3
Execute the command to run zookeeper in a docker container
In the bin
directory, there is a command to start zookeeper zkServer
and the Management Console zkCli
Since it was started through docker, the process of Zookeeper has been started and will provide the services to the public via port 2181.
So, it allows you to access Zookeeper’s console directly through zkCli
for management.
Create /hello-zone
node:
List the child nodes under /
and confirm that hello-zone
is created:
List the child nodes for /hello-zone
and verify that it is empty:
Get the data stored on the /hello-zone
node:
Zookeeper is used for service registration discovery and configuration management in Dubbo, and the structure of data in Zookeeper is shown in the following figure:
First, all data related to Dubbo is organized under the root node of /dubbo
.
The secondary directory is the service name like com.foo.BarService
.
The three-level directory has two child nodes, providers
and consumers
, representing the supplier and customers of the service.
The URL information for each application instance associated with the service will be recorded by the Level 4 directory. The providers
and consumer
will stored the providers information and the consumers information of the services seperately.
For example, the service provider of com.foo.BarService
will register its URL Information to /dubbo/com.foo.BarService/providers
; Similarly, service consumers will register their information under the corresponding consumer
node. At the same time, consumers will subscribe to the corresponding providers
node to be able to detect the changes of the service provider address list.
The code in this document can be found in https://github.com/dubbo/dubbo-samples/tree/master/3-extensions/registry/dubbo-samples-zookeeper.
Define a simple greetingservice
interface with only one simple method named sayHello
to greet to the caller.
Implement the GreetingService
interface and mark it as a service for Dubbo via @Service
.
Define ProviderConfiguration to assemble Dubbo services.
Dubbo-provider.properties is an external configuration in a spring application, as follows:
Since zookeeper runs in a docker container, please be noted that:
In the main
method, you could provide the Dubbo service by running a Spring Context.
Start the main
method of server, you will get the following output, which represents the success of the server’s startup, and the GreetingService
service is registered on the ZookeeperRegistry:
You could find the registration information of the service provider through the Zookeeper management terminal:
You could find that the Dubbo services just registered its URL address at the providers
node as follows:
dubbo://192.168.99.1:20880/com.alibaba.dubbo.samples.api.GreetingService?anyhost=true&application=demo-provider&dubbo=2.6.2&generic=false&interface=com.alibaba.dubbo.samples.api.GreetingService&methods=sayHello&pid=12938&side=provider×tamp=1533264631849
You could declare the reference service by @Reference, while it will generate a full call. The target address of the service could be queried by the Zookeeper’s provider
node.
Define the ConsumerConfiguration to assemble Dubbo service.
“dubbo-consumer.properties” is a method of external configuration in a Spring application, as follows:
Same as 3. Server: Assembling, You need to modify $DOCKER_HOST defined in dubbo.registry.address according to your own environment. You could find more instructions in step 3.
Run main
to initiate a remote call from a existed service provider. Dubbo first subscribes to the zookeeper service address and then selects one from the list of returned addresses to invoke the client:
The output are as follows:
Description:
consumers
nodeproviders
, configurators
, routers
from Zookeepers. The configurations
is related to the Dubbo configuration, and routers
is related to routing rules. The providers node subscription should be noted. When a new service provider to join, due to the relationship between the subscription, the new address list will be pushed to the subscriber. So service consumers also dynamically perceive changes in address lists.You could find the registration information of the service provider through the Zookeeper management terminal:
You could see that consumers of Dubbo’s servicehas registered its URL address at the consumers
node:
consumer://192.168.99.1/com.alibaba.dubbo.samples.api.GreetingService?application=demo-consumer&category=providers,configurators,routers&default.timeout=3000&dubbo=2.6.2&interface=com.alibaba.dubbo.samples.api.GreetingService&methods=sayHello&pid=82406&side=consumer×tamp=1533274951195
This document focuses on how to use ZooKeeper as a registry in Dubbo. This document also mentioned that the Zookeeper could be a configuration center and a service management in Dubbo. Zookeeper is a single-node, standalone mode. However, developers always bulid a Zookeeper server cluster called * Zookeeper ensemble * in the real world.
Through this document, readers can learn: