阅读前请看一下:我是一个热衷于记录的人,每次写博客会反复研读,尽量不断提升博客质量。文章设置为仅粉丝可见,是因为写博客确实花了不少精力。希望互相进步谢谢!!
文章目录
- 阅读前请看一下:我是一个热衷于记录的人,每次写博客会反复研读,尽量不断提升博客质量。文章设置为仅粉丝可见,是因为写博客确实花了不少精力。希望互相进步谢谢!!
- 1、前言
- 2、kafka是什么
- 3、简单使用
- 3、Docker按照kafka
- 4、总结
提示:以下是本篇文章正文内容
1、前言
visio 2016
2、kafka是什么
- Producer:即生产者,消息的产生者,是消息的入口;
- Consumer:消费者,即消息的消费方,是消息的出口;
- Broker:中间代理,即一个broker就是一个server。每个kafka集群内的broker都有一个不重复的编号,如图中的broker-0、broker-1等……
- Topic(主题):可以理解为消息的分类,kafka的数据就保存在topic。在每个broker上都可以创建多个topic,也可以类比为电脑的文件夹;
- Event(事件):也称为记录、消息(Message),可以类比为文件。示例事件包括支付交易、来自手机的地理位置更新、运输订单、来自物联网设备或医疗设备的传感器测量等等。这些事件被组织并存储在 Topic 中;
- Partition:为了实现扩展性,一个非常大的 topic 可以分布到多个 broker(即服务器) 上,一个 topic 可以分为多个 partition,每个 partition 是一个有序的队列。partition 中的每条消息都会被分配一个有序的 id(offset)。kafka 只保证按一个 partition 中的顺序将消息发给 consumer ,不保证一个 topic 的整体(多个 partition 间)的顺序;
- Consumer Group(CG):尚硅谷讲的,因为Topic被分为了Partition,同样Consumer一样可以分组。各个consumer可以组成一个组,同一个消费者组的消费者可以消费同一个topic的不同分区(partition)的数据,这也是为了提高kafka的吞吐量!
- Offset:kafka的存储文件都是按照offset.kafka来命名,用offset做名字的好处是方便查找。例如你想找位于2049的位置,只要找到2048.kafka的文件即可。当然the first offset就是00000000000.kafka。
参考连接《kafka基本原理详解》,里面有具体的流程。
下面是我自己看了很多资料理解的:
kafka 有很多 broker (因为一个 boker 相当于一个云服务器,即一台大型电脑),每个 broker 里有很多topic(相当于一个电脑有很多文件夹),而有时可能一个 topic 太大,一个 broker 无法存下一个 topic ,因此分为topicA-partition0 、 topicA-partition1 存在 broker1 、 broker2上(相当于分布式存储了,将一个大文件夹拆成很多小文件夹,当然为了防范风险,还有存其很多副本),而每一个 partition 下存的是event(相当于文件夹存的文档)。
理解尚硅谷讲的为什么kafka是这样的架构。
3、简单使用
官网教程中,要理解每个参数的作用,尤其注意这里.sh
是脚本文件,包含一系列命令,然后在 shell 被执行,例如-producer.sh
可以理解为开发人员为了用户操作方便,把用户需要在命令行执行的所有命令(这里为创建生产者的命令),写到了producer.sh
文件里,而用户只用执行此.sh文件,即可等价于执行很多复杂的命令,方便了用户。
官网那些.sh文件全是脚本文件,不同脚本文件集合了不同的命令行命令,一个脚本文件实现一个功能,用户执行这个.sh文件就能实现很多命令,方便很多。
3、Docker按照kafka
-
下载镜像
1.1 下载安装zookeeper镜像
sudo docker pull wurstmeister/zookeeper
1.2 下载安装kafka镜像
sudo docker pull wurstmeister/kafka
1.3 验证是否安装成功
sudo docker images
如下图所示为安装成功
-
启动
注意这里启动顺序有先后,需要先启动zookeeper。2.1 启动zookeeper
sudo docker run -d --name zookeeper \ -p 2181:2181 \ -e TZ="Asia/Shanghai" \ -t wurstmeister/zookeeper # --restart always # 不要此行参数!!!
参数介绍:
- --name:容器名字;
- -p:端口号(映射到宿主机的端口:zookeeper端口号);
- -t: 为容器重新分配一个伪输入终端;
- -e : 设置环境变量:
TZ="Asia/Shanghai"为设置了时区,因为虚拟机创建时就设置的为此时区,相当于同步docker内部时间和虚拟机时间。 - restart always:能够使我们在重启docker时,自动启动相关容器。例如使用 Docker 运行 Nginx。Nginx 作为目前常用的 web 服务器,我们肯定更希望看到它在因停电、主机重启等意外事件中尝试自动恢复。但网上有的博客docker安装kafka时,加了此参数,有的没有加,这里可以不用加。Docker 容器的重启策略一般用于生产环境,开发环境和实验环境可以忽略。若之后需要实现“重启docker时,自动启动相关容器”,添加参数即可,具体自行百度。
如下图所示,代表创建成功,返回了容器对应的id
2.2 启动kafka
sudo docker run -dit --name kafka \ -p 9092:9092 \ -e KAFKA_BROKER_ID=0 \ -e KAFKA_ZOOKEEPER_CONNECT=localhost:2181 \ -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9092 \ -e KAFKA_LISTENERS=PLAINTEXT://localhost:9092 \ -e TZ="Asia/Shanghai" \ -t wurstmeister/kafka
参数介绍:
- --name:容器名字;
- -p:端口号(映射到宿主机的端口:zookeeper端口号);
- -e : 设置环境变量:
KAFKA_BROKER_ID: 该ID是集群的唯一标识,因为在kafka集群中,每个kafka都有一个BROKER_ID来区分自己;
KAFKA_ZOOKEEPER_CONNECT==<这里换成你的zookeeper地址和端口>: 配置zookeeper管理kafka的路径;
KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://<这里换成你的kafka地址和端口>: kafka发布到zookeeper供客户端使用的服务地址;
KAFKA_LISTENERS: 配置kafka的监听端口,代表允许使用PLAINTEXT侦听器。网上有的教程将-e KAFKA_LISTENERS=PLAINTEXT://localhost:9092
换成了-e ALLOW_PLAINTEXT_LISTENER=yes
是一个意思。 - -v /etc/localtime:/etc/localtime: 将本地时间映射到容器中,即容器时间同步虚拟机的时间;
- -t: 为容器重新分配一个伪输入终端。
同样如下图所示代表创建成功,返回了容器对应的id
查看kafka日志记录:
sudo docker logs kafka
kafka就是容器名字
具体解决方案https://blog.csdn.net/weixin_55008454/article/details/126629905
其实就是防火墙没关闭
sudo docker ufw status 查看状态
sudo docker ufw disable 关闭防火墙
sudo docker ufw status 成功关闭
[2023-04-14 23:38:42,458] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:42,461] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:43,566] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:43,566] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:44,668] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:44,668] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:45,770] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:45,770] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:46,873] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:46,874] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:47,976] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:47,976] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:49,083] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:49,084] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:50,187] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:50,187] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:51,289] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:51,289] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:52,390] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:52,391] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:53,492] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:53,493] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:54,594] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:54,594] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:55,696] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:55,696] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:56,798] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:56,799] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:57,901] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:57,902] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:59,004] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:38:59,005] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:39:00,116] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:39:00,117] INFO Socket error occurred: localhost/127.0.0.1:2181: Connection refused (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:39:00,453] INFO [ZooKeeperClient Kafka server] Closing. (kafka.zookeeper.ZooKeeperClient)
[2023-04-14 23:39:01,222] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:39:01,327] INFO Session: 0x0 closed (org.apache.zookeeper.ZooKeeper)
[2023-04-14 23:39:01,327] INFO EventThread shut down for session: 0x0 (org.apache.zookeeper.ClientCnxn)
[2023-04-14 23:39:01,328] INFO [ZooKeeperClient Kafka server] Closed. (kafka.zookeeper.ZooKeeperClient)
[2023-04-14 23:39:01,330] ERROR Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer)
kafka.zookeeper.ZooKeeperClientTimeoutException: Timed out waiting for connection while in state: CONNECTING
at kafka.zookeeper.ZooKeeperClient.waitUntilConnected(ZooKeeperClient.scala:271)
at kafka.zookeeper.ZooKeeperClient.(ZooKeeperClient.scala:125)
at kafka.zk.KafkaZkClient$.apply(KafkaZkClient.scala:1948)
at kafka.server.KafkaServer.createZkClient
1
(
K
a
f
k
a
S
e
r
v
e
r
.
s
c
a
l
a
:
431
)
a
t
k
a
f
k
a
.
s
e
r
v
e
r
.
K
a
f
k
a
S
e
r
v
e
r
.
i
n
i
t
Z
k
C
l
i
e
n
t
(
K
a
f
k
a
S
e
r
v
e
r
.
s
c
a
l
a
:
456
)
a
t
k
a
f
k
a
.
s
e
r
v
e
r
.
K
a
f
k
a
S
e
r
v
e
r
.
s
t
a
r
t
u
p
(
K
a
f
k
a
S
e
r
v
e
r
.
s
c
a
l
a
:
191
)
a
t
k
a
f
k
a
.
K
a
f
k
a
1(KafkaServer.scala:431) at kafka.server.KafkaServer.initZkClient(KafkaServer.scala:456) at kafka.server.KafkaServer.startup(KafkaServer.scala:191) at kafka.Kafka
1(KafkaServer.scala:431)atkafka.server.KafkaServer.initZkClient(KafkaServer.scala:456)atkafka.server.KafkaServer.startup(KafkaServer.scala:191)atkafka.Kafka.main(Kafka.scala:109)
at kafka.Kafka.main(Kafka.scala)
[2023-04-14 23:39:01,332] INFO shutting down (kafka.server.KafkaServer)
[2023-04-14 23:39:01,340] INFO App info kafka.server for 0 unregistered (org.apache.kafka.common.utils.AppInfoParser)
[2023-04-14 23:39:01,341] INFO shut down completed (kafka.server.KafkaServer)
[2023-04-14 23:39:01,341] ERROR Exiting Kafka. (kafka.Kafka$)
[2023-04-14 23:39:01,344] INFO shutting down (kafka.server.KafkaServer)
1、切换root: su root + 输入root密码
注意:Ubuntu下su root认证失败
原因:第一次使用,自己输的是用户的密码,但用户的密码不等于root密码,因此使用 sudo passwd root
设置root的密码
2、查找文件 find / -iname “server.properties”。
find / -iname “selinux/config”
找到/config/server.properties 对应的文件
3、修改该文件,注意切换到config目录
vim ./server.properties
Kafka linux 采坑 | 启动报错: Timed out waiting for connection while in state: CONNECTING 改时间
- 测试发消息
4、总结
码字不易,谢谢点赞!!!
码字不易,谢谢点赞!!!
码字不易,谢谢点赞!!!