智能摘要文章源自JAVA秀-https://www.javaxiu.com/30425.html
Kafka是一套流处理系统,可以让后端服务轻松的相互沟通,是微服务架构中常用的组件。如果是消费者组接收消息,Kafka会把一条消息路由到组中的某一个服务。处理用户消息,可以让某一个用户所有消息都在一个Partition。A、B、C,默认情况下,这3条消息是在不同的Partition中(如P1、P2、P3)。MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。文章源自JAVA秀-https://www.javaxiu.com/30425.html
原文约 6537 字 | 图片 34 张 | 建议阅读 14 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/30425.html
图解 Kafka,画得太好了!
点击关注 ? 芋道源码 文章源自JAVA秀-https://www.javaxiu.com/30425.html
收录于话题文章源自JAVA秀-https://www.javaxiu.com/30425.html
#芋道源码文章源自JAVA秀-https://www.javaxiu.com/30425.html
117个文章源自JAVA秀-https://www.javaxiu.com/30425.html
点击上方“芋道源码”,选择“设为星标”文章源自JAVA秀-https://www.javaxiu.com/30425.html
管她前浪,还是后浪?文章源自JAVA秀-https://www.javaxiu.com/30425.html
能浪的浪,才是好浪!文章源自JAVA秀-https://www.javaxiu.com/30425.html
每天 8:55 更新文章,每天掉亿点点头发...文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html 源码精品专栏文章源自JAVA秀-https://www.javaxiu.com/30425.html
原创 | Java 2020 超神之路,很肝~文章源自JAVA秀-https://www.javaxiu.com/30425.html
中文详细注释的开源项目文章源自JAVA秀-https://www.javaxiu.com/30425.html
RPC 框架 Dubbo 源码解析文章源自JAVA秀-https://www.javaxiu.com/30425.html
网络应用框架 Netty 源码解析文章源自JAVA秀-https://www.javaxiu.com/30425.html
消息中间件 RocketMQ 源码解析文章源自JAVA秀-https://www.javaxiu.com/30425.html
数据库中间件 Sharding-JDBC 和 MyCAT 源码解析文章源自JAVA秀-https://www.javaxiu.com/30425.html
作业调度中间件 Elastic-Job 源码解析文章源自JAVA秀-https://www.javaxiu.com/30425.html
分布式事务中间件 TCC-Transaction 源码解析文章源自JAVA秀-https://www.javaxiu.com/30425.html
Eureka 和 Hystrix 源码解析文章源自JAVA秀-https://www.javaxiu.com/30425.html
Java 并发源码文章源自JAVA秀-https://www.javaxiu.com/30425.html
来源:blog.csdn.net/duysh/article/details/116355977文章源自JAVA秀-https://www.javaxiu.com/30425.html
基础文章源自JAVA秀-https://www.javaxiu.com/30425.html
生产者消费者文章源自JAVA秀-https://www.javaxiu.com/30425.html
Topics 主题文章源自JAVA秀-https://www.javaxiu.com/30425.html
Partitions 分区文章源自JAVA秀-https://www.javaxiu.com/30425.html
架构文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
其中的概念还是比较多的,下面通过图示的方式来梳理一下 Kafka 的核心概念,以便在我们的头脑中有一个清晰的认识。文章源自JAVA秀-https://www.javaxiu.com/30425.html
基础
Kafka 是一套流处理系统,可以让后端服务轻松的相互沟通,是微服务架构中常用的组件。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
生产者消费者
生产者服务 Producer 向 Kafka 发送消息,消费者服务 Consumer 监听 Kafka 接收消息。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
一个服务可以同时为生产者和消费者。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
Topics 主题
Topic 是生产者发送消息的目标地址,是消费者的监听目标。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
一个服务可以监听、发送多个 Topics。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
Kafka 中有一个【consumer-group(消费者组)】的概念。文章源自JAVA秀-https://www.javaxiu.com/30425.html
这是一组服务,扮演一个消费者。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
如果是消费者组接收消息,Kafka 会把一条消息路由到组中的某一个服务。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
这样有助于消息的负载均衡,也方便扩展消费者。文章源自JAVA秀-https://www.javaxiu.com/30425.html
Topic 扮演一个消息的队列。文章源自JAVA秀-https://www.javaxiu.com/30425.html
首先,一条消息发送了。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
然后,这条消息被记录和存储在这个队列中,不允许被修改。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
接下来,消息会被发送给此 Topic 的消费者。文章源自JAVA秀-https://www.javaxiu.com/30425.html
但是,这条消息并不会被删除,会继续保留在队列中。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
继续发送消息。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
像之前一样,这条消息会发送给消费者、不允许被改动、一直呆在队列中。文章源自JAVA秀-https://www.javaxiu.com/30425.html
(消息在队列中能呆多久,可以修改 Kafka 的配置)文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
Partitions 分区
上面 Topic 的描述中,把 Topic 看做了一个队列,实际上,一个 Topic 是由多个队列组成的,被称为【Partition(分区)】。文章源自JAVA秀-https://www.javaxiu.com/30425.html
这样可以便于 Topic 的扩展。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
生产者发送消息的时候,这条消息会被路由到此 Topic 中的某一个 Partition。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
消费者监听的是所有分区。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
生产者发送消息时,默认是面向 Topic 的,由 Topic 决定放在哪个 Partition,默认使用轮询策略。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
也可以配置 Topic,让同类型的消息都在同一个 Partition。文章源自JAVA秀-https://www.javaxiu.com/30425.html
例如,处理用户消息,可以让某一个用户所有消息都在一个 Partition。文章源自JAVA秀-https://www.javaxiu.com/30425.html
例如,用户1发送了3条消息:A、B、C,默认情况下,这3条消息是在不同的 Partition 中(如 P1、P2、P3)。文章源自JAVA秀-https://www.javaxiu.com/30425.html
在配置之后,可以确保用户1的所有消息都发到同一个分区中(如 P1)。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
这个功能有什么用呢?文章源自JAVA秀-https://www.javaxiu.com/30425.html
这是为了提供消息的【有序性】。文章源自JAVA秀-https://www.javaxiu.com/30425.html
消息在不同的 Partition 是不能保证有序的,只有一个 Partition 内的消息是有序的。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
架构
Kafka 是集群架构的,ZooKeeper是重要组件。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
ZooKeeper 管理者所有的 Topic 和 Partition。文章源自JAVA秀-https://www.javaxiu.com/30425.html
Topic 和 Partition 存储在 Node 物理节点中,ZooKeeper负责维护这些 Node。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
例如,有2个 Topic,各自有2个 Partition。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
这是逻辑上的形式,但在 Kafka 集群中的实际存储可能是这样的:文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
Topic A 的 Partition #1 有3份,分布在各个 Node 上。文章源自JAVA秀-https://www.javaxiu.com/30425.html
这样可以增加 Kafka 的可靠性和系统弹性。文章源自JAVA秀-https://www.javaxiu.com/30425.html
3个 Partition #1 中,ZooKeeper 会指定一个 Leader,负责接收生产者发来的消息。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
其他2个 Partition #1 会作为 Follower,Leader 接收到的消息会复制给 Follower。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
这样,每个 Partition 都含有了全量消息数据。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
即使某个 Node 节点出现了故障,也不用担心消息的损坏。文章源自JAVA秀-https://www.javaxiu.com/30425.html
Topic A 和 Topic B 的所有 Partition 分布可能就是这样的:文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
感谢阅读,希望对你有所帮助 ?文章源自JAVA秀-https://www.javaxiu.com/30425.html
翻译整理自:文章源自JAVA秀-https://www.javaxiu.com/30425.html
https://timothystepro.medium.com/visualizing-kafka-20bc384803e7文章源自JAVA秀-https://www.javaxiu.com/30425.html
欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢:文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
已在知识星球更新源码解析如下:文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
最近更新《芋道 SpringBoot 2.X 入门》系列,已经 20 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。文章源自JAVA秀-https://www.javaxiu.com/30425.html
提供近 3W 行代码的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。文章源自JAVA秀-https://www.javaxiu.com/30425.html
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章源自JAVA秀-https://www.javaxiu.com/30425.html
文章有帮助的话,在看,转发吧。谢谢支持哟 (*^__^*)文章源自JAVA秀-https://www.javaxiu.com/30425.html
阅读原文文章源自JAVA秀-https://www.javaxiu.com/30425.html

评论