This is the third day of my participation in the August More Text Challenge.

ZooKeeper

The installation of a zookeeper

1. Preparations:#! Prepare three nodes. The host name must be configured and the system time must be maintained between the servers
#! Note that /etc/hostname and /etc/hosts are used to configure the hostname.
#! Special idea the following operation 3 nodes to operate at the same time oh!#! Disable the firewall.Systemctl start firewalld Disable the firewall systemctl stop Firewalld Restart the firewall systemctl restart firewalld Check the firewall status systemctl Status Firewalld Disabling the firewall systemctl disable firewalld#! Local access: ping2. Upload ZK to three server nodes#! Note that I unzip to /usr/localUnder the2.1 Decompressing the file :tar zookeeper-3.4.6.tar.gz 2.2 Rename :mv zookeeper-3.4.6 ZooKeeper 2.3 Modifying environment Variables :vim /etc/profile#! This is where you add zooKeeper's global variable
export zOOKEEPER_HOME=/usr/local/zookeeper
export PATH=.:$ZOOKEEPER_HOME/bin
#2.4 Refreshing Environment Variables:
source /etc/profile
#! 2.5 Modifying the Configuration File in ZooKeeper:
#! 2.5.1 Go to the specified directory first:
  cd /usr/local/zookeeper/conf
#! 2.5.2 Then copy the zoo_sample. CFG file to zoo. CFG:
  mv zoo_sample.cfg zoo.cfg
#! 2.5.3 Then modify the two parts, save the changes and exit :wq
#! (1) Modify the data dir
      dataDir=/usr/local/zookeeper/data
#! (2) Change the cluster ADDRESS0= HKH221:2888:3888 server.1= HKH222:2888:3888 server.2= HKH223:2888:3888 and 2.5.4 Adding server identity configuration, there are 2 steps, the first is to create folders and files, The second is to add configuration:#! (1) Create folder:The mkdir/usr/local/zookeeper - 3.4.6 / data#! Create file myid path should be created in /usr/local/zookeeper-3.4.6/data:Vim /usr/local/zookeeper-3.4.6/data/ myid The contents of the myID file of each server are different. Change the values to 0, 1, and 2 respectively. With our previous zoo. CFG in the configuration file: server. 0, server. 1, for server 2 order, and then save the exit;#! 2.7 Zookeeper cluster environment is complete! Start the ZooKeeper commandBoot path: / usr/local/zookeeper/bin (also can be in any directory, because the configuration environment variable) execute the command: zkServer. Sh start (note 3 machine to start here, start to view state)
#! View status:Sh status (Check the ZK mode on the three nodes. One eader and two followers are displayed.) zkcli. sh Enters the ZooKeeper client
#! Follow the prompts to operate:Find: ls /ls/ZooKeeper#! Create and assign:Create/Jacquesh ZooKeeper Obtain :get/Jacquesh Set the value :set/Jacquesh Zookeeper1314 PS1: Any node can view the ZooKeeper cluster data PS2: Create nodes of two types: Ephemeral
#! Set zK startupCD /etc/rc.d/init.d/ touch ZooKeeper chmod 777 ZooKeeper vim Start ZooKeeper.#! /bin/bashProcessname: zooKeeper export JAVA_HOME=/usr/local/jdk1.8 export PATH=$JAVA_HOME/bin:$PATH case $1 in start) /usr/local/zookeeper-3.4.6/bin/ zkserver. sh start; Stop)/usr/local/zookeeper 3.4.6 / bin/zkServer. Sh stop;; Status)/usr/local/zookeeper 3.4.6 / bin/zkServer. Sh status;; Restart)/usr/local/zookeeper 3.4.6 / bin/zkServer. Sh restart;; *) echo "require start|stop|status|restart";; esac
#! Startup configuration:
chkconfig zookeeper on
#! Validation:
chkconfig --add zookeeper
chkconfig --list zookeeper

#! Start or stop the ZooKeeper service
service zookeeper start/stop

#! Add ZooKeeper to boot
chkconfig --add zookeeper

#! Check whether the added ZooKeeper is in the startup task
chkconfig --list zookeeper
Copy the code

code

Pom file

    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.5.9</version>
    </dependency>
Copy the code

ZKUtils

public class ZKUtils {

    / / zookeeper instance
    private ZooKeeper zk;
    // Distributed service lock service directory

    private String rootPath = "/lock";
    / / they address
    private String address = "193.112.8.126:2181";
    // The zooKeeper instantiated gate prevents uninstantiated calls
    private CountDownLatch createLatch = new CountDownLatch(1);
    // Distributed thread lock
    private CountDownLatch threadLatch = new CountDownLatch(1);
    //watch
    private DefaultWatch watch;
    private Integer timeout = 10000;

    public static class Builder {
        /** * exposes the configuration to the main data using the constructor pattern */
        private Integer timeout;
        private String address;
        private String rootPath;

        public Builder setTimeout(Integer timeout) {
            this.timeout = timeout;
            return this;
        }

        public Builder setAddress(String address) {
            this.address = address;
            return this;
        }

        public Builder setRootPath(String rootPath) {
            this.rootPath = rootPath;
            return this;
        }

        public ZKUtils build(a) throws IOException, InterruptedException, KeeperException {

            ZKUtils zkUtils = new ZKUtils();
            if (address == null) address = zkUtils.address;
            if (timeout == null) timeout = zkUtils.timeout;
            if (rootPath == null) rootPath = zkUtils.rootPath;

            zkUtils.zk = new ZooKeeper(address, timeout,
                    /** * add this paragraph in case of error. -. * /event -> { System.out.println(event.toString()); });/** * Initialize DefaultWatch */
            zkUtils.watch = new DefaultWatch(zkUtils.createLatch, zkUtils.threadLatch, zkUtils.zk, rootPath);
            /** * Predetermine whether the service master node exists
            Stat stat = zkUtils.zk.exists(rootPath, zkUtils.watch);
            if (stat == null) {
                zkUtils.zk.create(rootPath,
                        rootPath.getBytes(),
                        ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                /** * Lock the process after the create instruction is sent to prevent other zooKeeper methods */ from being called before the Node is created
                zkUtils.createLatch.await();
            }
            returnzkUtils; }}public static Builder builder(a) {
        /** * returns the builder instance */
        return new Builder();
    }

    /** * Close method */
    public void closed(a) {
        if(zk ! =null) {
            try {
                zk.close();
            } catch(InterruptedException e) { e.printStackTrace(); }}}/ * * *@linkDefaultWatch * performs distributed lock operations * implements the process to see *@see  DefaultWatch#tryLock
     */
    public void tryLock(a) {
        watch.tryLock();
    }

    public void unLock(a) { watch.unLock(); }}Copy the code

DefaultWatch

public class DefaultWatch implements Watcher.AsyncCallback.ChildrenCallback.AsyncCallback.Create2Callback {
    CountDownLatch createLatch;
    ZooKeeper zooKeeper;
    CountDownLatch threadLatch;
    String rootPath;
    private    String lockName;
    public DefaultWatch(CountDownLatch createLatch,CountDownLatch threadLatch,ZooKeeper zooKeeper,String rootPath) {
        this.threadLatch=threadLatch;
        this.createLatch = createLatch;
        this.zooKeeper=zooKeeper;
        this.rootPath=rootPath;
    }
    public void tryLock(a) {
        / / reentrant
        try {
            /** * The lock operation is to create the thread on the zooKeeper node using the createmode.ephemeral_sequential number */
            zooKeeper.create(rootPath+"/lock", Thread.currentThread().getName().getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL,
            this, Thread.currentThread().getName());
            /** * Wait for the ZooKeeper callback to wake up the thread@see DefaultWatch#processResult(int, String, Object, String, Stat)
             */
            threadLatch.await();
        } catch(InterruptedException e) { e.printStackTrace(); }}@Override
    public void process(WatchedEvent event) {
        switch (event.getType()){
            case None :
                System.out.println("None");
                break;
            case  NodeCreated:
                if (event.getPath().equals(rootPath)){
                    System.out.println(event.getPath()+"Node Creation");
                    createLatch.countDown();
                    System.out.println("Release");
                }
                break;
            case  NodeDeleted:
                System.out.println("NodeDeleted");
                break;
            case  NodeDataChanged:
                System.out.println("NodeDataChanged");
                break;
            case  NodeChildrenChanged:
                System.out.println("NodeChildrenChanged");
                 zooKeeper.getChildren(rootPath, this.this."");

                break;
            case  DataWatchRemoved:
                System.out.println("DataWatchRemoved");
                break;
            case  ChildWatchRemoved:
                System.out.println("ChildWatchRemoved");
                break; }}/** * The function of requesting all child node callbacks is to sort all node nodes ** select the smallest node to release the lock ** /
    @Override
    public void processResult(int rc, String path, Object ctx, List<String> children) {
        if(children! =null&&children.size()>0){
            Collections.sort(children);
            if(lockName.equals(rootPath+"/"+children.get(0))){ threadLatch.countDown(); }}else{
            /** * If the business root node has no children, all tasks are completed */
            System.out.println("All missions completed."); }}/** * Callback after node creation */
    @Override
    public void processResult(int rc, String path, Object ctx, String name, Stat stat) {
        // Create locks after each thread starts, then get locks all children in the directory, do not register watch in the lock directory
           System.out.println(ctx.toString()+" create path: "+ name);
        /** * Assign the name of the created node to the lock name * then get all the children of the business root * just pass the request and still have to wait for the callback * Request to see the callback of the child node can be seen *@see DefaultWatch#processResult(int, String, Object, List) 
         */
        lockName = name;
        zooKeeper.getChildren(rootPath, this.this, ctx );
    }

    /** * All threads will go to getChildren to determine if they are the thread that acquired the lock@see DefaultWatch#process(WatchedEvent) 
     */
    public void unLock(a) {
        try {
            zooKeeper.delete(lockName,-1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch(KeeperException e) { e.printStackTrace(); }}}Copy the code
public interface RejectedExecutionHandler {
    void rejectedExecution(Runnable r, ThreadPoolExecutor executor);
}
Copy the code