Spring cloud zookeeper actual combat



When building the micro service platform in the bank, theZookeeperAs a service registry.

Just distributedCAPIn terms of the theorem,ZookeeperdoCPEurekadoAP, but it isAPThe scenario is more suitable for the high availability of the service registry, and availability is more important than consistency.

Current domesticZookeeperPopularity is affected byDubboAlibaba has its own service registry to cooperate with the open sourceDubboHowever, the service registry did not followDubboOpen source together, under the constraints of the objective environment,DubboOfficially recommendedZookeeperAs a service registration center, this has affected a large number of enterprises, although later, Alibaba’s official relevant team has published articles that are not recommendedZookeeperHowever, just like rumor and rumor refutation, the scope of rumor dissemination is much higher than that of rumor refutation information.

Why doesn’t Alibaba use zookeeper for service discovery- Alibaba middleware team blog

So, if you don’t have to use itZookeeperIf you want to build a service registry, please watch this article selectively as a pair ofZookeeperJust understand and study.ZookeeperIt is still a great framework, a well deserved distributed top-level project, and unmatched in data consistency.

Open againSpringOn the official website, I can’t help sighing the greatness of the open source community. When I studied microservices last year, onlyEurekaZuulFeignRibbonHystrixAnd several other components have been developed to30Multiple open source components; Today’sSpring Cloud, already supportedZookeeper, becoming the most popular micro service solution in the industry.

Spring cloud zookeeper actual combat


Official definition:

ZooKeeper: A Distributed Coordination Service for Distributed Applications.

Zookeeper: distributed coordination service for distributed applications.


ZookeeperDownload address:Releases – Apache Zookeeper

Choose a stableReleaseVersion download:

Spring cloud zookeeper actual combat

ZookeeperuseJavaWrite, runZookeeperConfiguration required beforeJavaenvironment

defaultconfThere is no configuration file in the directory. This is only for learning and creating fileszoo.cfg, content andzoo_sample.cfgConsistent, use the default configuration.

Spring cloud zookeeper actual combat

Run from the command linebinUnder directoryzkServer.cmdstart-upZookeeperServer (if present)UnixPlease run in environment.shScript, the same below).

Spring cloud zookeeper actual combat

Start the client connection.

functionzkCli.cmdstart-upZookeeperThe client is a command line interface that can operate on server dataRedissimilar.

Spring cloud zookeeper actual combat

data structure

quoteZookeeperA picture on the official website,ZookeeperThe internal data structure of adopts tree structure to/Is the root node, similar toUnixFile system, information can be stored in each node.

Spring cloud zookeeper actual combat

ZookeeperNodes are divided into permanent nodes, sequential nodes and temporary nodes, as shown in the figure below.

Spring cloud zookeeper actual combat

Permanent node: the node always exists. Unless the delete command is explicitly executed, child nodes can be created under the permanent node.

Sequential node: after the node is created,ZookeeperAdd a serial number to the node, as shown in the figurezk-sequence-node0000000008, where0000000008byZookeeperAdditional serial number. The serial number is unique under the same parent node. The format is10Digit, insufficient10Bit use0Make up.

Temporary node: the life cycle of the node depends on the session that created it. When the client disconnects, the node will be deleted automatically. The temporary node is not allowed to have child nodes.

Spring cloud zookeeper actual combat

The temporary node here is very suitable for micro service registration center.

When the microservice starts, it acts as a client toZookeeperRegister and create a temporary node containing the current microservice information. When the microservice goes offline, the temporary node will be automatically deletedZookeeperDeletion ensures the high availability of microservices.

And the client can add listening to the nodewatchThat is, the observer will notify the client when the node is updated, so as to ensure that the service list obtained by the microservice is always up-to-date.

Visualization tools

ZookeeperI won’t explain the basic commands,ZookeeperThe commands are nothing more than adding, deleting, updating and querying nodes. There is no need to study deeply. As a non professional application support personnel, you can simply understand them.

It is recommended to use visualization tools toZookeeperLearning, you can visually operate the nodes without the trouble of issuing commands.

zkui – Github

Spring cloud zookeeper actual combat

actual combat

newly buildmavenProject, add dependency:

    <!--  Zookeeper service discovery component -- >

Add configuration:

    #Application name
    name: sample-application
      #Zookeeper address. It is demonstrated here that only a single zookeeper node is used without cluster deployment
      connect-string: localhost:2181
      #Service discovery
        #Register itself
        register: true
        #Priority registration IP
        prefer-ip-address: true
  port: 9000

Start the application and a lot of information will be printedZookeeperConnection related logs:

Spring cloud zookeeper actual combat

seeZookeeperNode information of the server, inservices/sample-applicationOne is mounted under the nodenamebyUUIDThe node stores the relevant information of the microservice.

Spring cloud zookeeper actual combat


Have time to study together in the futureZookeeperCluster and other mainstream distributed solutions.

Author:Mengyunzhi development team of Hebei University of Technology Xi Shuo Zhang