zookeeper study notes - introduction to basic usage

Basic functions of zk Understand

zookeper is a high-performance, distributed, open source distributed application coordination service. It provides simple and primitive functions. Distributed applications can implement advanced services based on it, such as synchronization, configuration management, cluster management, and naming services. . It is easy to program and uses the file system directory as the data model. The server runs on java, and the client uses

the functions of c and zk in java2: coordination
: multiple nodes complete an action together
1. Cluster member management
2. Locks
3. Master selection 4.
Synchronization Data model 1. Hierarchical structure 2. Each node in the tree structure is called Znode 3. Each znode has data (byte[] type), and can also have child nodes Node path slash division /host/ds, no relative Path Store data changes, ACL changes and timestamps through the data structure stat Data changes, the version number will increase. The data in the znode can be read and written. Application Scenarios Data publishing/subscribing Data publishing/subscribing is the so-called configuration center , the publisher publishes the data to one or a series of nodes in zk , and the reader subscribes to the data. When the data changes, he will be notified of the data change in time. The essence of load balancing is to use the configuration management function of zookeeper. The steps involved are: : 1. The service provider registers the mapping of its own domain name and IP port into zk





















2. The service consumer obtains the corresponding IP and port from zk through the domain name. There are multiple IPs and ports, but only one of them is obtained
. 3. When the service provider goes down, the correspondence between the corresponding domain name and IP will be reduced. A mapping
4. Ali's dubbo service framework is based on zk to implement service routing and load

. Naming service
In distributed systems, Name Service is also an important application scenario, and through zk, it can also be similar to the one
in J2EE. The effect of JNDI; in a distributed environment, the naming service is more of a resource location, not a real entity resource.
Its essence is to use zk for centralized configuration management and search.

Distributed coordination/notification
– realized through watcher and notification mechanism
– distribution Locks
– Distributed Transactions

Distributed Locks
– Exclusive Locks
– Shared Locks
Distributed Queues
– FIFO

Cluster Roles
– Leader: Provides read and write services for clients
– Follower: Provides read services, all write services need to be transferred to the Leader role, Participate in elections
– Observer: provides read services and participates in the election process, generally to enhance the read request concurrency capability of the zk cluster
Session
– the connection between the Zk client and the zk server
– to maintain the client through heartbeat detection Connected sessions
- receive notifications of watch events from the server
- can set a timeout

Basic Concepts
Data node (Znode)
– not the meaning of the machine
– data node in the Zk tree structure, used to store data
– persistent node: once created, it will always be stored in zk unless the delete operation is actively called
– temporary node: Binding with the client's session, once the client session is invalid, all temporary nodes created by the client will be removed
– SEQUENTIAL Znode: When creating a node, if the attribute SEQUENTIAL is set, it will automatically append an
integer to the Type number, which will be automatically incremented when the same node is created.

Version
– Version: The version of the current Znode
– Cversion: The version of the child node of the
current Znode – Aversion: The ACL (ask control) version of the current Znode
Watcher
– Acting on the Znode node
– Various event notifications: data update, child node status, etc.

ACL
- Access Control Lists
- similar to linux/unix permission control
- CREATE: permission to create child nodes
- READ: permission to get node data and child node list
- WRITE: update node Permissions for data
– DELETE: permission to delete child nodes
– ADMIN: permission to set node ACL
CREATE and DELETE are permission controls for child nodes

zookeeper stand-alone mode

In the development and test environment, there are not many physical resources, because the stand-alone mode is adopted. Of course, the cluster mode can also be deployed on a single physical machine, but this will increase the resource consumption of the resources of the stand-alone physical machine. In the development environment, we generally use the stand-alone
mode.
stand-alone mode cannot be used in the production environment, because neither the reliability of the system nor the read and write performance can meet the needs of production.
Zk is developed based on the java language, because the resource requirements of the stand-alone mode Similar to ordinary java programs, 1 CPU and
512m of memory are enough

– Zk download address: http://www.apache.org/dyn/closer.cgi/zookeeper/
– decompress the file
Windows: use winzip or winwar • Linux: use tar –zxvf zookeeper -
3.4.6.tar.gz – to
configure environment variables This parameter is generally configured for easy operation. bin – .sh: linux environment – ​​.cmd: windows environment conf – zoo_sample.cfg is an example configuration file, which needs to be modified: your own name, usually zoo.cfg – Log4j.properties is the log configuration file











Contrib: some toolkits for operating zk
lib: some packages that zk depends on
Recipes: code samples for some usages of zk
Dist-maven: the release directory compiled by

maven Change zoo_sample.cfg in conf to zoo.cfg
Zoo. cfg
– tickTime: The default is 3000ms. As a basic unit, multiples of it can be used to represent the internal time interval configuration of the system, for example:
• 2*tickTime is the timeout time of the client session
• 1*tickTime is the connection between the client and the zk server Heartbeat time
– dataDir: no default configuration, must be configured; if it is not configured, it will not be able to start. It is used to configure the directory for storing snapshot files. If dataLogDir is not configured, then
transaction logs will also be stored in this directory
– clientPort: zk running port, The default is 2181
to start
– Windows: double-click zkServer.cmd directly
– Linux: zkServer.sh start

client command line
– Linux: bin/zkCli.sh
– Windows: bin/zkCli.cmd
– Without any parameters, connect to localhost:2181 by default
– zkCli.cmd –server ip:port connect to the specified server address

View command line help
– Enter help on the command line to view all commands:
Lists and Syntax
- In fact, entering any character and carriage return will prompt help information
ls path [watch]
• Path represents the node path of the specified data node
• List all child nodes under the specified node
• Can view all child nodes of the first level
• There is a default zookeeper reserved node under ls / during installation
• Watch means to monitor the changes of the child nodes of the path

Create
– create [-s] [-e] path data acl
• Create a zookeeper node
• -s or -e means to create a build It is a sequence or temporary node, and a persistent node is created without default
. Path: The full path of the node, there is no relative node representation.
Data: The data stored in the current node.
Acl is used for permission control, by default Do not do any permission control

Read
– get path [watch]
• Get the data content and attribute information of the specified node
• Path represents the node path of the specified data node
• In the right picture
– the data in the wokers node
– cZxid is the transaction that created the node id
– Mzxid last updated transaction id of this node
– Mime: last update time
– cversion child node version
– dataVersion data version
– aclVersion: Ask the control version

Update
– set path data [version]
• Update the data content of the specified node
• Path indicates the path of the node to be updated
• data: The updated data
• Version: Specify the updated data version, generally not specified, if
If the data version has been updated, an error will be reported when the old version is specified

Delete
– delete path [version]
• Delete the specified node
• Path represents the deleted node
• Version: Specify the deleted data version, generally not specified, if the
data version has been updated, An error will be reported when specifying an older version
















Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326980212&siteId=291194637