ubuntu18.04中搭建 ZooKeeper 单机环境

ubuntu18.04中搭建 ZooKeeper 单机环境,第1张

ubuntu18.04中搭建 ZooKeeper 单机环境

来源:微信公众号「编程学习基地」

文章目录
    • ubuntu18.04中搭建 ZooKeeper 单机环境
      • 1 - 下载安装包
      • 2 - 修改配置文件
      • 3 - 配置环境变量
      • 4 - 启动 ZooKeeper
        • 查看启动的所有参数
        • 启动服务:
      • 5 - 使用 ZooKeeper 客户端连接
      • 6 - ZooKeeper 服务的常用命令

ubuntu18.04中搭建 ZooKeeper 单机环境 1 - 下载安装包

ZooKeeper下载地址:https://archive.apache.org/dist/zookeeper,这里下载 3.4.14 版本的安装包,或者直接wget下载。

wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz

下载之后,上传至服务器,解压并移动到 /data/ 下。

tar -zxvf zookeeper-3.4.14.tar.gz
mv zookeeper-3.4.1 /data
cd /data/

查看zookeeper包

root@VM-16-5-ubuntu:/data# ls
zookeeper-3.4.14
2 - 修改配置文件

到解压后的配置目录下:

cd /data/zookeeper-3.4.14/conf/

total 12
-rw-r--r-- 1 root root  535 May  4  2020 configuration.xsl
-rw-r--r-- 1 root root 2712 May  4  2020 log4j.properties
-rw-r--r-- 1 root root  922 May  4  2020 zoo_sample.cfg

可以看到有个 zoo_sample.cfg 文件,我们需要基于它来配置本地服务的具体信息。

# 拷贝配置文件:
cp zoo_sample.cfg zoo.cfg

# 修改配置文件:
vim zoo.cfg

ps:注意这里修改的是拷贝的 zoo.cfg 文件

只需要修改 dataDir 参数,并添加 dataLogDir 参数,其他参数保持默认即可。

# 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.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper
dataLogDir=/data/zookeeper-3.4.14/data/log
# 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

配置参数说明:

  • tickTime:用于计算的基础时间单元。比如 session 超时:N*tickTime;
  • initLimit:用于集群,允许从节点连接并同步到 master 节点的初始化连接时间,以 tickTime 的倍数来表示;
  • syncLimit:用于集群, master 主节点与从节点之间发送消息,请求和应答时间长度(心跳机制);
  • dataDir:数据存储位置;
  • dataLogDir:日志目录;
  • clientPort:用于客户端连接的端口,默认 2181

说明:测试环境中,为了方便迁移 ZooKeeper 时,ZooKeeper 上的数据不丢失,建议上述目录与安装目录保持一致。

3 - 配置环境变量
vim /etc/profile

添加环境变量:

export ZOOKEEPER_HOME=/data/zookeeper-3.4.14
export PATH=$ZOOKEEPER_HOME/bin:$PATH

使得配置的环境变量生效:

source /etc/profile
4 - 启动 ZooKeeper

前提:系统中已经安装好了 JDK,否则 ZooKeeper 是启动不了的。

查看启动的所有参数
# cd /data/zookeeper-3.4.14/bin
# ./zkServer.sh 
ZooKeeper JMX enabled by default
Using config: /data/zookeeper-3.5.8/bin/../conf/zoo.cfg
Usage: ./zkServer.sh [--config ] {start|start-foreground|stop|restart|status|print-cmd}
启动服务:
# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /data/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
5 - 使用 ZooKeeper 客户端连接

ZooKeeper 的安装包中提供了本地客户端,可以通过命令行连接 ZooKeeper 服务。

# cd /data/zookeeper-3.4.14/bin

# 指定服务端地址,可以连接本地 ZK 服务,网络互通的情况下,也可以连接远程 ZK 服务
# 如果要连接本地 ZK 服务,可省略 -server 参数,直接用 ./zkCli.sh 即可:
./zkCli.sh -server 127.0.0.1

# 简单使用示例:
Welcome to ZooKeeper!
...
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1(CONNECTED) 0] ls /
[zookeeper]
[zk: 127.0.0.1(CONNECTED) 1] ls /zookeeper
[config, quota]
[zk: 127.0.0.1(CONNECTED) 2] 
6 - ZooKeeper 服务的常用命令

参考:https://www.cnblogs.com/shoufeng/p/15680031.html

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5665312.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存