I. Introduction to Zookeeper
Apache ZooKeeper is an open source distributed application coordination component, which is an important component of Hadoop and Hbase. It provides consistency services for distributed applications, including configuration and maintenance, domain name service, distributed synchronization, and group service.
ZooKeeper’s main role in the development of micro-service projects is to serve as a service registry. We can register the prepared services with ZooKeeper.
2. Install ZooKeeper
Environment to prepare
ZooKeeper runs in Java, version 1.8 or higher (JDK 8 LTS, JDK 11 LTS, JDK 12 – Java 9 and 10 not supported)
download
ZooKeeper
- Zookeeper.apache.org/releases.ht…
- Archive.apache.org/dist/zookee…
The installation
Upload the file to the Linux server.
Stand-alone version
Create a directory/unzip
Example Create a ZooKeeper directory.
mkdir -p /usr/local/zookeeper
Copy the code
Unzip the files to this directory.
Tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz -c /usr/local/zookeeper/Copy the code
Create data directories and log directories.
Mkdir -p/usr/local/zookeeper/apache - they are - 3.6.1 track - bin/datamkdir - p / usr/local/zookeeper/apache - they are - 3.6.1 track - bin/logCopy the code
Modifying a Configuration File
# enter configuration file directory CD/usr/local/apache/zookeeper - they are - 3.6.1 track - bin/conf / # zookeeper start the default load called zoo. CFG configuration file, CFG cp zoo_sample. CFG zoo. CFG # Modify the configuration file vi zoo.cfgCopy the code
Modify data directory dataDir and log directory dataLogDir. The modification results are as follows:
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, / TMP here is just # example sakes. DataDir = / usr/local/apache/zookeeper - they are - 3.6.1 track - bin/data DataLogDir = / usr/local/apache/zookeeper - they are - 3.6.1 track - bin/log # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 ## Metrics Providers # # https://prometheus.io Metrics Exporter #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider #metricsProvider.httpPort=7000 #metricsProvider.exportJvmInfo=trueCopy the code
Start/close
Start.
CD/usr/local/apache/zookeeper - they are - 3.6.1 track - bin/bin/zkServer. Sh start --------------------------------------------------------------------------------- ZooKeeper JMX enabled by default Using Config: / usr/local/apache/zookeeper - they are - 3.6.1 track - bin/bin /.. /conf/zoo.cfg Starting zookeeper ... STARTEDCopy the code
Shut down.
CD/usr/local/apache/zookeeper - they are - 3.6.1 track - bin/bin/zkServer. Sh stop --------------------------------------------------------------------------------- ZooKeeper JMX enabled by default Using Config: / usr/local/apache/zookeeper - they are - 3.6.1 track - bin/bin /.. /conf/zoo.cfg Stopping zookeeper ... STOPPEDCopy the code
Cluster version
Prepare two more machines, and add them together to form a cluster environment (if the computer can’t run, change to one machine running three processes).
Create a directory/unzip
Example Create a ZooKeeper directory.
mkdir -p /usr/local/zookeeper
Copy the code
Unzip the files to this directory.
Tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz -c /usr/local/zookeeper/Copy the code
Create data directories and log directories.
Mkdir -p/usr/local/zookeeper/apache - they are - 3.6.1 track - bin/datamkdir - p / usr/local/zookeeper/apache - they are - 3.6.1 track - bin/logCopy the code
Myid file
Create a myID file in the data directory, just write a 1 in the file, the other two machines write 2 and 3 respectively.
CD/usr/local/apache/zookeeper - they are - 3.6.1 track - bin/data/vi myidCopy the code
Modifying a Configuration File
# enter configuration file directory CD/usr/local/apache/zookeeper - they are - 3.6.1 track - bin/conf / # zookeeper start the default load called zoo. CFG configuration file, CFG cp zoo_sample. CFG zoo. CFG # modify the configuration file vi zoo.cfgCopy the code
Major modifications:
- The data directory
dataDir
- Log directory
dataLogDir
- port
clientPort
(If it is a pseudo cluster of one machine, you need to modify port 2181, such as 2181, 2182, 2183) - Cluster configuration (If it is a pseudo cluster of one machine, you need to modify ports 2888 and 3888, such as 2888, 2889, 2890 and 3888, 3889, 3890)
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, / TMP here is just # example sakes. DataDir = / usr/local/apache/zookeeper - they are - 3.6.1 track - bin/data DataLogDir = / usr/local/apache/zookeeper - they are - 3.6.1 track - bin/log # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 ## Metrics Providers # # https://prometheus.io Metrics Exporter #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider # metricsProvider. HttpPort = 7000 # metricsProvider. ExportJvmInfo = true # # cluster configuration for server 1 of 1 is myid the contents of the file, 2888 is used to cluster internal communication, 1=192.168.10.101:2888:3888 server.2=192.168.10.102:2888:3888 server.3=192.168.10.103:2888:3888Copy the code
Start/close
Start.
CD/usr/local/apache/zookeeper - they are - 3.6.1 track - bin/bin/zkServer. Sh start ################################################################################# ZooKeeper JMX enabled by default Using Config: / usr/local/apache/zookeeper - they are - 3.6.1 track - bin/bin /.. /conf/zoo.cfg Starting zookeeper ... STARTEDCopy the code
Shut down.
CD/usr/local/apache/zookeeper - they are - 3.6.1 track - bin/bin/zkServer. Sh stop ################################################################################# ZooKeeper JMX enabled by default Using Config: / usr/local/apache/zookeeper - they are - 3.6.1 track - bin/bin /.. /conf/zoo.cfg Stopping zookeeper ... STOPPEDCopy the code
Viewing Cluster Status
CD/usr/local/apache/zookeeper - they are - 3.6.1 track - bin/bin/zkServer. Sh status # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 192.168.10.101 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ZooKeeper JMX enabled by default Using the config: / usr/local/zookeeper/apache - they are - 3.6.1 track - bin/bin /.. /conf/zoo.cfg Client port found: 2181\. Client address: localhost. Mode: Followers # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 192.168.10.102 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ZooKeeper JMX enabled by Default Using config: / usr/local/zookeeper/apache - they are - 3.6.1 track - bin/bin /.. /conf/zoo.cfg Client port found: 2181\. Client address: localhost. Mode: Leader # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 192.168.10.103 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ZooKeeper JMX enabled by default Using the config: / usr/local/zookeeper/apache - they are - 3.6.1 track - bin/bin /.. /conf/zoo.cfg Client port found: 2181\. Client address: localhost. Mode: followerCopy the code
If the preceding information is displayed, the ZooKeeper cluster environment is set up successfully. Then, you can connect to ZooKeeper through the RPC framework and use ZooKeeper as our registry. Click to get a video tutorial on Java Microservices Architecture and Spring Buckets. Hello Mr. Ward, welcome to our attention.