欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java操作zookeeper集群 zookeeper 

程序员文章站 2022-03-22 17:39:04
...

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.kovansys.test</groupId>
  <version>1.1.0</version>

  <artifactId>zookeeper_test</artifactId>

  <packaging>jar</packaging>

  <properties>

    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>

    <spring.version>5.1.6.RELEASE</spring.version>
    <logback.version>1.2.3</logback.version>
    <slf4j.version>1.7.26</slf4j.version>
    <junit.version>5.4.1</junit.version>
    <java.encoding>UTF-8</java.encoding>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <zookeeper.version>3.4.14</zookeeper.version>

  </properties>
  <build>
    <directory>target</directory>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>${zookeeper.version}</version>
        
        <exclusions>
           <exclusion>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-log4j12</artifactId>
           </exclusion>
           <exclusion>
           <groupId>log4j</groupId>
           <artifactId>log4j</artifactId>
           </exclusion>
        </exclusions>

    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>


    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>${logback.version}</version>
    </dependency>

    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>${logback.version}</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jcl-over-slf4j</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>log4j-over-slf4j</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

  </dependencies>

</project>

 

    测试类

    

package zookeeper_test;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.junit.jupiter.api.Test;

public class ZookeeperTest {

  @Test
  public void testZookeeper() {
    int SESSION_TIMEOUT = 30 * 1000;

    Watcher wh = new Watcher() {
      public void process(WatchedEvent event) {
        System.out.println("WatchedEvent >>> " + event.toString());
      }
    };

    ZooKeeper zk = null;
    try {
      zk = new ZooKeeper("192.168.0.180:2181,192.168.0.180:2182,192.168.0.180:2183", SESSION_TIMEOUT, wh);

      // create node
      zk.create("/test", "My Data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
      // get node data
      System.out.println("data of /test is:" + String.valueOf(new String(zk.getData("/test", false, null))));

      // add watcher
      zk.exists("/test", wh);
      // update node
      zk.setData("/test", "Data updated".getBytes(), -1);
      // get node data
      System.out.println("data of /test is:" + String.valueOf(new String(zk.getData("/test", false, null))));

      // add watcher
      zk.exists("/test", wh);
      // delete
      zk.delete("/test", -1);
      // if node exist
      System.out.println(" node /test exists: [" + (zk.exists("/test", false) != null) + "]");

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (zk != null) {
        try {
          zk.close();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

  }
}

 

相关标签: zookeeper