一、本机搭建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); @Override
public 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"));
}
}

Spring Boot 知识笔记(集成zookeeper)的更多相关文章

  1. Spring Boot 知识笔记(整合Mybatis)

    一.pom.xml中添加相关依赖 <!-- 引入starter--> <dependency> <groupId>org.mybatis.spring.boot&l ...

  2. Spring Boot 知识笔记(配置文件)

    Spring boot 提供了两种常用的配置文件,properties和yml文件. 1.yml yml是YAML(YAML Ain't Markup Language)语言的文件,以数据为中心,比j ...

  3. Spring Boot 知识笔记(热部署)

    热部署原理: 使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart ClassLoader ...

  4. Spring Boot 知识笔记(创建maven项目、HTTP接口)

    一.使用Maven手工创建SpringBoot应用(IDEA) 1.  点击File——New——Project——Maven——Next,填写相关信息,创建项目. 2.  在pom.xml中添加相关 ...

  5. Spring Boot 知识笔记(thymleaf模板引擎)

    一.pom.xml中引入 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...

  6. Spring Boot 知识笔记(整合Redis)

    一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  7. Spring Boot 知识笔记(定时任务与异步)

    一.定时任务 1.启动类里面增加注入 @SpringBootApplication //@SpringBootApplication = @Configuration+@EnableAutoConfi ...

  8. Spring Boot 知识笔记(整合Mybatis续-补充增删改查)

    续上篇,补充数据库增删改查的其他场景. 一.Mapper中添加其他场景操作 package net.Eleven.demo.Mapper; import net.Eleven.demo.domain. ...

  9. Spring Boot 知识笔记(servlet、监听器、拦截器)

    一.通过注解自定义servlet package net.Eleven.demo.servlet; import javax.servlet.ServletException; import java ...

随机推荐

  1. 【Oracle】 RMAN命令汇总

    RMAN命令汇总 2013年写了关于RMAN命令的汇总,先转换为MD文档,温故而知新. 1.进入RMAN 进入本地数据库 [oracle@oracle-n1 ~]$ rman target / 进入远 ...

  2. 【UOJ#61】【UR #5】怎样更有力气(最小生成树)

    [UOJ#61][UR #5]怎样更有力气(最小生成树) 题面 UOJ 题解 最最最暴力的想法是把所有边给处理出来然后跑\(MST\). 考虑边权的情况,显然离线考虑,把么一天按照\(w_i\)进行排 ...

  3. 关于5G手机使用4G套餐扫盲

    有些人说换5G手机用4G套餐不用5G套餐可以享受最高 300 mbps 的签约速率.在此我来给你们科普下. 5G套餐分为 500 mbps 和 1000 mbps 两种.且都享受优先接入,顺序是 10 ...

  4. 从零开始实现放置游戏(六)——实现后台管理系统(4)Excel批量导入

    前面我们已经实现了在后台管理系统中,对配置数据的增删查改.但每次添加只能添加一条数据,实际生产中,大量数据通过手工一条一条添加不太现实.本章我们就实现通过Excel导入配置数据的功能.这里我们还是以地 ...

  5. 面试官常问的Nginx的几个问题

    1.什么是Nginx? Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器 Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3 ...

  6. rabbitmq 实现多个消费队列

    1.将消费程序复制重新生成一个. 2.channel.BasicQos(0, 1, false); 空闲的先消费

  7. python 检查站点是否可以访问

    最近碰到系统有时候会访问不了,想写一个程序来检测站点是不是可以访问的功能,正好在学python,于是写了一个方法来练练手,直接上代码. import urllib.request import smt ...

  8. 【微信小程序】动态设置图片大小

    我们都知道微信小程序的组件image是用来显示图片的,它有一下几个属性:1.src              图片资源地址2.mode          图片裁剪.缩放的模式3.binderror   ...

  9. element实现vue级联多选

    已经有大神完成element的改造github:https://github.com/webCoderJ/ele-multi-cascader#Attributes 已实践可用

  10. Jmeter吞吐量控制器

    吞吐量控制器 场景: 假如有两个业务分别是A, B在同一线程组内有10并发, 7个做A业务, 3个做B业务,吞吐量控制器比较推荐使用. 添加吞吐量控制器 ​ 用法1: Percent Executio ...