kafka.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta name="generator" content="Asciidoctor 2.0.15">
  8. <title>Kafka基本概念</title>
  9. <link rel="stylesheet" href="css/site.css">
  10. <link href="css/custom.css" rel="stylesheet">
  11. <script src="js/setup.js"></script><script defer src="js/site.js"></script>
  12. </head>
  13. <body class="article toc2 toc-left"><div id="banner-container" class="container" role="banner">
  14. <div id="banner" class="contained" role="banner">
  15. <div id="switch-theme">
  16. <input type="checkbox" id="switch-theme-checkbox" />
  17. <label for="switch-theme-checkbox">Dark Theme</label>
  18. </div>
  19. </div>
  20. </div>
  21. <div id="tocbar-container" class="container" role="navigation">
  22. <div id="tocbar" class="contained" role="navigation">
  23. <button id="toggle-toc"></button>
  24. </div>
  25. </div>
  26. <div id="main-container" class="container">
  27. <div id="main" class="contained">
  28. <div id="doc" class="doc">
  29. <div id="header">
  30. <h1>Kafka基本概念</h1>
  31. <div id="toc" class="toc2">
  32. <div id="toctitle">Table of Contents</div>
  33. <span id="back-to-index"><a href="index.html">Back to index</a></span><ul class="sectlevel1">
  34. <li><a href="#_启动">1. 启动</a></li>
  35. </ul>
  36. </div>
  37. </div>
  38. <div id="content">
  39. <div id="preamble">
  40. <div class="sectionbody">
  41. <div class="paragraph">
  42. <p>Kafka 的数据单元被称为 <strong>消息</strong>(message),消息可以有 个可选的元数据 也就是键。键 个字节数组,与消息一样</p>
  43. </div>
  44. <div class="paragraph">
  45. <p>当消息以 种可控的方式写入不同的分区时,会用到<strong>键</strong>。最简单的例子就是为键生成一个一致性散列值,然后使用散列值对主题分区数进行取模,为消息选取分区。这样可以保证具有相同键的消息总是被写到相同的分区上。</p>
  46. </div>
  47. <div class="paragraph">
  48. <p>为了提高效率,消息被分批次写入 Kafka <strong>批次</strong>就是一组消息,这些消息属于同一 个主题和分区</p>
  49. </div>
  50. <div class="paragraph">
  51. <p>对于 afka 来说,消息不过是晦涩难懂的字节数组,所以有人建议用 些额外的结构来定义消息内容,让它们更易于理解。根据应用程序的需求,肖息<strong>模式(schema )</strong>有许可用的选项如JSON、xml</p>
  52. </div>
  53. <div class="paragraph">
  54. <p>Kaflca 的悄息通过<strong>主题(topic)</strong>进行分类。主题就好比数据库的表,或者文件系统里的文件夹</p>
  55. </div>
  56. <div class="paragraph">
  57. <p>主题可以被分为若干个<strong>分区</strong></p>
  58. </div>
  59. <div class="paragraph">
  60. <p><strong>偏移量(offset)</strong>是另一种元数据,它是个不断递增的整数值,在创建消息 时, Kafka 会把它添加到消息里。在给定的分区里,每个悄息的偏移量都是唯一的。消费者把每个分区最后读取的悄息偏移量保存在 Zookeeper Kafka 上,如果悄费者关闭或重启,它的读取状态不会丢失。</p>
  61. </div>
  62. <div class="paragraph">
  63. <p>消费者是<strong>消费者群组(Consumer Group)</strong>的一部分,也就是说,会有一个或多个消费者共同读取一个主题,群组保证每个分区只能被一个消费者使用</p>
  64. </div>
  65. <div class="paragraph">
  66. <p>消费者与分区之间的映射通常被称为悄费者对分区的<strong>所有权关系</strong></p>
  67. </div>
  68. <div class="paragraph">
  69. <p>一个独立的 Kafka 服务器被称为 <strong>broker</strong></p>
  70. </div>
  71. <div class="paragraph">
  72. <p>broker 是<strong>集群</strong>的组成部分,每个集群都有 一个broker 同时充当了<strong>集群控制器</strong>的角色</p>
  73. </div>
  74. <div class="paragraph">
  75. <p>一个分区可以分配给多个 broke ,这个时候会发生分区复制,在集群中, 一个分区从属于 一个broker, 该broker 被称为分区的<strong>首领</strong></p>
  76. </div>
  77. <div class="paragraph">
  78. <p>保留消息(在一定期限内)是 Kafka一个重要特性。要么保留一段时间(比如7天),要么保留到消息达到一定大小的字节数</p>
  79. </div>
  80. <div class="paragraph">
  81. <p>可以通过配置把主题当作<strong>紧凑型日志</strong>,只有最后一个带有特定键的消息会被保留下来</p>
  82. </div>
  83. <div class="paragraph">
  84. <p>分区的所有权从一个消费者转移到另一个消费者,这样的行为被称为<strong>再均衡</strong></p>
  85. </div>
  86. <div class="paragraph">
  87. <p>消费者通过向被指派为<strong>群组协调器(GroupCoordinator)</strong>的 broker (不同的群组可以有不同的协调器)发送<strong>心跳</strong>来维持它们和群组的从属关系以及它们对分区的所有权关系</p>
  88. </div>
  89. <div class="paragraph">
  90. <p>消费者可以使用 Kafka 来追踪消息在分区里的位置(偏移量),我们把更新分区当前位置的操作叫作<strong>提交</strong></p>
  91. </div>
  92. <div class="paragraph">
  93. <p>一个消费者群组里一个消费者可能同时消费一个主题的多个分区,但一个分区一定只由一个消费者消费</p>
  94. </div>
  95. <div class="paragraph">
  96. <p>如果只有一个消费者,就不需要<strong>订阅</strong>主题, 而代 是为自己<strong>分配</strong>分区。</p>
  97. </div>
  98. <div class="paragraph">
  99. <p><strong>首领副本(Leader)</strong> 所有生产者和消费者的请求都经过它</p>
  100. </div>
  101. <div class="paragraph">
  102. <p><strong>跟随者副本(follower)</strong> 从Leader复制数据</p>
  103. </div>
  104. <div class="paragraph">
  105. <p>Kafka 使用<strong>零拷贝</strong>技术向客户端发送消息,也就是说, Kafka 直接把消息从文件(或者更确切地说是 Linux 文件系统缓存)里发送到网络通道而不需要经过任何中间缓冲区</p>
  106. </div>
  107. </div>
  108. </div>
  109. <div class="sect1">
  110. <h2 id="_启动"><a class="anchor" href="#_启动"></a>1. 启动</h2>
  111. <div class="sectionbody">
  112. <div class="listingblock">
  113. <div class="content">
  114. <pre class="highlight"><code class="language-shell" data-lang="shell">[root@localhost ~]# /usr/local/zookeeper/bin/zkServer.sh start
  115. [root@localhost ~]# telnet localhost 2181
  116. Trying ::1...
  117. Connected to localhost.
  118. Escape character is '^]'.
  119. srvr
  120. Zookeeper version: 3.5.8-f439ca583e70862c3068a1f2a7d4d068eec33315, built on 05/04/2020 15:07 GMT
  121. Latency min/avg/max: 0/0/0
  122. Received: 1
  123. Sent: 0
  124. Connections: 1
  125. Outstanding: 0
  126. Zxid: 0x8d
  127. Mode: standalone
  128. Node count: 132
  129. Connection closed by foreign host.</code></pre>
  130. </div>
  131. </div>
  132. <div class="listingblock">
  133. <div class="content">
  134. <pre class="highlight"><code class="language-shell" data-lang="shell">[root@localhost ~]# /usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/local/kafka/config/server.properties
  135. [root@localhost ~]# /usr/local/kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
  136. [root@localhost ~]# /usr/local/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
  137. &gt;Test Message1
  138. &gt;
  139. ^D
  140. [root@localhost ~]# /usr/local/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
  141. Test Message1
  142. ^C
  143. [root@localhost ~]#</code></pre>
  144. </div>
  145. </div>
  146. </div>
  147. </div>
  148. </div>
  149. <div id="footer">
  150. <div id="footer-text">
  151. Last updated 2024-03-18 05:44:42 UTC
  152. </div>
  153. </div>
  154. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.3/highlight.min.js"></script>
  155. <script>
  156. if (!hljs.initHighlighting.called) {
  157. hljs.initHighlighting.called = true
  158. ;[].slice.call(document.querySelectorAll('pre.highlight > code')).forEach(function (el) { hljs.highlightBlock(el) })
  159. }
  160. </script>
  161. <script src="https://utteranc.es/client.js"
  162. repo="pxzxj/articles"
  163. issue-term="title"
  164. label="utteranc"
  165. theme="github-light"
  166. crossorigin="anonymous"
  167. async>
  168. </script>
  169. </div>
  170. </div>
  171. </div>
  172. </body>
  173. </html>