zookeeper及zkui安装

本文详细介绍了Zookeeper的安装步骤,包括单机、UI界面(zkui)安装和集群配置。接着讲解了如何在SpringBoot中集成Zookeeper,使用Curator作为客户端,并提供了相关配置和操作示例。最后提到了Zookeeper集群的搭建过程,以及通过配置文件创建和管理节点的方法。
摘要由CSDN通过智能技术生成

zookeeper安装

        1.下载地址:https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3.tar.gz

        2.解压

tar -zxvf apache-zookeeper-3.6.3-bin.tar.gz

mv apache-zookeeper-3.6.3-bin zookeeper

mv zookeeper /home/

        3.创建数据目录     

cd /home/zookeeper/

mkdir data

        4.修改配置文件

cd /home/zookeeper/conf

cp zoo_sample.cfg zoo.cfg

#修改数据目录
dataDir=/home/zookeeper/data

        5.启动及查看状态

cd /home/zookeeper/bin

#启动
./zkServer.sh start

#查看状态
./zkServer.sh status

#关闭
./zkServer.sh stop

zkui安装

        1.下载地址:https://github.com/DeemOpen/zkui。因为是源码安装,所以如果服务器上没有maven环境,需先安装maven。

        2.解压

unzip  zkui-master.zip

mv zkui-master zkui

mv zkui /home/

        3.编译

cd /home/zkui/

mvn clean install

        4.修改配置文件

#拷贝配置文件
cp config.cfg  ./target/

#修改zkserver地址
zkServer=192.168.88.204:2181

#修改登录用户
userSet = {"users": [{ "username":"qxmz" , "password":"qxmz123","role": "ADMIN" }

        5.启动

cd target

#编译后的zkui是springboot格式的jar包
nohup java -jar zkui-2.0-SNAPSHOT-jar-with-dependencies.jar &

zookeeper集群安装(3台服务器举例:88.204,88.205,88.206)

        1.参考单机模式,在三台机器上分别部署zookeeper。

        2.修改配置文件,添加集群节点。三台机器一样

server.1=192.168.88.204:2888:3888
server.2=192.168.88.205:2888:3888
server.3=192.168.88.206:2888:3888

        3.新建myid(目录就是dataDir)

cd /home/zookeeper/data

#88.204
echo 1 > myid

#88.205
echo 2 > myid

#88.206
echo 3 > myid

        4.分别启动三台zk,并查看当前节点状态

./zkServer.sh start

./zkServer.sh status

springboot集成

        1.以集成curator举例说明

        2.pom文件添加依赖

<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-framework</artifactId>
	<version>5.1.0</version>
</dependency>
<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-recipes</artifactId>
	<version>5.1.0</version>
</dependency>
<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-client</artifactId>
	<version>5.1.0</version>
</dependency>

        3.初始化连接

package com.qxmz.config;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @description:
 * @author: junzhang
 * @time: 2021/7/12 14:52
 */
@Configuration
public class ZookeeperConfig {
    @Resource
    CommonConfig commonConfig;

    @Bean
    public CuratorFramework curatorFramework() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(commonConfig.getElapsedTimeMs(), commonConfig.getRetryCount());
//        CuratorFramework client = CuratorFrameworkFactory.newClient(
//                connectString,
//                sessionTimeoutMs,
//                connectionTimeoutMs,
//                retryPolicy);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString(commonConfig.getConnectString())
                .connectionTimeoutMs(commonConfig.getConnectionTimeoutMs())
                .sessionTimeoutMs(commonConfig.getSessionTimeoutMs())
                .retryPolicy(retryPolicy)
                .build();
        client.start();
        return client;
    }


}

        4.工具类

package com.qxmz.utils;

import com.qxmz.config.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.CuratorCache;
import org.apache.curator.framework.recipes.cache.CuratorCacheListener;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.zookeeper.CreateMode;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: junzhang
 * @time: 2021/7/12 15:10
 */
@Component
@Slf4j
public class CuratorUtils {
    @Resource
    CuratorFramework client;
    @Resource
    CommonConfig commonConfig;

    /**
     * @Description: 创建临时节点
     * @Param:
     * @return:
     * @Author: junzhang
     * @date: 2021/7/12 15:16
     */
    public void createEphemeralNode(String path, String value) {
        try {
            // 递归创建所需父节点
            client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path, value.getBytes());
        } catch (Exception e) {
            log.error("createEphemeralNode error...", e);
        }
    }

    /**
     * @Description: 创建永久节点
     * @Param:
     * @return:
     * @Author: junzhang
     * @date: 2021/7/12 15:16
     */
    public void createPersistentNode(String path, String value) {
        try {
            // 递归创建所需父节点
            client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, value.getBytes());
        } catch (Exception e) {
            log.error("createEphemeralNode error...", e);
        }
    }

    /**
     * @Description: 检测节点是否存在
     * @Param:
     * @return:
     * @Author: junzhang
     * @date: 2021/7/12 15:55
     */
    public boolean isExistNode(String path) {
        client.sync();
        try {
            return client.checkExists().forPath(path) != null;
        } catch (Exception e) {
            log.error("isExistNode error...", e);
        }
        return false;
    }

    /**
     * @Description: 设置节点数据
     * @Param:
     * @return:
     * @Author: junzhang
     * @date: 2021/7/12 15:58
     */
    public void setNodeData(String path, String nodeData) {
        try {
            client.setData().forPath(path, nodeData.getBytes("UTF-8"));
        } catch (Exception e) {
            log.error("setNodeData error...", e);
        }
    }

    /**
     * @Description: 获取节点数据
     * @Param:
     * @return:
     * @Author: junzhang
     * @date: 2021/7/12 16:02
     */
    public String getNodeData(String path) {
        try {
            if (!isExistNode(path)) {
                return null;
            }
            byte[] dataByte = client.getData().forPath(path);
            String data = new String(dataByte, "UTF-8");
            if (StringUtils.isNotEmpty(data)) {
                return data;
            }
        } catch (Exception e) {
            log.error("getNodeData error...", e);
        }
        return null;
    }

    /**
     * @Description: 获取节点下所有子节点数据
     * @Param:
     * @return:
     * @Author: junzhang
     * @date: 2021/7/12 16:56
     */
    public List<String> getChildNodeData(String path) {
        try {
            return client.getChildren().forPath(path);
        } catch (Exception e) {
            log.error("getChildNodeData error...", e);
        }
        return null;
    }

    /**
     * @Description: 删除节点
     * @Param:
     * @return:
     * @Author: junzhang
     * @date: 2021/7/12 16:06
     */
    public void deleteNode(String path) {
        try {
            //client.delete().deletingChildrenIfNeeded().forPath(path);
            client.delete().guaranteed().deletingChildrenIfNeeded().forPath(path);
        } catch (Exception e) {
            log.error("deleteNode error...", e);
        }
    }

    @Bean(name = "ocrServerList")
    public List<String> serversList() throws Exception {
        List<String> serversList = new ArrayList<>();
        String path = commonConfig.getOcrPath();
        List<String> childList = getChildNodeData(path);
        for (String child : childList) {
            String value = getNodeData(path + "/" + child);
            serversList.add(value);
        }
        //设置监听
        addWatcherWithChildCache(path, serversList);
        return serversList;
    }

    /**
     * @Description: 监听节点
     * @Param:
     * @return:
     * @Author: junzhang
     * @date: 2021/7/12 16:17
     */
    public void addWatcherWithChildCache(String path, List<String> serversList) throws Exception {
        CuratorCache curatorCache = CuratorCache.build(client, path);
        CuratorCacheListener listener = CuratorCacheListener.builder().forNodeCache(new NodeCacheListener() {
            public void nodeChanged() throws Exception {
                log.info("node change---------");
                serversList.clear();
                List<String> varList = client.getChildren().forPath(path);
                for (String var : varList) {
                    byte[] dataByte = client.getData().forPath(path + "/" + var);
                    String data = new String(dataByte, "UTF-8");
                    serversList.add(data);
                }
            }
        }).build();
        /*CuratorCacheListener listener = CuratorCacheListener.builder().forPathChildrenCache(File.separator, client, (c, e) -> {
            // child node listener
            System.out.println("change---------");
        }).build();*/
        /*curatorCache.listenable().addListener(new CuratorCacheListener() {
            @Override
            public void event(Type type, ChildData childData, ChildData childData1) {
                System.out.println("change---------");
            }
        });*/
        curatorCache.listenable().addListener(listener);
        curatorCache.start();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值