目录
3.使用连接
提示:以下是本篇文章正文内容,下面案例可供参考
一、Curator是什么?
Curator是开源的Zookeeper的连接客户端,封装了比zk自身提供的客户端更好的连接方式。
二、使用步骤
1.引入库
代码如下(示例):
1、如果自身代码已经有了Zookeeper引用jar包了,把curator-framework里面的zookeeper剔除
2、如果要使用zookeeper的分布式锁或者其他高级功能,需要引入curator-recipes包
3、curator的版本与zookeeper版本有关系,我使用的是3.4.x要配合curator的4.2.0版本
<!--Curator-连接zookeeper客户端 zk 3.4.x的客户端配 4.2.0-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
2.创建连接
2.1普通连接zookeeper:
package com.ustcinfo.eop.eopapi.config;
import com.ustcinfo.eop.eopapi.entity.stats.ZookeeperProperties;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/**
* ClassName: CuratorConfig
* Create: 2023/8/9 9:54
*
* @author SLIU
* @Version 1.0
* Description:
*/
@Configuration
public class CuratorConfig {
@Resource
private ZookeeperProperties wrapperZk;
private CuratorFramework build;
@Bean
public CuratorFramework init(){
CuratorFramework build = CuratorFrameworkFactory.
newClient(wrapperZk.getAddress(), wrapperZk.getSessionTimeoutMs(),
wrapperZk.getConnectionTimeoutMs(),
new RetryNTimes(wrapperZk.getRetryCount(), wrapperZk.getElapsedTimeMs()));
build.start();
return build;
}
}
2.2权限连接zookeeper:
package com.ustcinfo.eop.eopapi.config;
import com.ustcinfo.eop.eopapi.entity.stats.ZookeeperProperties;
import com.ustcinfo.eop.eopapi.utils.AESUtil;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/**
* ClassName: CuratorConfig
* Create: 2023/8/9 9:54
*
* @author SLIU
* @Version 1.0
* Description:
*/
@Configuration
public class CuratorConfig {
@Resource
private ZookeeperProperties wrapperZk;
private CuratorFramework build;
@Bean
public CuratorFramework init(){
build = CuratorFrameworkFactory.builder()
.authorization("digest", ("admin:" + AESUtil.decrypt(wrapperZk.getZkAdminPassword(), "eopYes")).getBytes())
.connectString(wrapperZk.getAddress())
.connectionTimeoutMs(wrapperZk.getConnectionTimeoutMs())
.retryPolicy(new RetryNTimes(wrapperZk.getRetryCount(), wrapperZk.getElapsedTimeMs()))
.build();
build.start();
return build;
}
}
在使用Curator连接是权限是使用authorization()方法。
3.使用连接
package com.ustcinfo.eop.eopapi.api.infoApi;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import com.ustcinfo.eop.eopapi.api.result.ResultCodeEnum;
import com.ustcinfo.eop.eopapi.api.result.ResultInfo;
import com.ustcinfo.eop.eopapi.entity.stats.ZookeeperProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
* ClassName: ServiceInfoApi
* Create: 2023/8/4 9:58
*
* @author SLIU
* @Version 1.0
* Description:
*/
@RequestMapping(value = "/infoApi/serviceInfoApi")
@Api(tags = "服务信息中心")
@RestController
public class ServiceInfoApi {
@Resource
private ZookeeperProperties wrapperZk;
@Resource
private CuratorFramework curatorFramework;
@PostMapping("/getCuratorClientNodeList")
@ApiOperation(value = "获取zookeeper所有子节点")
public ResultInfo getCuratorClientNodeList() throws Exception {
JSONArray resultArray = JSONUtil.createArray();
List<String> children = curatorFramework.getChildren().forPath("/eip");
for (String child : children) {
if (child.contains("MsgFlowMonitor")) {
List<String> children1 = curatorFramework.getChildren().forPath("/eip/" + child);
for (String s : children1) {
String str = new String(curatorFramework.getData().forPath("/eip/" + child + "/" + s)) ;
if (!StrUtil.isEmptyIfStr(str)) {
JSON json = JSONUtil.parse(str);
JSONObject obj = JSONUtil.createObj();
obj.set("id",json.getByPath("id",String.class));
obj.set("globalTotalCount",json.getByPath("globalTotalCount",Integer.class));
obj.set("globalExceptionCount",json.getByPath("globalExceptionCount",Integer.class));
long time = json.getByPath("startTime",Long.class);
long awayTime = System.currentTimeMillis() - time;
long day = awayTime / (24 * 60 * 60 * 1000);
long hour = (awayTime / (60 * 60 * 1000) - day * 24);
long min = ((awayTime / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s1 = (awayTime / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
obj.set("runTime", day + "天" + hour + "小时" + min + "分" + s1 + "秒");
resultArray.add(obj);
}
}
}
}
return ResultInfo.ok(resultArray);
}
}
获取子节点getChildren().forPath()
获取数据getData().forPath()