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); @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)的更多相关文章
- Spring Boot 知识笔记(整合Mybatis)
一.pom.xml中添加相关依赖 <!-- 引入starter--> <dependency> <groupId>org.mybatis.spring.boot&l ...
- Spring Boot 知识笔记(配置文件)
Spring boot 提供了两种常用的配置文件,properties和yml文件. 1.yml yml是YAML(YAML Ain't Markup Language)语言的文件,以数据为中心,比j ...
- Spring Boot 知识笔记(热部署)
热部署原理: 使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart ClassLoader ...
- Spring Boot 知识笔记(创建maven项目、HTTP接口)
一.使用Maven手工创建SpringBoot应用(IDEA) 1. 点击File——New——Project——Maven——Next,填写相关信息,创建项目. 2. 在pom.xml中添加相关 ...
- Spring Boot 知识笔记(thymleaf模板引擎)
一.pom.xml中引入 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...
- Spring Boot 知识笔记(整合Redis)
一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Spring Boot 知识笔记(定时任务与异步)
一.定时任务 1.启动类里面增加注入 @SpringBootApplication //@SpringBootApplication = @Configuration+@EnableAutoConfi ...
- Spring Boot 知识笔记(整合Mybatis续-补充增删改查)
续上篇,补充数据库增删改查的其他场景. 一.Mapper中添加其他场景操作 package net.Eleven.demo.Mapper; import net.Eleven.demo.domain. ...
- Spring Boot 知识笔记(servlet、监听器、拦截器)
一.通过注解自定义servlet package net.Eleven.demo.servlet; import javax.servlet.ServletException; import java ...
随机推荐
- springboot项目POM文件第一行报错 Unknown Error
改成 war 不错了,但是打包麻烦 pom 文件报错 UnKnown Error第一次碰到这个问题,花了几个小时才解决,除了UnKnown 没有任何提示.网上搜的大部分情况都不是我遇到的. 还是没有解 ...
- windows上mysql解压缩版本、centos上rpm方式的安装、初始化等
一.windows版本: https://dev.mysql.com/doc/refman/5.7/en/windows-install-archive.html 启动或者暂停mysql服务: htt ...
- ssh工具推荐MobaXterm 可能是你遇到过的比较出色的一款
之前一直用xshell,现在推荐一个更好用的工具. 一站式的解决你的需求,而且画风个人也比较喜欢,而且随便一百度就能找得到green PJ 的版本
- 【译】.NET Core 是 .NET 的未来
为什么要翻译咧,.NET 5 都宣布在 .NET Core 之后发布咯,何不再给 .NET Core 打打鸡血,我这个 .NET Core 的死忠粉. 原文:<.NET Core is the ...
- VS2019已还原ReSharper的功能
本文只谈论 ReSharper 的那些常用功能中,Visual Studio 2019 能还原多少,主要提供给那些正在考虑不使用 ReSharper 插件的 Visual Studio 用户作为参考. ...
- Python【day 14】sorted函数、filter函数和map函数的区别
sorted函数.filter函数和map函数的区别1.作用 前者用于排序, 中者用于筛选, 后者用于返回值(不是特定的筛选或者排序)2.写法 前者 sorted(iterable,key=自定义函数 ...
- 微信小程序入门小结
- git报错 - remote: HTTP Basic: Access denied
十年河东,十年河西,莫欺少年穷 学无止境,精益求精 git 拉取代码报: remote: HTTP Basic: Access denied,这是因为你的GIT密码修改后,需要重新认证授权,那么怎么操 ...
- docker操作命令大全和后台参数
一.命令行 可以通过运行 docker ,或者 docker help 命令得到命令行的帮助信息(我们以 CentOS 为操作环境为例): [root@iz2ze2bn5x2wqxdeq65wlpz ...
- flink 并行计数器实现
1.flink实现计数器的灵感来源于Hadoop的MapReduce计算框架里的理念. flink通过实现Accumulator接口实现并行计数.并行管理是由flink实现的. public inte ...