1. Create maven project
2. Add POM Dependency under XML file
3. Copy log4j The properties file to the project root directory
log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=target/spring.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
4 create zookeeper client
private static String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181"; private static int sessionTimeout = 2000; private ZooKeeper zkClient = null; @Before //If you need to execute part of the initialization code Before each @ Test method is executed, you can write the initialization code to @ Before public void init() throws Exception { //1: Connect to zk //ZooKeeper(String connectString , int sessionTimeout, Watcher watcher) // Refer to 1 connection information // Parameter 2 timeout // Parameter 3 listener is responsible for monitoring the changes of important objects or data. If there is a change, execute the processing method immediately. zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() { public void process(WatchedEvent watchedEvent) { System.out.println("Send SMS"); try { print(); } catch (Exception e) { e.printStackTrace(); } } }); }
5. Create a test class and create child nodes
6. When creating a non listening test, judge whether this node exists
7. Query the child nodes under sanguo
public void print() throws Exception { List<String> list = zkClient.getChildren("/sanguo", true); System.out.println(list); }
8. Obtain child nodes and listen for node changes, and change the value of Watch to true
public void print() throws Exception { List<String> list = zkClient.getChildren("/sanguo", true); System.out.println(list); } @Test public void test03() throws Exception { print(); Thread.sleep(Long.MAX_VALUE); }
9. First create the / servers node on the cluster
create /servers "servers"
10. Client listening code writing
11. Implementation of server-side write operation to Zookeeper
Create node
private void createNode(String hostname) throws KeeperException, InterruptedException { //create /servers/server1 "server1" System.out.println(hostname+" It's online"); zk.create(parentNode+"/"+hostname,hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); }
Create Hadoop node
12. Summary
ZooKeeper is a distributed, open source distributed application coordination service. It contains a simple primitive set. Distributed applications can implement synchronization services, configuration maintenance and naming services based on it. Configure multiple instances to form a cluster to provide external services to achieve the purpose of horizontal expansion. The data on each server is the same, and each server can provide external read and write services. This is the same as redis, that is, each server is equal to the client
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG sckgbj-1627476570383) (D: \ program \ big data \ Experiment 2 \ 30.png)]
There are three main types of roles in Zookeeper:
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-rMGZ6PfT-1627476570384)(D:\Program \ big data \ Experiment 2 \ 31.png)]
What are the deployment methods of ZooKeeper? What are the roles in the cluster? How many machines does the cluster need at least?
(1) Deployment mode: stand-alone mode and cluster mode
(2) Roles: Leader and Follower
(3) Minimum number of machines required for cluster: 3
Zookeeper monitoring:
Picture transfer in progress... (img-rMGZ6PfT-1627476570384)]
What are the deployment methods of ZooKeeper? What are the roles in the cluster? How many machines does the cluster need at least?
(1) Deployment mode: stand-alone mode and cluster mode
(2) Roles: Leader and Follower
(3) Minimum number of machines required for cluster: 3
Zookeeper monitoring:
Monitoring in zookeeper is lightweight, so it is easy to set up, maintain and distribute. When the client loses contact with the zookeeper server, the client will not receive the notification of monitoring events. Only after the client reconnects, if necessary, the previously registered monitoring will be re registered and triggered, which is usually transparent to developers. There is only one situation that will lead to the loss of monitoring events, that is, the monitoring of a znode node is set through exists(). However, if a client loses contact with the zookeeper server within the time interval between the creation and deletion of this znode node, the client will not be notified of the event even after reconnecting to the zookeeper server later.