参考博客:https://www.cnblogs.com/summerday152/p/14012622.html
单机
- docker pull zookeeper(拉取镜像)
- docker images (查看镜像)
- docker run -d -p 2181:2181 --name kaka-zookeeper --restart always 2b (启动镜像)
- docker ps(查看容器)
- docker exec -it d2 bash (进入容器)
- ./bin/zkCli.sh (启动zk客户端)
root@d2e3991366cc:/apache-zookeeper-3.7.0-bin# ./bin/zkCli.sh
Connecting to localhost:2181
2021-06-23 00:39:48,681 [myid:] - INFO [main:Environment@98] - Client environment:zookeeper.version=3.7.0-e3704b390a6697bfdf4b0bef79e3da7a4f6bac4b, built on 2021-03-17 09:46 UTC
2021-06-23 00:39:48,688 [myid:] - INFO [main:Environment@98] - Client environment:host.name=d2e3991366cc
2021-06-23 00:39:48,688 [myid:] - INFO [main:Environment@98] - Client environment:java.version=11.0.11
2021-06-23 00:39:48,690 [myid:] - INFO [main:Environment@98] - Client environment:java.vendor=Oracle Corporation
2021-06-23 00:39:48,690 [myid:] - INFO [main:Environment@98] - Client environment:java.home=/usr/local/openjdk-11
2021-06-23 00:39:48,694 [myid:] - INFO [main:Environment@98] - Client environment:java.class.path=/apache-zookeeper-3.7.0-bin/bin/../zookeeper-server/target/classes:/apache-zookeeper-3.7.0-bin/bin/../build/classes:/apache-zookeeper-3.7.0-bin/bin/../zookeeper-server/target/lib/*.jar:/apache-zookeeper-3.7.0-bin/bin/../build/lib/*.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/zookeeper-prometheus-metrics-3.7.0.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/zookeeper-jute-3.7.0.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/zookeeper-3.7.0.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/snappy-java-1.1.7.7.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/slf4j-log4j12-1.7.30.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/slf4j-api-1.7.30.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/simpleclient_servlet-0.9.0.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/simpleclient_hotspot-0.9.0.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/simpleclient_common-0.9.0.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/simpleclient-0.9.0.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/netty-transport-native-unix-common-4.1.59.Final.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/netty-transport-native-epoll-4.1.59.Final.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/netty-transport-4.1.59.Final.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/netty-resolver-4.1.59.Final.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/netty-handler-4.1.59.Final.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/netty-common-4.1.59.Final.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/netty-codec-4.1.59.Final.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/netty-buffer-4.1.59.Final.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/metrics-core-4.1.12.1.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/log4j-1.2.17.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/jline-2.14.6.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-util-ajax-9.4.38.v20210224.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-util-9.4.38.v20210224.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-servlet-9.4.38.v20210224.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-server-9.4.38.v20210224.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-security-9.4.38.v20210224.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-io-9.4.38.v20210224.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/jetty-http-9.4.38.v20210224.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/javax.servlet-api-3.1.0.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/jackson-databind-2.10.5.1.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/jackson-core-2.10.5.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/jackson-annotations-2.10.5.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/commons-cli-1.4.jar:/apache-zookeeper-3.7.0-bin/bin/../lib/audience-annotations-0.12.0.jar:/apache-zookeeper-3.7.0-bin/bin/../zookeeper-*.jar:/apache-zookeeper-3.7.0-bin/bin/../zookeeper-server/src/main/resources/lib/*.jar:/conf:
2021-06-23 00:39:48,695 [myid:] - INFO [main:Environment@98] - Client environment:java.library.path=/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib
2021-06-23 00:39:48,695 [myid:] - INFO [main:Environment@98] - Client environment:java.io.tmpdir=/tmp
2021-06-23 00:39:48,695 [myid:] - INFO [main:Environment@98] - Client environment:java.compiler=<NA>
2021-06-23 00:39:48,695 [myid:] - INFO [main:Environment@98] - Client environment:os.name=Linux
2021-06-23 00:39:48,695 [myid:] - INFO [main:Environment@98] - Client environment:os.arch=amd64
2021-06-23 00:39:48,695 [myid:] - INFO [main:Environment@98] - Client environment:os.version=3.10.0-957.21.3.el7.x86_64
2021-06-23 00:39:48,695 [myid:] - INFO [main:Environment@98] - Client environment:user.name=root
2021-06-23 00:39:48,695 [myid:] - INFO [main:Environment@98] - Client environment:user.home=/root
2021-06-23 00:39:48,695 [myid:] - INFO [main:Environment@98] - Client environment:user.dir=/apache-zookeeper-3.7.0-bin
2021-06-23 00:39:48,695 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.free=21MB
2021-06-23 00:39:48,697 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.max=247MB
2021-06-23 00:39:48,699 [myid:] - INFO [main:Environment@98] - Client environment:os.memory.total=29MB
2021-06-23 00:39:48,705 [myid:] - INFO [main:ZooKeeper@637] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@4c70fda8
2021-06-23 00:39:48,713 [myid:] - INFO [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-06-23 00:39:48,720 [myid:] - INFO [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2021-06-23 00:39:48,763 [myid:] - INFO [main:ClientCnxn@1726] - zookeeper.request.timeout value is 0. feature enabled=false
Welcome to ZooKeeper!
2021-06-23 00:39:48,811 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1171] - Opening socket connection to server localhost/127.0.0.1:2181.
2021-06-23 00:39:48,812 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1173] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
2021-06-23 00:39:48,866 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1005] - Socket connection established, initiating session, client: /127.0.0.1:35988, server: localhost/127.0.0.1:2181
JLine support is enabled
2021-06-23 00:39:48,972 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1438] - Session establishment complete on server localhost/127.0.0.1:2181, session id = 0x1000362a0b70000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
- 测试zk是否启动成功(创建删除节点)
- java连接zk测试,(我这里用的是3.4版本的 docker pull zookeeper:3.4)
<!--测试zk-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
<!--测试zk-->
public class ZooKeeperClient {
private static String connectString = "47.100.41.55:2181";
private static int sessionTimeout = 4000;
private ZooKeeper zkClient = null;
@Before
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
System.out.println(watchedEvent.getType() + "---" + watchedEvent.getPath());
try {
ZooKeeperClient.this.zkClient.getChildren("/", true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
@After
public void close() throws InterruptedException {
zkClient.close();
}
// 参数1:数据插入到哪个节点下
// 参数2:需要插入的数据
// 参数3:安全验证的模式,一般选ZooDefs.Ids.OPEN_ACL_UNSAFE
// 参数4:插入的类型,PERSISTENT(永久有效的),PERSISTENT_SEQUENTIAL(久化的有顺序编号的)
// EPHEMERAL(临时的),EPHEMERAL_SEQUENTIAL(临时的有顺序编号的)
@Test
public void create() throws KeeperException, InterruptedException {
String path = "/tod";
String data = "huashi";
// String nodeCreated = zkClient.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
// String nodeCreated = zkClient.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
String nodeCreated = zkClient.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(nodeCreated + " done!");
}
@Test
public void getChildren() throws KeeperException, InterruptedException {
String path = "/";
List<String> children = zkClient.getChildren( path, true);
for (String child : children) {
System.out.println(child);
}
// Thread.sleep(Long.MAX_VALUE);
}
@Test
public void exist() throws KeeperException, InterruptedException {
String path = "/tod";
Stat stat = zkClient.exists(path, false);
System.out.println(stat == null ? path + " no exit!" : path + " exit!");
}
@Test
public void delete() throws KeeperException, InterruptedException {
// String path = "/tod";
String path = "/tod0000000002";
zkClient.delete(path, zkClient.exists(path, false).getVersion());
}
@Test
public void set() throws KeeperException, InterruptedException {
String path = "/tod";
String data = "shenda";
Stat stat = zkClient.setData(path, data.getBytes(), zkClient.exists(path, false).getVersion());
System.out.println(stat);
}
@Test
public void get() throws KeeperException, InterruptedException {
String path = "/tod";
byte[] data = zkClient.getData(path, false, zkClient.exists(path, false));
System.out.println(new String(data));
}
}
- 使用 Curator 测试 zookeeper
<!-- 原生zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!--Apache Curator-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.9.0</version>
</dependency>
<!-- zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
public class CuratorTest {
public static void main(String[] args) throws Exception{
CuratorFramework client = CuratorFrameworkFactory.newClient("47.100.41.55:2181", new RetryNTimes(10, 5000));
client.start();// 连接
// 获取子节点,顺便监控子节点
List<String> children = client.getChildren().usingWatcher(new CuratorWatcher() {
public void process(WatchedEvent event) throws Exception
{
System.out.println("监控: " + event);
}
}).forPath("/");
System.out.println("children = "+children);
// 创建节点
String result = client.create().withMode(CreateMode.PERSISTENT).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).forPath("/test", "Data".getBytes());
System.out.println("result = "+result);
// 设置节点数据
client.setData().forPath("/test", "111".getBytes());
client.setData().forPath("/test", "222".getBytes());
// 删除节点
//System.out.println(client.checkExists().forPath("/test"));
/*client.delete().withVersion(-1).forPath("/test");
System.out.println(client.checkExists().forPath("/test"));*/
client.close();
System.out.println("OK!");
}
}