zookeeper及zkui安装
作者:互联网
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(); } }
标签:zkui,return,String,org,zookeeper,client,import,path,安装 来源: https://blog.csdn.net/zjun1001/article/details/118703434