zookeeper学习
简介
ZooKeeper是一个分布式的用作微服务开发的注册中心,为实现各个功能块的调用监控作用。简单说就是只要有一边作为提供者注册进注册中心,消费者也注册进入注册中心,znode设置Watcher来监控节点值的变化,它还能够实现调度的作用
安装
在官网里下好包,有Windows和Linux版本的,下好了之后进行解压,在conf下zoo_sample.cfg直接复制再重写改为zoo.cfg
配置文件修改为
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
#dataDir=/tmp/zookeeper
dataDir=../data
# the port at which the clients will connect
clientPort=2181
启动
在安装的结构目录下的bin下执行zkServer.cmd启动
看到了这样的字段则启动成功
再启动客户端
操作
create
create [-s] [-e] path data acl
普通创建:不带有-s、-e参数
-s:含有序列
-e:临时(重启或者超时消失)
set
设置节点的具体值
set 节点 value值
get
获得节点的值
delete
可以删除指定znode,当该znode拥有子znode时,必须先删除其所有子znode,否则操作将失败。
rmr
rmr命令可用于代替delete命令,rmr是一个递归删除命令,如果发生指定节点拥有子节点时,rmr命令会首先删除子节点
Java
导入环境依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
@EnableDiscoveryClient //该注解用于向使用consul或者zookeeper作为注册中心时注册服务
实际
在很多的微服务开发中有很多的实现注册中心功能的组件,zookeeper为其中的一种来实现服务注册