当前位置: 首页 > news >正文

SpringBoot整合Zookeeper

引入Jar包

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.13</version>
</dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>4.1.0</version><exclusions><exclusion><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId></exclusion></exclusions>
</dependency>

Zookeeper配置

Zookeeper配置连接信息

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;@Configuration
public class ZookeeperConfig {@Bean("zookeeperClient")public CuratorFramework create() {RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy);client.start();return client;}
}

Zookeeper 的使用

节点监控

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;import com.plf.zookeeper.service.NodeService;@Component
@Order(1)
public class ZookeeperInit implements CommandLineRunner {private static final String ROOT_PATH = "/server";public TreeCache treeCache;@Autowiredprivate CuratorFramework zookeeperClient;@Autowiredprivate NodeService nodeService;@Overridepublic void run(String... args) throws Exception {if (!nodeService.isExitNode(ROOT_PATH)) {nodeService.createNode(ROOT_PATH);}// 监视某个节点的数据变化treeCache = new TreeCache(zookeeperClient, ROOT_PATH);treeCache.getListenable().addListener(new TreeCacheListener() {@Overridepublic void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {ChildData eventData = event.getData();switch (event.getType()) {case NODE_ADDED:System.out.println(eventData.getPath() + "节点添加" + eventData.getPath() + "\t添加数据为:"+ new String(eventData.getData()));break;case NODE_UPDATED:System.out.println(eventData.getPath() + "节点数据更新\t更新数据为:" + new String(eventData.getData())+ "\t版本为:" + eventData.getStat().getVersion());break;case NODE_REMOVED:System.out.println(eventData.getPath() + "节点被删除");break;default:break;}}});// 启动监视器对象treeCache.start();}
}

节点新增

import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class NodeService {@Autowiredprivate CuratorFramework zookeeperClient;public void createNode(String nodePath) {try {zookeeperClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(nodePath);} catch (Exception e) {e.printStackTrace();}}public boolean isExitNode(String path) {try {Stat stat = zookeeperClient.checkExists().forPath(path);if(stat == null) {return false;}return true;} catch (Exception e) {e.printStackTrace();}return false;}
}

还有很多其他的方法可以自行扩展。

http://www.lryc.cn/news/98947.html

相关文章:

  • Java和C#选哪个?
  • 首批!棱镜七彩通过汽车云-汽车软件研发效能成熟度模型能力评估
  • 【Docker】容器的数据卷
  • CentOS7安装jenkins
  • Hadoop的伪分布式安装方法
  • iOS 应用上架的步骤和工具简介
  • 【信号去噪】基于马氏距离和EDF统计(IEE-TSP)的基于小波的多元信号去噪方法研究(Matlab代码实现)
  • 智驾SOC shell编程应用实战笔记
  • C#实现计算题验证码
  • 【lesson6】Linux下:第一个小程序,进度条代码
  • PostgreSQL实战-pg13主从复制切换测试
  • 如何使用OpenCV库进行图像检测
  • Mybatis中where 1=1 浅析
  • element中el-input组件限制输入条件(数字、特殊字符)
  • 会议OA项目之会议发布(一)
  • 【Android】对象为null的一个原因:在方法内部,重新创建了一个新的对象并将其赋值给原对象,但这并不会改变原始的原对象的引用
  • macbook 软件iMovie for Mac(专业视频剪辑工具)中文版
  • web APIs-练习一
  • AX88179A千兆网卡芯片,支持switch联网
  • Pytorch个人学习记录总结 05
  • windows部署安装redis安装教程
  • Jmeter post请求传参问题
  • 对Windows应用程序进行代码签名
  • 2022 China Open Source Report
  • postgresql|数据库|启动数据库时报错:FATAL: could not map anonymous shared memory的解决
  • 数字签名与数字证书
  • 基于Jquery EasyUI JSZip FileSaver的简单使用
  • git远程一个分支对应本地两个分支推送
  • SpringCloud学习—Hystrix:服务熔断
  • 8款常用系统镜像烧录软件