Top distributed open source project, coupled with this visualization tool, really fragrant!

As a top distributed open source project, ZooKeeper is widely used. Dubbo and Kafka are well-known open source projects. I've only heard of it before and haven't studied it carefully. Today, I'll take you to learn about ZooKeeper, mainly from three aspects: ZooKeeper installation, visualization tools and application. I hope it will be helpful to you!

SpringBoot e-commerce project mall (40k+star) address: https://github.com/macrozheng/mall

brief introduction

ZooKeeper is a distributed coordination framework, which can provide consistency services for distributed systems. ZooKeeper was originally developed by Yahoo and later donated to the Apache foundation. Now it has successfully become the top project of Apache. At present, it has 9.5k+Star on Github.

Distributed coordination

To understand ZooKeeper, we first need to understand what is distributed coordination? Take the example of the registry in Spring Cloud.

There are many services in the microservice (distributed) system, and there are multiple instances of the same service. In the application, we can load balance the call of services through the service name, and these services may hang up or new instances may be added. At this time, we need something to coordinate and save the corresponding relationship between the service name and the call IP of available instance 3. At this time, the registry is a distributed coordinator, and ZooKeeper can be used to act as this coordinator.

install

ZooKeeper installation is very convenient for both Windows and Linux. Let's learn about its installation first.

Windows setup

  • First download the ZooKeeper installation package at: https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz

  • Unzip it to the specified directory. After unzipping, the directory structure is as follows;

  • Create the configuration file zoo. Exe in the conf directory CFG, as follows;
# Set heartbeat time in milliseconds
tickTime=2000
# The folder where the in memory database snapshot is stored
dataDir=I:/developer/env/apache-zookeeper-3.7.0-bin/data
# Listen to the port of the client connection
clientPort=2181
  • Enter the bin directory and start the ZooKeeper service;
zkServer.cmd
  • After the service is started successfully, the console will output the following information.

Linux Installation

  • Using Docker to install ZooKeeper is undoubtedly the most convenient. First, we download its Docker image;
docker pull zookeeper:3.7.0
  • Create the configuration file directory of ZooKeeper and switch to this directory to create the configuration file zoo cfg;
mkdir /mydata/zookeeper/conf/ -p
cd /mydata/zookeeper/conf/
touch zoo.cfg
  • Profile zoo The contents of CFG are as follows, which can be edited directly by VIM;
# Set heartbeat time in milliseconds
tickTime=2000
# The folder where the in memory database snapshot is stored
dataDir=/tmp/zookeeper
# Listen to the port of the client connection
clientPort=2181
  • Run the ZooKeeper container.
docker run -p 2181:2181 --name zookeeper \
-v /mydata/zookeeper/conf/zoo.cfg:/conf/zoo.cfg \
-d zookeeper:3.7.0

Command line operation

Next, we use the command line to operate ZooKeeper and get familiar with the use of ZooKeeper.

  • First, use zkCli command line tool to connect to ZooKeeper;
zkCli.cmd -server 127.0.0.1:2181
  • You can view the common commands of ZooKeeper through help command;
[zk: 127.0.0.1:2181(CONNECTED) 0] help
ZooKeeper -server host:port -client-configuration properties-file cmd args
        addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
        addauth scheme auth
        close
        config [-c] [-w] [-s]
        connect host:port
        create [-s] [-e] [-c] [-t ttl] path [data] [acl]
        delete [-v version] path
        deleteall path [-b batch size]
        delquota [-n|-b|-N|-B] path
        get [-s] [-w] path
        getAcl [-s] path
        getAllChildrenNumber path
        getEphemerals path
        history
        listquota path
        ls [-s] [-w] [-R] path
        printwatches on|off
        quit
        reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
        redo cmdno
        removewatches path [-c|-d|-a] [-l]
        set [-s] [-v version] path data
        setAcl [-s] [-v version] [-R] path acl
        setquota -n|-b|-N|-B val path
        stat [-w] path
        sync path
        version
        whoami
  • As we all know, Redis stores data in the form of key value, while ZooKeeper stores data in the form of znode value. Znode is a bit like a directory, and / directory is the root directory in ZooKeeper. You can view all znodes through the following commands;
[zk: 127.0.0.1:2181(CONNECTED) 1] ls /
[zookeeper]
  • Create a znode called / zk_test, store the string my_data, which is a bit like Redis;
[zk: 127.0.0.1:2181(CONNECTED) 2] create /zk_test my_data
Created /zk_test
  • Looking at all znode, you can see zk_test this znode;
[zk: 127.0.0.1:2181(CONNECTED) 3] ls /
[zk_test, zookeeper]
  • Obtain the data stored in znode;
[zk: 127.0.0.1:2181(CONNECTED) 4] get /zk_test
my_data
  • Modify the data in znode;
[zk: 127.0.0.1:2181(CONNECTED) 5] set /zk_test test_data
[zk: 127.0.0.1:2181(CONNECTED) 6] get /zk_test
test_data
  • Delete the data in znode;
[zk: 127.0.0.1:2181(CONNECTED) 7] delete /zk_test
[zk: 127.0.0.1:2181(CONNECTED) 8] ls /
[zookeeper]

Visual management

PrettyZoo is a Zookeeper graphical management client based on Apache cursor and JavaFX. High appearance value, recommended.

  • First download the installation package of prettyzoo at: https://github.com/vran-dev/PrettyZoo/releases

  • We need to create a connection to ZooKeeper. We can find that PrettyZoo supports connection through SSH channel;

  • Double click the connection to view the data stored in ZooKeeper. It is clear that ZooKeeper stores data according to the directory structure;

  • Right click the directory. We can create and delete znode. With this tool, we can basically say goodbye to the command line operation;

  • If you still think the command line is cool, PrettyZoo also implements the command line function. Open the command line tab and you can knock the command happily.

Node type

A node (znode) in ZooKeeper has a life cycle, which depends on the type of node. There are four types:

  • Persistent node: the default node type. After a node is created, it will always exist.
  • Persistent Sequential node: it has the feature of persistent node. The node name will be followed by a self increasing number suffix.
  • Ephemeral: it exists temporarily. When the session creating the node is closed, the node is deleted.
  • Ephemeral Sequential: it has the characteristics of temporary node. The suffix of self increasing number will be added after the node name.

If you use the command line to create nodes, the sequence feature corresponds to the - s option and the temporary feature corresponds to the - e option, such as the following command:

# Create persistent order node
create -s /test/seq segText
# Create temporary node
create -e /test/tmp tmpText
# Create temporary sequence node
create -s -e /test/seqTmp setTmpText

After successful creation, the following is displayed:

If you use PrettyZoo to create, just check an option.

Use as a registry

CAP is an important theory in distributed architecture, which includes consistency, availability and partition tolerance. We often use Eureka to support AP, while ZooKeeper supports CP. Next, let's learn about the application of ZooKeeper as a registry in Spring Cloud.

  • ZooKeeper is used as a registry. Its usage is basically the same as Eureka and Consul. First, we need to use it in POM Add the service discovery component of ZooKeeper in XML;
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
  • Then modify the configuration file application YML, add ZooKeeper related configuration;
spring:
  cloud:
    zookeeper:
      # zookeeper connection address
      connect-string: localhost:2181
      discovery:
        # Register as a service
        register: true
        # Use IP address instead of hostname when registering
        prefer-ip-address: true
  • Here, we still use the example in the Spring Cloud learning tutorial. There are two services, zookeeper Ribbon service and zookeeper user service. The former calls the latter remotely through the Ribbon;

  • Start the two services respectively. Through PrettyZoo, we can find that when ZooKeeper is used as the registration center, the name, IP and port of the registration service are stored in it;

  • We called the interface test in zookeeper ribbon service and found that it can be accessed normally. The interface address is: http://localhost:8301/user/1

  • If we turn off the ZooKeeper user service at this time, we can find that ZooKeeper will automatically delete the stored data;

  • It can be seen that ZooKeeper, as the registration center of microservices, is realized through temporary nodes. When the service goes online, it will be registered with ZooKeeper and deleted by ZooKeeper when the service goes offline, ensuring the high availability of microservices.

summary

Today, we learned about the installation of ZooKeeper, the use of the visualization tool PrettyZoo, and the application of ZooKeeper as a registry in Spring Cloud. In fact, ZooKeeper has many applications in distributed systems, such as making distributed locks, realizing master selection function, and replacing UUID to generate unique ID. if you are interested, you can study it in depth!

reference material

Official documents: https://zookeeper.apache.org/doc/current/zookeeperStarted.html

Project source code address

https://github.com/macrozheng/springcloud-learning

This article GitHub https://github.com/macrozheng/mall-learning Already included, welcome to Star!

Keywords: Java Zookeeper Back-end

Added by irken on Tue, 11 Jan 2022 05:50:34 +0200