安装分布式的 ZooKeeper:
1.下载 ZooKeeper 压缩包,并解压到指定目录:
wget https://www.apache.org/dyn/closer.cgi/zookeeper/zookeeper-3.6.3/apache-zookeeper-3.6.3-bin.tar.gz
tar -zxvf apache-zookeeper-3.6.3-bin.tar.gz -C /opt/
2.配置 ZooKeeper:
cd /opt/apache-zookeeper-3.6.3-bin/conf
cp zoo_sample.cfg zoo.cfg
修改 zoo.cfg 配置文件:
tickTime=2000
dataDir=/opt/zookeeper/data
clientPort=2181
initLimit=5
syncLimit=2
server.1=192.168.1.1:2888:3888
server.2=192.168.1.2:2888:3888
server.3=192.168.1.3:2888:3888
其中,tickTime 表示 ZooKeeper 使用的基本时间单位,单位是毫秒;dataDir 表示 ZooKeeper 存储数据的目录;clientPort 表示 ZooKeeper 服务监听的端口;initLimit 和 syncLimit 表示 ZooKeeper 与客户端之间的最大延迟;server.X 表示 ZooKeeper 集群中的每个节点的 IP 地址和端口号。
3.启动 ZooKeeper:
/opt/apache-zookeeper-3.6.3-bin/bin/zkServer.sh start
或者使用自定义的启动脚本:
#!/bin/bash
ZOOKEEPER_HOME=/opt/apache-zookeeper-3.6.3-bin
case $1 in
start)
echo "Starting ZooKeeper ..."
$ZOOKEEPER_HOME/bin/zkServer.sh start
;;
stop)
echo "Stopping ZooKeeper ..."
$ZOOKEEPER_HOME/bin/zkServer.sh stop
;;
restart)
echo "Restarting ZooKeeper ..."
$ZOOKEEPER_HOME/bin/zkServer.sh restart
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit 0
然后使用以下命令启动、停止或重启 ZooKeeper:
1.安装 expect 工具:
yum install expect -y
2.编写远程一键启停的脚本:
#!/bin/bash
# 服务器列表
servers=(
"192.168.1.1"
"192.168.1.2"
"192.168.1.3"
)
# ZooKeeper 安装目录
zookeeper_home=/opt/apache-zookeeper-3.6.3-bin
# ZooKeeper 启动脚本
zookeeper_start_script=$zookeeper_home/bin/zkServer.sh
# ZooKeeper 停止脚本
zookeeper_stop_script=$zookeeper_home/bin/zkServer.sh
# ZooKeeper 配置文件
zookeeper_config_file=$zookeeper_home/conf/zoo.cfg
# ZooKeeper 客户端端口
zookeeper_client_port=2181
# ZooKeeper 节点数
zookeeper_node_count=3
# 启动 ZooKeeper
function start_zookeeper() {
for server in "${servers[@]}"; do
echo "Starting ZooKeeper on $server ..."
expect -c "
spawn ssh root@$server
expect {
"yes/no" { send "yesr"; exp_continue }
"password:" { send "passwordr" }
}
expect "#*"
send "source /etc/profile && $zookeeper_start_script startr"
expect "#*"
send "exitr"
"
done
}
# 停止 ZooKeeper
function stop_zookeeper() {
for server in "${servers[@]}"; do
echo "Stopping ZooKeeper on $server ..."
expect -c "
spawn ssh root@$server
expect {
"yes/no" { send "yesr"; exp_continue }
"password:" { send "passwordr" }
}
expect "#*"
send "source /etc/profile && $zookeeper_stop_script stopr"
expect "#*"
send "exitr"
"
done
}
# 列出 ZooKeeper 节点
function list_zookeeper_nodes() {
echo "ZooKeeper Nodes:"
for i in $(seq 1 $zookeeper_node_count); do
node=$(grep -E "^server.$i=" $zookeeper_config_file | awk -F '=' '{print $2}')
echo "$node"
done
}
# 显示帮助信息
function show_help() {
echo "Usage: $0 "
}
# 处理命令行参数
case $1 in
start)
start_zookeeper
;;
stop)
stop_zookeeper
;;
list)
list_zookeeper_nodes
;;
*)
show_help
;;
esac
在脚本中,定义了服务器列表、ZooKeeper 安装目录、启动脚本、停止脚本、配置文件、客户端端口和节点数等变量。然后,使用 expect 工具远程登录到每个服务器上,执行相应的启动、停止或查询命令。
可以使用 ssh 来编写分布式 ZooKeeper 的一键启停脚本。以下是一个示例脚本:
#!/bin/bash
# ZooKeeper 节点列表,格式为 "hostname:port"
NODE_LIST=(
"192.168.1.1:2181"
"192.168.1.2:2181"
"192.168.1.3:2181"
)
# ZooKeeper 安装目录
ZOOKEEPER_HOME=/opt/zookeeper
# ZooKeeper 启动脚本
ZOOKEEPER_START_SCRIPT=$ZOOKEEPER_HOME/bin/zkServer.sh
# ZooKeeper 配置文件
ZOOKEEPER_CONFIG_FILE=$ZOOKEEPER_HOME/conf/zoo.cfg
# 一键启动 ZooKeeper
function start_zookeeper() {
for node in "${NODE_LIST[@]}"; do
echo "Starting ZooKeeper on $node ..."
ssh -n $node "$ZOOKEEPER_START_SCRIPT start"
done
}
# 一键停止 ZooKeeper
function stop_zookeeper() {
for node in "${NODE_LIST[@]}"; do
echo "Stopping ZooKeeper on $node ..."
ssh -n $node "$ZOOKEEPER_START_SCRIPT stop"
done
}
# 列出 ZooKeeper 节点
function list_zookeeper_nodes() {
echo "ZooKeeper Nodes:"
for node in "${NODE_LIST[@]}"; do
echo "$node"
done
}
# 显示帮助信息
function show_help() {
echo "Usage: $0 "
}
# 处理命令行参数
case $1 in
start)
start_zookeeper
;;
stop)
stop_zookeeper
;;
list)
list_zookeeper_nodes
;;
*)
show_help
;;
esac
在脚本中,定义了 ZooKeeper 节点列表、安装目录、启动脚本、配置文件等变量。然后,使用 ssh 远程登录到每个节点上,执行相应的启动、停止或查询命令。
使用该脚本,可以轻松地一键启动、停止或查询分布式 ZooKeeper 集群的状态。例如,要启动 ZooKeeper,可以执行以下命令:
./zookeeper.sh start
要停止 ZooKeeper,可以执行以下命令:
./zookeeper.sh stop
要列出 ZooKeeper 节点,可以执行以下命令:
./zookeeper.sh list
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
了解OpenTelemetry的朋友应该知道,为了将率属于同一个请求的多个操作(Span)串起来,上游应用会生成一个唯一的TraceId。在进行跨应用的Web调用时,这个TraceId和代表跟踪操作标识的SpanID一并发给目标应用,W3C还专门指定了一份名为…