Spring Boot 知识笔记(集成zookeeper)
一、本机搭建zookeeper伪集群
1、下载安装包,复制三份
2、每个安装包目录下面新建一个data文件夹,用于存放数据目录
3、安装包的conf目录下,修改zoo.cfg配置文件
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=5 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=2 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. #这里要修改成刚才创建的目录 dataDir=D:/tools/zookeeper/zookeeper-3.4.6_1/data # the port at which the clients will connect #每个安装包的启动端口要不一样 clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1#server后面的两个端口也必须不同 server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:4888:5888 server.3=127.0.0.1:6888:7888
4、上面创建的data目录,新建myid文件,分别写入1,2,3,与配置文件中的server点后面的数字一致
5、进入bin目录,分别启动三个zk
6、在节点1,创建一个znode,并设置初始内容,如下
7、登录其他节点,查看这个znode的内容,如下,可以看到,不同节点的zk已经同步了znode的内容,这就是zk的核心特性,基于这个特性,可以对分布式应用程序实现服务不同、统一配置管理、统一服务命名,服务注册等功能。
二、使用java集成zk,实现znode的增删改查
package net.Eleven.demo.OtherTest; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import java.util.List; import java.util.concurrent.CountDownLatch;public class ZookeeperClient implements Watcher {private ZooKeeper zookeeper;private static final int SESSION_TIME_OUT=2000; //超时时间private CountDownLatch countDownLatch = new CountDownLatch(1);@Overridepublic void process(WatchedEvent watchedEvent) {if (watchedEvent.getState()== Event.KeeperState.SyncConnected){System.out.println("Watch received event");countDownLatch.countDown();}}/*** 连接zk* @param host* @throws Exception*/public void connectZookeeper(String host) throws Exception{zookeeper = new ZooKeeper(host,SESSION_TIME_OUT,this);countDownLatch.await();System.out.println("zookeeper.java connection success");}//创建节点public String createNode(String path,String data) throws Exception{return this.zookeeper.create(path,data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);}//获取所有节点public List<String> getChildren(String path) throws KeeperException,InterruptedException{List<String> children = zookeeper.getChildren(path,false);return children;}//获取节点上的数据public String getData(String path) throws KeeperException,InterruptedException{byte[] data = zookeeper.getData(path,false,null);if (data==null){return "";}return new String(data);}//设置节点信息public Stat setData(String path,String data) throws KeeperException, InterruptedException{Stat stat = zookeeper.setData(path, data.getBytes(), -1);return stat;}//删除节点public void deleteNode(String path) throws InterruptedException, KeeperException{zookeeper.delete(path, -1);}//关闭连接public void closeConnection() throws InterruptedException {if (zookeeper != null) {zookeeper.close();}}public boolean isConnected(){return zookeeper.getState() == ZooKeeper.States.CONNECTED;}public static void main(String[] args) throws Exception {ZookeeperClient zookeeper = new ZookeeperClient();zookeeper.connectZookeeper("127.0.0.1:2181");List<String> children = zookeeper.getChildren("/");System.out.println(children);zookeeper.createNode("/Eleven4","create node by java");System.out.println(zookeeper.getData("/Eleven4"));} }
转载于:https://www.cnblogs.com/Eleven-Liu/p/11086964.html
Spring Boot 知识笔记(集成zookeeper)相关推荐
- 超赞:不愧是阿里内部“Spring boot学习笔记”从头到尾,全是精华
spring boot为何会出现? 随着动态语言的流行(Ruby.Groovy. Scala. Node.js),Java 的开发显得格外的笨重:繁多的配置.低下的开发效率.复杂的部署流程以及第三方技 ...
- Spring Boot学习笔记-基础(2)
Spring Boot学习笔记-基础(2) Spring Boot 优点: – 快速创建独立运行的Spring项目以及与主流框架集成 – 使用嵌入式的Servlet容器,应用无需打成WAR包 – st ...
- Spring Boot学习笔记(1)
文章目录 Spring Boot学习笔记(1) Spring Boot 整合 JSP Spring Boot HTML Thymeleaf 常用语法 Spring Boot 数据校验 Spring B ...
- springboot thymeleaf配置_【程序源代码】Spring Boot 开发笔记web开发实战1
关键字:<Spring Boot 开发笔记>系列文章 各位亲爱的小伙伴:大家好! <Spring Boot 开发笔记>系列文章 这套笔记和源码是我自己在学习springboot ...
- Spring Boot 极简集成 Shiro
点击关注公众号,Java干货及时送达 1. 前言 Apache Shiro是一个功能强大且易于使用的Java安全框架,提供了认证,授权,加密,和会话管理. Shiro有三大核心组件: Subject: ...
- Spring Boot学习笔记-实践建言
2019独角兽企业重金招聘Python工程师标准>>> 本文延续<Spring Boot学习笔记-快速示例>,从开发指南中摘出一些实践经验可供参考.这也是笔者看到的眼前一 ...
- 超详细 Spring Boot 知识清单
2019独角兽企业重金招聘Python工程师标准>>> 超详细 Spring Boot 知识清单 在过去两三年的Spring生态圈,最让人兴奋的莫过于Spring Boot框架.或许 ...
- Spring Boot学习笔记-进阶(3)
文章目录 Spring Boot学习笔记-进阶(3) 一.Spring Boot与缓存 二.Spring Boot与消息 三.Spring Boot与检索 四.Spring Boot与任务 异步任务 ...
- Spring Boot知识清单
2019独角兽企业重金招聘Python工程师标准>>> Spring Boot知识清单 Spring Boot 应用本质上就是一个基于 Spring 框架的应用,它是 Spring ...
- 给你一份超详细 Spring Boot 知识清单
转载自 给你一份超详细 Spring Boot 知识清单 在过去两三年的Spring生态圈,最让人兴奋的莫过于Spring Boot框架.或许从命名上就能看出这个框架的设计初衷:快速的启动Spri ...
最新文章
- RabbitMQ超详细安装教程(Linux)
- 使用CEfSharp之旅(7)CEFSharp 拦截 http 请求 websocket 内容
- 什么是A记录、MX记录、CNAME记录
- 获取用户真实Ip地址
- linux下怎么查kill某个进程,Linux下查询进程PS或者杀死进程kill的小技巧
- 火狐浏览器添加MetaMask钱包和本地开启私有链开发
- 好像博问不能回复了,看似是某个脚本错误阻止了提交。可有此事?
- 效力微软 15 年的前员工解释 Windows 10 为什么问题如此多
- Ubuntu系统用户忘记密码
- TOMCAT下应用部署新法(/META-INF/context.xml)
- 在O(1)的时间内删除链表节点
- 【公众号】怎样写好公众号第一篇文章?
- 标准时间格式与时间戳的转化
- NASA锂电池容量增量数据处理
- 企业上云,打造数字经济新动能
- 电脑网页服务器拒绝连接失败,电脑服务器拒绝了连接怎么回事
- python立体爱心_css绘制各种各样的形状图形
- 17岁少年找黑客攻击航司系统获刑4年
- 毕业设计管理系统 数据库设计
- Base64在线解析,编码转化为图片
热门文章
- node的里html中写script报错,内置对象 · TypeScript 入门教程
- (11) python 使用baostock获取历史A股K线数据
- ECMAScript 学习笔记03
- j2ee和mysql怎么连接_Eclipse下配置j2ee开发环境及与MySQL数据库的连接
- bytes的json解析
- Win10+caffe+CUDA9.1+vs2013+Matlab2018b+GPU环境,跑通faster_rcnn-master
- Convolutional Networks(要点)
- python数字图像处理(4):图像数据类型及颜色空间转换
- Linux网络编程和套接字
- php必填参数校验,laravel请求参数校验方法