docker 应用 —— docker安装zookeeper单机版

文章目录

    • 1、查看zookeeper镜像
    • 2、运行安装命令:
    • 3、 查看 zookeeper进程
    • 4、进入 zkCli.sh
    • 5、ZooInspector 客户端连接
    • 6、使用 Curator 测试 zookeeper
      • 6.1、maven 依赖
      • 6.2、测试代码 CuratorTest .java
      • 6.3、日志
      • 6.4、在 ZooInspector 中查看

1、查看zookeeper镜像

执行命令 docker search zookeeper

[root@iz2zei0x4t16rv0e5buzvhz /]# docker search zookeeper
NAME                                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
zookeeper                           Apache ZooKeeper is an open-source server wh…   544                 [OK]                
jplock/zookeeper                    Builds a docker image for Zookeeper version …   162                                     [OK]
mesoscloud/zookeeper                ZooKeeper                                       73                                      [OK]
wurstmeister/zookeeper                                                              65                                      [OK]
confluentinc/cp-zookeeper           Official Confluent Docker Image for Zookeeper   39                                      
mbabineau/zookeeper-exhibitor                                                       23                                      [OK]
digitalwonderland/zookeeper         Latest Zookeeper - clusterable                  18                                      [OK]
[root@iz2zei0x4t16rv0e5buzvhz /]# 

使用官方的: zookeeper

2、运行安装命令:

docker run -d \
-p 2181:2181 \
-v /mysoft/zookeeper/data/:/data/ \
--name=zookeeper  \
--privileged zookeeper

3、 查看 zookeeper进程

[root@iz2zei0x4t16rv0e5buzvhz /]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                                                   NAMES
3a7e6e62ba6c        zookeeper           "/docker-entrypoint.…"   7 seconds ago       Up 7 seconds        2888/tcp, 0.0.0.0:2181->2181/tcp, 3888/tcp                                              zookeeper
[root@iz2zei0x4t16rv0e5buzvhz /]# 

4、进入 zkCli.sh

docker 应用 —— docker安装zookeeper单机版_第1张图片

5、ZooInspector 客户端连接

启动命令:java -jar zookeeper-dev-ZooInspector.jar

操作记录:

C:\Users\Administrator>cd D:\develop_tools\apache\zookeeper\ZooInspector\build
C:\Users\Administrator>d:
D:\develop_tools\apache\zookeeper\ZooInspector\build>java -jar zookeeper-dev-ZooInspector.jar

docker 应用 —— docker安装zookeeper单机版_第2张图片

输入地址:
docker 应用 —— docker安装zookeeper单机版_第3张图片

进入后,
docker 应用 —— docker安装zookeeper单机版_第4张图片

6、使用 Curator 测试 zookeeper

6.1、maven 依赖


<dependency>
    <groupId>org.apache.zookeepergroupId>
    <artifactId>zookeeperartifactId>
    <version>3.4.6version>
dependency>


<dependency>
    <groupId>org.apache.curatorgroupId>
    <artifactId>curator-frameworkartifactId>
    <version>2.9.0version>
dependency>


<dependency>
    <groupId>com.101tecgroupId>
    <artifactId>zkclientartifactId>
    <version>0.9version>
dependency>

<dependency>
    <groupId>log4jgroupId>
    <artifactId>log4jartifactId>
    <version>1.2.17version>
dependency>

6.2、测试代码 CuratorTest .java

import java.util.List;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.CuratorWatcher;
import org.apache.curator.retry.RetryNTimes;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.ZooDefs;

public class CuratorTest {
	
    public static void main(String[] args) throws Exception{
    	
        CuratorFramework client = CuratorFrameworkFactory.newClient("xx.xx.xx: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!");
    }
}

6.3、日志

执行代码后,日志如下:
docker 应用 —— docker安装zookeeper单机版_第5张图片

6.4、在 ZooInspector 中查看

刷新ZooInspector 后,看开添加的节点和节点的值。

docker 应用 —— docker安装zookeeper单机版_第6张图片

你可能感兴趣的:(docker,Zookeeper,Linux)