1.环境准备:
准备好三台机器,分别配置为 centos7 操作系统 ip分别为:
172.16.182.137
172.16.182.138
172.16.182.139
2. zookeeper环境搭建
1. 下载安装包:
wget https://dlcdn.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz --no-check-certificate
然后解压:
tar -xvzf apache-zookeeper-3.7.0-bin.tar.gz
进入目录
cd apache-zookeeper-3.7.0-bin/
cd conf
mv zoo_sample.cfg zoo.cfg
配置修改:
添加配置:
server.1=172.16.182.137:2888:3888
server.2=172.16.182.138:2888:3888
server.3=172.16.182.139:2888:3888
# 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
# 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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
server.1=172.16.182.137:2888:3888
server.2=172.16.182.138:2888:3888
server.3=172.16.182.139:2888:3888
配置文件改好后,我们还需要新建 myid
文件表示这个节点是 server.x
的哪个:到我们上面配置的 dataDir
目录下 (/tmp/zookeper
) ,新建一个 myid
的 文件,写入 server.x
的 x
:
#172.16.182.137 第一台操作:
mkdir -p /tmp/zookeeper
echo 1 > /tmp/zookeeper/myid
#172.16.182.138 第二台操作:
mkdir -p /tmp/zookeeper
echo 2> /tmp/zookeeper/myid
#172.16.182.139 第三台操作:
mkdir -p /tmp/zookeeper
echo 3 > /tmp/zookeeper/myid
每个结点都执行启动命令:
./bin/zkServer.sh start
启动后使用jps查看进程:
如果里面有
QuorumPeerMain 则表示启动成功了,其中 QuorumPeerMain就是zk的进程了
三个节点都起来了,可以查看集群的状态,分别在三个机器上执行查看状态命令:
# 137
[root@kafkanode1 apache-zookeeper-3.7.0-bin]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/kafkaLearn/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
#138
./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/kafkaLearn/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
#139
./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/kafkaLearn/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
通过查看状态发现 139这台是leader,其它两台是follower
最后使用zkCli 连接到主节点(139)上,验证是否可以正常使用:
[zk: 172.16.182.139:2181(CONNECTED) 1] create /liu niu
Created /liu
[zk: 172.16.182.139:2181(CONNECTED) 2] ls /
[admin, brokers, cluster, config, consumers, controller, controller_epoch, feature, isr_change_notification, latest_producer_id_block, liu, log_dir_event_notification, test, zookeeper]
[zk: 172.16.182.139:2181(CONNECTED) 3] get /liu
niu
确定实际可用。
总结:
Zookeeper是一个开源的分布式协调服务,目前由Apache进行维护,Zookeeper可以用于实现分布式系统中常见的发布、订阅,负载均衡,命令服务,分布式协调/通知、集群管理、Master选举、分布式锁和分布式队列等功能。它具有以下特性:
- 顺序一致性: 从客户端发起事务请求,最终都会严格按照其发起顺序被应用到zookeeper中
- 原子性:所有事务请求的处理结果再整个集群中的所有机器上是一致的;不存在部分机器应用了该事务,而一部分没有应用的情况;
- 单一视图: 一旦服务端成功应用了一个事务,则其引起的改变会一直保留,直到被另外一个事务所更改;
- 可靠性:一旦服务端成功应用了一个事务,则其引起的改变会一直保留,直到被另外一个事务所更改
- 实时性:一旦一个事务被成功应用后,Zookeeper可以保证客户端立即可以读到这个事务变更后的最新状态的数据。
Zookeeper致力于为那些高吞吐的大型分布式系统提供一个高性能、高可用、且严格具有顺序访问控制能力的分布式协调服务。它具有以下四个目标:
目标1 :简单的数据类型,
目标2: 构架集群
目标3:顺序访问
目标4:高性能且高可用
3. Kafka集群的搭建
1. 官网下载:
wget https://downloads.apache.org/kafka/3.0.0/kafka_2.12-3.0.0.tgz
2.解压:
tar -xvzf kafka_2.12-3.0.0.tgz
cd kafka_2.12-3.0.0/
vim config/server.properties
编辑配置:
broker.id
这个是必须要改的,且必须是个整形。需要保持唯一 。这里137对应 0,138对应1,139对应2-
listeners
,这个也是必填的,默认是没有配置这一项的,注释掉了- 238节点配置 :
listeners=PLAINTEXT://172.16.182.137:9092
- 239节点配置 :
listeners=PLAINTEXT://172.16.182.138:9092
- 240节点配置 :
listeners=PLAINTEXT://172.16.182.139:9092
- 238节点配置 :
zookeeper.connect
这个是zk的集群地址,可以根据自己的情况修改我这里设置为:- zookeeper.connect=172.16.182.137:2181,172.16.182.138:2181,172.16.182.139:2181
3.启动
三台实例分别执行
bin/kafka-server-start.sh config/server.properties
4.测试执行状态:
执行 : bin/kafka-topics.sh --create --bootstrap-server 172.16.182.137:9092 --replication-factor 3 --partitions 1 --topic repl-test
返回: Created topic repl-test.
执行: bin/kafka-topics.sh --describe --bootstrap-server 172.16.182.137:9092 --topic repl-test
返回:
Topic: repl-test PartitionCount: 1 ReplicationFactor: 3 Configs: segment.bytes=1073741824
#发送消息测试:
执行: bin/kafka-console-producer.sh --bootstrap-server 172.16.182.137:9092 --topic repl-test-topic
// 发送的消息:
>test message 1
>test message 2
#接收消息测试:
> bin/kafka-console-consumer.sh --bootstrap-server 172.16.182.137:9092 --from-beginning --topic repl-test
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
// 接收到的消息:
test message 1
test message 2
总结:
Kafka是最初由Linkedin公司开发,是一个分布式、分区的、多副本的、多订阅者,基于zookeeper协调的分布式日志系统(也可以当做MQ系统),常见可以用于web/nginx日志、访问日志,消息服务等等,Linkedin于2010年贡献给了Apache基金会并成为顶级开源项目。
主要应用场景是:日志收集系统和消息系统。
Kafka主要设计目标如下:
- 以时间复杂度为O(1)的方式提供消息持久化能力,即使对TB级以上数据也能保证常数时间的访问性能。
- 高吞吐率。即使在非常廉价的商用机器上也能做到单机支持每秒100K条消息的传输。
- 支持Kafka Server间的消息分区,及分布式消费,同时保证每个partition内的消息顺序传输。
- 同时支持离线数据处理和实时数据处理。
- Scale out:支持在线水平扩展