搭建分布式架构 --ZooKeeper注册中心安装
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建分布式架构 --ZooKeeper注册中心安装相关的知识,希望对你有一定的参考价值。
ZooKeeper是一个分布式开源框架,提供了协调分布式应用的基本服务,它向外部应用暴露一组通用服务——分布式同步(Distributed Synchronization).命名服务(Naming Service).集群维护(Group Maintenance)等,简化分布式应用协调及其管理的难度,提供高性能的分布式服务。ZooKeeper本身可以以Standalone模式安装运行,不过它的长处在于通过分布式ZooKeeper集群(一个Leader,多个Follower),基于一定的策略来保证ZooKeeper集群的稳定性和可用性,从而实现分布式应用的可靠性。(获取【下载地址】 )
这里先讲解一下单节点安装(后期会提供集群安装模式),由于直接进行操作,故下面记录详细步骤:
ZooKeeper Standalone模式
1.修改操作系统的/etc/hosts 文件中添加
# zookeeper servers (主机IP jeesz-provider-01)
[root@cloud ~]# vi /etc/hosts
xxx.xxx.xxx.xxx jeesz-provider-01
2.到 http://apache.fayea.com/zookeeper/下载 zookeeper-3.4.6
[root@cloud ~]# wget http://apache.fayea.com/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
由于我提前下载了并上传到了我指定的目录:/home/jeesz
3. 解压 zookeeper 安装包
[root@cloud jeesz] tar -zxvf zookeeper-3.4.6.tar.gz
4. 在/home/jeesz/zookeeper-3.4.6 目录下创建以下目录
[root@cloud ~]# cd /home/jeesz/zookeeper-3.4.6
[root@cloud zookeeper-3.4.6]# mkdir data
[[email protected] zookeeper-3.4.6]# mkdir logs
5.将 zookeeper-3.4.6/conf 目录下的 zoo_sample.cfg 文件拷贝一份,命名为为zoo.cfg
[[email protected] conf]# cp zoo_sample.cfg zoo.cfg
6.修改 zoo.cfg 配置文件
[[email protected] conf]# vi 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.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/home/jeesz/zookeeper-3.4.6/data
dataLogDir=/home/jeesz/zookeeper-3.4.6/logs
# the port at which the clients will connect
clientPort=2181
#2888,3888 are election port
server.1=jeesz-provider-01:2888:3888
7.在/home/jeesz/zookeeper-3.4.6/data 下创建 myid 文件
编辑 myid 文件,并在对应的 IP 的机器上输入对应的编号。如在 zookeeper 上,myid
文件内容就是 1。如果只在单点上进行安装配置,那么只有一个 server.1。
[[email protected] data]# vi myid
1
8.jeesz 用户下修改 vi /home/jeesz/.bash_profile,增加 zookeeper 配置
[[email protected] ~] vi /home/jeesz/.bash_profile
# zookeeper env
export ZOOKEEPER_HOME=/home/jeesz/zookeeper-3.4.6
export PATH=$ZOOKEEPER_HOME/bin:$PATH
使配置文件生效
[[email protected] ~]# source /home/jeesz/.bash_profile
9.在防火墙中打开要用到的端口 2181.2888.3888
切换到 root 用户权限,执行以下命令:
[[email protected] ~]# chkconfig iptables on
[[email protected] ~]# service iptables start
编辑/etc/sysconfig/iptables
[[email protected] ~]# vi /etc/sysconfig/iptables
增加以下 3 行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2181 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2888 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3888 -j ACCEPT
重启防火墙:
[[email protected] ~] service iptables restart
查看防火墙端口状态:
[[email protected]]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
6 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:8080
7 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:2181
8 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:2888
9 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:3888
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
10.启动并测试 zookeeper
(1) 到/home/jeesz/zookeeper-3.4.6/bin 目录中执行:
[[email protected] bin]# zkServer.sh start
(2) 输入 jps 命令查看进程:
[[email protected] zookeeper-3.4.6]# jps
48064 Jps
47348 QuorumPeerMain
46826 Bootstrap
其中,QuorumPeerMain 是 zookeeper 进程,启动正常
(3) 查看状态:
[[email protected] bin]# zkServer.sh status
JMX enabled by default
Using config: /home/jeesz/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: standalone
(4) 查看 zookeeper 服务输出信息:
由于服务信息输出文件在/home/jeesz/zookeeper-3.4.6/bin/zookeeper.out
[[email protected] bin]# tail -f zookeeper.out
2016-02-16 02:20:14,500 [myid:] - INFO [main:[email protected]] - Reading configuration from: /home/jeesz/zookeeper-3.4.6/bin/../conf/zoo.cfg
2016-02-16 02:20:14,700 [myid:] - ERROR [main:[email protected]] - Invalid configuration, only one server specified (ignoring)
......
11.停止 zookeeper 进程
[[email protected] bin]# zkServer.sh stop
12.配置 zookeeper 开机使用 jeesz 用户启动
编辑/etc/rc.local 文件,加入:
[[email protected] bin]# vi /etc/rc.local
su - jeesz -c ‘/home/jeesz/zookeeper-3.4.6/bin/zkServer.sh start‘
以上是关于搭建分布式架构 --ZooKeeper注册中心安装的主要内容,如果未能解决你的问题,请参考以下文章