SpringBoot集成Curator实现Zookeeper基本操作
Curator介绍
Zookeeper是一个Apache开源的分布式的应用,为系统架构提供协调服务。从设计模式角度来审视:该组件是一个基于观察者模式设计的框架,负责存储和管理数据,接受观察者的注册,一旦数据的状态发生变化,Zookeeper就将负责通知已经在Zookeeper上注册的观察者做出相应的反应,从而实现集群中类似Master/Slave管理模式。ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
curator是对zookeeper原生api的封装,原生的api特别难用。
curator提供了流式编程风格,做的非常不错,是目前使用率最高的一个zookeeper框架。
curator它主要包含三个依赖(curator的依赖都已经放到maven仓库,你直接使用maven来构建它。对于大多数人来说,我们可能最常需要引入的是curator-recipes):
- curator-recipes:包含了curator实现的各项功能,如读写锁、互斥锁、队列等,依赖于framework和Client:http://curator.apache.org/curator-recipes/index.html
- curator-framework:包含了高层级的流式API,构建在Client之上如对节点的增删改查等:http://curator.apache.org/curator-recipes/index.html
- curator-client:Zookeeper的基础客户端实现,如连接、重试、超时处理等:http://curator.apache.org/curator-client/index.html
官方文档说明
Curator 2.x.x-兼容两个zk 3.4.x 和zk 3.5.x,Curator 3.x.x-兼容兼容zk 3.5。
因此为了不必要的麻烦,我们推荐使用2.x.x。
如果想在 Zookeeper 3.4.x 中使用Curator ,可以选择 4.2.x 版本的 curator。
curator 4.2.x 版本和 zookeeper 3.4.x 版本会在兼容模式下运行。
为了使用这种模式,你必须在版本管理工具中移除对 Zookeeper 的依赖,并且重新添加对 Zookeeper 的依赖。
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
</dependency>
SpringBoot使用Curator
引入相关依赖
<!--curator -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.13.0</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
application.yml定义连接属性
server:
port: 9999
#curator配置
curator:
connectString: 192.168.198.155:2182,192.168.198.155:2183,192.168.198.155:2184 # zookeeper 地址
retryCount: 1 # 重试次数
elapsedTimeMs: 2000 # 重试间隔时间
sessionTimeoutMs: 60000 # session超时时间
connectionTimeoutMs: 10000 # 连接超时时间
读取配置
CuratorConf.java
@Component
@ConfigurationProperties(prefix = "curator")
public class CuratorConf {
private int retryCount;
private int elapsedTimeMs;
private String connectString;
private int sessionTimeoutMs;
private int connectionTimeoutMs;
public int getRetryCount() {
return retryCount;
}
public void setRetryCount(int retryCount) {
this.retryCount = retryCount;
}
public int getElapsedTimeMs() {
return elapsedTimeMs;
}
public void setElapsedTimeMs(int elapsedTimeMs) {
this.elapsedTimeMs = elapsedTimeMs;
}
public String getConnectString() {
return connectString;
}
public void setConnectString(String connectString) {
this.connectString = connectString;
}
public int getSessionTimeoutMs() {
return sessionTimeoutMs;
}
public void setSessionTimeoutMs(int sessionTimeoutMs) {
this.sessionTimeoutMs = sessionTimeoutMs;
}
public int getConnectionTimeoutMs() {
return connectionTimeoutMs;
}
public void setConnectionTimeoutMs(int connectionTimeoutMs) {
this.connectionTimeoutMs = connectionTimeoutMs;
}
}
公用连接创建对象
@Configuration
public class ZkConfiguration {
@Autowired
private CuratorConf curatorConf;
/**
* 这里会自动调用一次start,请勿重复调用
*/
@Bean(initMethod = "start")
public CuratorFramework curatorFramework() {
return CuratorFrameworkFactory.newClient(
curatorConf.getConnectString(),
curatorConf.getSessionTimeoutMs(),
curatorConf.getConnectionTimeoutMs(),
new RetryNTimes(curatorConf.getRetryCount(), curatorConf.getElapsedTimeMs()));
}
}
编写测试类,实现各种基础操作
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ZkApp.class)
public class CuratorTest {
@Autowired
private CuratorFramework client;
// 测试连接
@Test
public void contextLoads() {
System.out.println(client.toString());
}
// 创建节点
@Test
public void createPath() throws Exception {
// 父节点不存在则创建
String path = client.create().creatingParentsIfNeeded().forPath("/zkblog/p1",
"Java博客".getBytes(StandardCharsets.UTF_8));
System.out.println(path);
byte[] data = client.getData().forPath("/zkblog/p1");
System.out.println(new String(data));
}
// 赋值,修改数据
@Test
public void setData() throws Exception {
int version = 0; // 当前节点的版本信息
Stat stat = new Stat();
client.getData().storingStatIn(stat).forPath("/zkblog/p1");
version = stat.getVersion();
// 如果版本信息不一致,说明当前数据被修改过,则修改失败程序报错
client.setData().withVersion(version).forPath("/zkblog/p1",
"Java林的博客".getBytes(StandardCharsets.UTF_8));
byte[] data = client.getData().forPath("/zkblog/p1");
System.out.println(new String(data));
}
// 查询节点
@Test
public void getPath() throws Exception {
// 查内容
byte[] data = client.getData().forPath("/zkblog/p1");
System.out.println(new String(data));
// 查状态
Stat stat = new Stat();
client.getData().storingStatIn(stat).forPath("/zkblog/p1");
System.out.println(JSON.toJSONString(stat, true));
}
// 删除节点
@Test
public void deletePath() throws Exception {
// deletingChildrenIfNeeded如果有子节点一并删除
// guaranteed必须成功比如网络抖动时造成命令失败
client.delete().guaranteed().deletingChildrenIfNeeded().inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
System.out.println("删除成功");
// { "path":"/javacui/p1","resultCode":0,"type":"DELETE"}
System.out.println(JSON.toJSONString(curatorEvent, true));
}
}).forPath("/zkblog/p1");
}
// 查询子节点
@Test
public void getPaths() throws Exception {
List<String> paths = client.getChildren().forPath("/zkblog");
for (String p : paths) {
System.out.println(p);
}
}
}
分类:
SpringBoot_bak
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!