centos7 Kafka部署(单机版)

centos7 Kafka部署(单机版)

一、概念:

Kafka主要应用场景是:日志收集系统和消息系统。
Kafka是最初由Linkedin公司开发,是一个分布式、分区的、多副本的、多订阅者,基于zookeeper协调的分布式日志系统(也可以当做MQ系统)。
采用zookeeper对集群中的broker和comsumer进行管理;可以注册topic到zookeeper上,通过zookeeper的协调机制,producer保存对应的topic的broker信息,可以随机或者轮询发送到broker上,produce可以基于语义指定分片,消息发送到broker的某个分片上。

Kafka主要设计目标如下:

1、以时间复杂度方式提供消息持久化能力,即使对TB级以上数据也能保证常数时间的访问性能。
2、高吞吐率。即使在非常廉价的商用机器上也能做到单机支持每秒100K条消息的传输。
3、支持Kafka Server间的消息分区,及分布式消费,同时保证每个partition内的消息顺序传输。
4、同时支持离线数据处理和实时数据处理。
5、Scale out:支持在线水平扩展。

MQ消息队列简介:

MQ消息队列中间件(Message Queue)是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构。
主流消息队列包括:RabbitMQ、ActiveMQ、RocketMQ、ZeroMQ、Kafka、IBM WebSphere等。

Kafka队列优点:

1、解耦

在项目启动之初来预测将来项目会碰到什么需求,是极其困难的。消息系统在处理过程中间插入了一个隐含的、基于数据的接口层,两边的处理过程都要实现这一接口。这允许你独立的扩展或修改两边的处理过程,只要确保它们遵守同样的接口约束。

2、冗余(副本)

有些情况下,处理数据的过程会失败。除非数据被持久化,否则将造成丢失。消息队列把数据进行持久化直到它们已经被完全处理,通过这一方式规避了数据丢失风险。许多消息队列所采用的"插入-获取-删除"范式中,在把一个消息从队列中删除之前,需要你的处理系统明确的指出该消息已经被处理完毕,从而确保你的数据被安全的保存直到你使用完毕。

3、扩展性

因为消息队列解耦了你的处理过程,所以增大消息入队和处理的频率是很容易的,只要另外增加处理过程即可。不需要改变代码、不需要调节参数。扩展就像调大电力按钮一样简单。

4、灵活性&峰值处理能力

在访问量剧增的情况下,应用仍然需要继续发挥作用,但是这样的突发流量并不常见;如果为以能处理这类峰值访问为标准来投入资源随时待命无疑是巨大的浪费。使用消息队列能够使关键组件顶住突发的访问压力,而不会因为突发的超负荷的请求而完全崩溃。

5、可恢复性

系统的一部分组件失效时,不会影响到整个系统。消息队列降低了进程间的耦合度,所以即使一个处理消息的进程挂掉,加入队列中的消息仍然可以在系统恢复后被处理。

6、顺序保证

在大多使用场景下,数据处理的顺序都很重要。大部分消息队列本来就是排序的,并且能保证数据会按照特定的顺序来处理。Kafka保证一个Partition内的消息的有序性。

7、缓冲

在任何重要的系统中,都会有需要不同的处理时间的元素。例如,加载一张图片比应用过滤器花费更少的时间。消息队列通过一个缓冲层来帮助任务最高效率的执行———写入队列的处理会尽可能的快速。该缓冲有助于控制和优化数据流经过系统的速度。

8、异步通信

很多时候,用户不想也不需要立即处理消息。消息队列提供了异步处理机制,允许用户把一个消息放入队列,但并不立即处理它。想向队列中放入多少消息就放多少,然后在需要的时候再去处理它们。

两种主要的消息传递模式:

1、点对点传递模式:

在点对点消息系统中,消息持久化到一个队列中。此时,将有一个或多个消费者消费队列中的数据。但是一条消息只能被消费一次。当一个消费者消费了队列中的某条数据之后,该条数据则从消息队列中删除。该模式即使有多个消费者同时消费数据,也能保证数据处理的顺序。生产者发送一条消息到queue,只有一个消费者能收到。

2、发布-订阅模式(kafka默认模式):

在发布-订阅消息系统中,消息被持久化到一个topic中。与点对点消息系统不同的是,消费者可以订阅一个或多个topic,消费者可以消费该topic中所有的数据,同一条数据可以被多个消费者消费,数据被消费后不会立马删除。在发布-订阅消息系统中,消息的生产者称为发布者,消费者称为订阅者。发布者发送到topic的消息,只有订阅了topic的订阅者才会收到消息。

二、centos7 Kafka部署(单机版)

官网
zookeeper:http://www.apache.org/dyn/closer.cgi/zookeeper/
kafka:http://kafka.apache.org/downloads

1、先安装部署zookeeper

Kafka默认是由内置的Zookeeper的,如果使用内置的zookeeper的话,可以直接采用默认集成的,此处我们选择手工自定义部署。

1)、先安装jdk,这里直接使用清华源里的AdoptOpenJDK

wget -c https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/16/jdk/x64/linux/OpenJDK16U-jdk_x64_linux_openj9_16.0.1_9_openj9-0.26.0.tar.gz
wget -c https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/16/jre/x64/linux/OpenJDK16U-jre_x64_linux_openj9_16.0.1_9_openj9-0.26.0.tar.gz
tar -xf OpenJDK16U-jdk_x64_linux_openj9_16.0.1_9_openj9-0.26.0.tar.gz
tar -xf OpenJDK16U-jre_x64_linux_openj9_16.0.1_9_openj9-0.26.0.tar.gz
mv jdk-16.0.1+9 /usr/local/jdk
mv jdk-16.0.1+9-jre /usr/local/jdk/jre

cat>>/etc/profile<<EOF
JAVA_HOME=/usr/local/jdk
JRE_HOME=/usr/local/jdk/jre
CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar
JAVA_OPTS="-Xmx1024M -Xms1024M -Xmn384M -XX:PermSize=512m -XX:MaxPermSize=2048m"
PATH=$PATH:\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin
EOF

source /etc/profile
java -version

openjdk version "16.0.1" 2021-04-20
OpenJDK Runtime Environment AdoptOpenJDK-16.0.1+9 (build 16.0.1+9)
Eclipse OpenJ9 VM AdoptOpenJDK-16.0.1+9 (build openj9-0.26.0, JRE 16 Linux amd64-64-Bit Compressed References 20210421_23 (JIT enabled, AOT enabled)
OpenJ9   - b4cc246d9
OMR      - 162e6f729
JCL      - cea22090ecf based on jdk-16.0.1+9)

2)、下载、解压、部署、运行

wget -c https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/apache-zookeeper-3.6.3-bin.tar.gz
#Tar解压Zookeeper软件包;
tar -xf apache-zookeeper-3.6.3-bin.tar.gz
#创建Zookeeper部署目录;
mkdir -p /usr/local/zookeeper/
#将解压程序移动至ZK部署目录;
mv apache-zookeeper-3.6.3-bin/* /usr/local/zookeeper/
#查看ZK是否部署成功;
ls -l /usr/local/zookeeper/
#CP拷贝默认模板配置文件;
cd /usr/local/zookeeper/conf/
\cp zoo_sample.cfg zoo.cfg
#启动ZK软件服务;
/usr/local/zookeeper/bin/zkServer.sh start
#查看ZK服务进程和端口;
ps -ef|grep -ai zookeeper
netstat -tnlp|grep -aiwE 2181

3)、客户端测试

#启动ZK客户端命令行、创建测试信息;
/usr/local/zookeeper/bin/zkCli.sh
[root@localhost ~]# /usr/local/zookeeper/bin/zkCli.sh
/usr/local/jdk/bin/java
Connecting to localhost:2181
2021-07-11 04:09:30,521 [myid:] - INFO  [main:Environment@98] - Client environment:zookeeper.version=3.6.3--6401e4ad2087061bc6b9f80dec2d69f2e3c8660a, built on 04/08/2021 16:35 GMT
2021-07-11 04:09:30,525 [myid:] - INFO  [main:Environment@98] - Client environment:host.name=localhost
2021-07-11 04:09:30,525 [myid:] - INFO  [main:Environment@98] - Client environment:java.version=16.0.1
2021-07-11 04:09:30,526 [myid:] - INFO  [main:Environment@98] - Client environment:java.vendor=AdoptOpenJDK
2021-07-11 04:09:30,526 [myid:] - INFO  [main:Environment@98] - Client environment:java.home=/usr/local/jdk
2021-07-11 04:09:30,526 [myid:] - INFO  [main:Environment@98] - Client environment:java.class.path=/usr/local/zookeeper/bin/../zookeeper-server/target/classes:/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/zookeeper-prometheus-metrics-3.6.3.jar:/usr/local/zookeeper/bin/../lib/zookeeper-jute-3.6.3.jar:/usr/local/zookeeper/bin/../lib/zookeeper-3.6.3.jar:/usr/local/zookeeper/bin/../lib/snappy-java-1.1.7.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/usr/local/zookeeper/bin/../lib/simpleclient_servlet-0.6.0.jar:/usr/local/zookeeper/bin/../lib/simpleclient_hotspot-0.6.0.jar:/usr/local/zookeeper/bin/../lib/simpleclient_common-0.6.0.jar:/usr/local/zookeeper/bin/../lib/simpleclient-0.6.0.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-unix-common-4.1.63.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-epoll-4.1.63.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-4.1.63.Final.jar:/usr/local/zookeeper/bin/../lib/netty-resolver-4.1.63.Final.jar:/usr/local/zookeeper/bin/../lib/netty-handler-4.1.63.Final.jar:/usr/local/zookeeper/bin/../lib/netty-common-4.1.63.Final.jar:/usr/local/zookeeper/bin/../lib/netty-codec-4.1.63.Final.jar:/usr/local/zookeeper/bin/../lib/netty-buffer-4.1.63.Final.jar:/usr/local/zookeeper/bin/../lib/metrics-core-3.2.5.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper/bin/../lib/json-simple-1.1.1.jar:/usr/local/zookeeper/bin/../lib/jline-2.14.6.jar:/usr/local/zookeeper/bin/../lib/jetty-util-ajax-9.4.39.v20210325.jar:/usr/local/zookeeper/bin/../lib/jetty-util-9.4.39.v20210325.jar:/usr/local/zookeeper/bin/../lib/jetty-servlet-9.4.39.v20210325.jar:/usr/local/zookeeper/bin/../lib/jetty-server-9.4.39.v20210325.jar:/usr/local/zookeeper/bin/../lib/jetty-security-9.4.39.v20210325.jar:/usr/local/zookeeper/bin/../lib/jetty-io-9.4.39.v20210325.jar:/usr/local/zookeeper/bin/../lib/jetty-http-9.4.39.v20210325.jar:/usr/local/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/usr/local/zookeeper/bin/../lib/jackson-databind-2.10.5.1.jar:/usr/local/zookeeper/bin/../lib/jackson-core-2.10.5.jar:/usr/local/zookeeper/bin/../lib/jackson-annotations-2.10.5.jar:/usr/local/zookeeper/bin/../lib/commons-cli-1.2.jar:/usr/local/zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper/bin/../zookeeper-*.jar:/usr/local/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/usr/local/zookeeper/bin/../conf:
2021-07-11 04:09:30,526 [myid:] - INFO  [main:Environment@98] - Client environment:java.library.path=/usr/local/jdk/lib/default:/usr/local/jdk/lib:/usr/lib64:/usr/lib
2021-07-11 04:09:30,526 [myid:] - INFO  [main:Environment@98] - Client environment:java.io.tmpdir=/tmp
2021-07-11 04:09:30,526 [myid:] - INFO  [main:Environment@98] - Client environment:java.compiler=j9jit29
2021-07-11 04:09:30,527 [myid:] - INFO  [main:Environment@98] - Client environment:os.name=Linux
2021-07-11 04:09:30,527 [myid:] - INFO  [main:Environment@98] - Client environment:os.arch=amd64
2021-07-11 04:09:30,527 [myid:] - INFO  [main:Environment@98] - Client environment:os.version=3.10.0-1127.el7.x86_64
2021-07-11 04:09:30,527 [myid:] - INFO  [main:Environment@98] - Client environment:user.name=root
2021-07-11 04:09:30,527 [myid:] - INFO  [main:Environment@98] - Client environment:user.home=/root
2021-07-11 04:09:30,527 [myid:] - INFO  [main:Environment@98] - Client environment:user.dir=/root
2021-07-11 04:09:30,527 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.free=4MB
2021-07-11 04:09:30,529 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.max=256MB
2021-07-11 04:09:30,529 [myid:] - INFO  [main:Environment@98] - Client environment:os.memory.total=8MB
2021-07-11 04:09:30,533 [myid:] - INFO  [main:ZooKeeper@1006] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@4f3deac
2021-07-11 04:09:30,540 [myid:] - INFO  [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-07-11 04:09:30,551 [myid:] - INFO  [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2021-07-11 04:09:30,560 [myid:] - INFO  [main:ClientCnxn@1736] - zookeeper.request.timeout value is 0. feature enabled=false
Welcome to ZooKeeper!
2021-07-11 04:09:30,582 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1181] - Opening socket connection to server localhost/[0:0:0:0:0:0:0:1]:2181.
2021-07-11 04:09:30,582 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1183] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
2021-07-11 04:09:30,596 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1013] - Socket connection established, initiating session, client: /[0:0:0:0:0:0:0:1]:35944, server: localhost/[0:0:0:0:0:0:0:1]:2181
JLine support is enabled
2021-07-11 04:09:30,664 [myid:localhost:2181] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1448] - Session establishment complete on server localhost/[0:0:0:0:0:0:0:1]:2181, session id = 0x100004949480000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] create /otoyix 192.168.0.37
Created /otoyix
[zk: localhost:2181(CONNECTED) 2] ls /
[otoyix, zookeeper]
[zk: localhost:2181(CONNECTED) 3] get /otoyix 
192.168.0.37
[zk: localhost:2181(CONNECTED) 4] quit

2、Kafka服务部署

下载、解压、部署、运行

wget -c https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.7.1/kafka_2.12-2.7.1.tgz
#Tar解压kafka软件包;
tar -xf kafka_2.12-2.7.1.tgz
#创建kafka部署目录;
mkdir -p /usr/local/kafka/
#将解压程序移动至kafka部署目录;
mv kafka_2.12-2.7.1/* /usr/local/kafka/
#查看kafka是否部署成功;
ls -l /usr/local/kafka/
#查看默认模板配置文件;
ls -l /usr/local/kafka/config/server.properties
#启动kafka软件服务;
/usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/local/kafka/config/server.properties
#查看服务进程和端口;
ps -ef|grep -ai kafka
netstat -tnlp|grep -aiwE 9092

三、 Kafka消息发布、订阅案例

Topic被称为消息的主题、队列,每一个消息都有它的topic,Kafka通过topic对消息进行归类。Kafka中可以将Topic从物理上划分成一个或多个分区(Partition)。
每个分区在物理上对应一个文件夹,以”topicName_partitionIndex”的命名方式命名,该dir包含了这个分区的所有消息(.log)和索引文件(.index),这使得Kafka的吞吐率可以水平扩展。

1、创建一个Topic;

cd /usr/local/kafka/bin/
./kafka-topics.sh --create  \
 --zookeeper localhost:2181  \
 --replication-factor 1  \
 --partitions 1  \
 --topic testTopic

–replication-factor 复制因子为1;
–partitions 分区为1;

2、查看已创建的Topic:

./kafka-topics.sh --list --zookeeper localhost:2181
结果
testTopic

3、 Kafka消息发布
默认Kafka支持从Console控制台发送信息,消费者也可以从Console控制台接受信息,操作步骤和方法如下:
1)创建一个生产者(产生消息);

[root@localhost bin]# ./kafka-console-producer.sh --broker-list localhost:9092 --topic testTopic
otoyix
20210712
中国人是不可战胜的

–broker-list 表示代理服务器的列表,默认单台;
2)创建一个消费者(订阅/消费消息);

cd /usr/local/kafka/bin/
./kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic testTopic --from-beginning

–from-beginning 表示从消息开始处读取;
通过如上方法测试Kafka消息发布和订阅,在生产者的Console输入数据,消费者的Console就可以看到信息,即代表Kafka可以成功进行消息的发布和订阅。
centos7 Kafka部署(单机版)_第1张图片

四、注:

1、单机容器化时,我选择的保活脚本内容为

docker run -itd --privileged=true  --name=kafka \
 -p 9092:9092 \
 -p 2181:2181 \
 -v /usr/local/docker_kafka:/usr/local/kafka \
 kafka:v1 sh /usr/local/kafka/init.sh

cat /usr/local/docker_kafka/init.sh

JAVA_HOME=/usr/local/jdk
JRE_HOME=/usr/local/jdk/jre
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
JAVA_OPTS="-Xmx1024M -Xms1024M -Xmn384M -XX:PermSize=512m -XX:MaxPermSize=2048m"
PATH=/usr/local/sbin:/opt/ansible/ansible/bin:/bin:/usr/bin:/sbin:/usr/sbin:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
source /etc/profile
/usr/local/kafka/bin/zookeeper-server-start.sh -daemon /usr/local/kafka/config/zookeeper.properties
sleep 20
/usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/local/kafka/config/server.properties
/usr/bin/init
/usr/sbin/sshd
ping -c 9999999 127.0.0.1

2、 cat /usr/local/kafka/config/server.properties|grep -vE ‘#|$’

broker.id=0
listeners=PLAINTEXT://:9092
advertised.listeners=PLAINTEXT://192.168.0.168:9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=localhost:2181
zookeeper.connection.timeout.ms=18000
group.initial.rebalance.delay.ms=0

3、创建主题topic和分区

创建topic和分区
 ./kafka-topics.sh --zookeeper 192.168.0.168:2181 --create --topic toyixcf  --replication-factor 1 --partitions 3

查看topic详细信息

for i in 001 002  003 ;do    ./kafka-topics.sh --zookeeper localhost:2181  --describe --topic dcy$i ;done

Topic: dcy001   PartitionCount: 3       ReplicationFactor: 1    Configs: 
        Topic: dcy001   Partition: 0    Leader: 0       Replicas: 0     Isr: 0
        Topic: dcy001   Partition: 1    Leader: 0       Replicas: 0     Isr: 0
        Topic: dcy001   Partition: 2    Leader: 0       Replicas: 0     Isr: 0
Topic: dcy002   PartitionCount: 3       ReplicationFactor: 1    Configs: 
        Topic: dcy002   Partition: 0    Leader: 0       Replicas: 0     Isr: 0
        Topic: dcy002   Partition: 1    Leader: 0       Replicas: 0     Isr: 0
        Topic: dcy002   Partition: 2    Leader: 0       Replicas: 0     Isr: 0
Topic: dcy003   PartitionCount: 3       ReplicationFactor: 1    Configs: 
        Topic: dcy003   Partition: 0    Leader: 0       Replicas: 0     Isr: 0
        Topic: dcy003   Partition: 1    Leader: 0       Replicas: 0     Isr: 0
        Topic: dcy003   Partition: 2    Leader: 0       Replicas: 0     Isr: 0

--------------------------end

你可能感兴趣的:(K8S,Devops,MQ,Kafka,zookeeper)