{"id":361,"date":"2020-05-31T22:20:53","date_gmt":"2020-05-31T14:20:53","guid":{"rendered":"http:\/\/www.leohe.net.cn\/?p=361"},"modified":"2020-06-01T23:17:15","modified_gmt":"2020-06-01T15:17:15","slug":"kafka","status":"publish","type":"post","link":"https:\/\/china-abc.leohe.net.cn\/?p=361","title":{"rendered":"kafka \u5b89\u88c5\u548cPHP\u4f7f\u7528"},"content":{"rendered":"<p><strong>kafka\u4e0b\u8f7d\uff1a<\/strong><br \/>\nwget https:\/\/www.apache.org\/dyn\/closer.cgi?path=\/kafka\/2.5.0\/kafka_2.12-2.5.0.tgz<br \/>\ntar -xzvf kafka_2.12-2.5.0.tgz -C \/usr\/local\/<br \/>\ncd \/usr\/local\/kafka_2.13-2.5.0<br \/>\ncd config<br \/>\n\u590d\u5236zookeeper\u914d\u7f6e\u9879<br \/>\ncat zookeeper.properties |grep -v '#' &gt;&gt; zk.properties<br \/>\nvim zk.properties<br \/>\n\u4fee\u6539dataDir\u9879<br \/>\ndataDir=\/usr\/local\/zookeeper\/data<br \/>\n\u590d\u5236kafka\u914d\u7f6e\u9879<br \/>\ncd \/usr\/local\/kafka_2.13-2.5.0\/config<br \/>\ncat server.properties | grep -v '#' &gt;&gt;kafka1.properties<br \/>\nvim kafka1.properties<br \/>\n##\u5982\u679c\u9700\u8981\u5f00\u542f\u8fdc\u7a0b\u8bbf\u95ee\u5219\u4fee\u6539\u5982\u4e0b\u4fe1\u606f\uff1a<br \/>\n\/\/\u6dfb\u52a0host.name<br \/>\nhost.name=192.168.0.30<br \/>\n\/\/\u6b64\u9879\u6253\u5f00<br \/>\nlisteners=PLAINTEXT:\/\/:9092<br \/>\n\/\/\u6b64\u9879\u6253\u5f00\u5e76\u8bbe\u7f6e\u5982\u4e0b<br \/>\nadvertised.listeners=PLAINTEXT:\/\/192.168.0.30:9092<br \/>\n\u91cd\u542f\u52a8\u670d\u52a1\uff0c\u5373\u53ef\u901a\u8fc7\u8fdc\u7a0b\u8fde\u63a5\u5230kafka\u4e3b\u673a\u3002\u518d\u5c31\u662f\u6ce8\u610f\u9632\u706b\u5899<br \/>\n\u4fdd\u5b58\u9000\u51fa<\/p>\n<p><strong>\u542f\u52a8zookeeper<\/strong><br \/>\n.\/bin\/zookeeper-server-start.sh -daemon config\/zk.properties<br \/>\nnetstat -anp|grep 2128<br \/>\n\u7aef\u53e3\u5b58\u5728 \u5219\u8868\u793a \u542f\u52a8\u6210\u529f<br \/>\n<strong>\u542f\u52a8kafka<\/strong><br \/>\n.\/bin\/kafka-server-start.sh -daemon config\/kafka1.properties<br \/>\nnetstat -anp|grep 9092<br \/>\n\u7aef\u53e3\u5b58\u5728 \u5219\u8868\u793a \u542f\u52a8\u6210\u529f<\/p>\n<p><strong>\u4f7f\u7528\uff1a<\/strong><br \/>\n\/\/\u67e5\u770b\u6240\u6709topic\u5217\u8868<br \/>\n.\/bin\/kafka-topics.sh --list --zookeeper localhost:2181<br \/>\n\/\/\u521b\u5efatopic \u8868\u793a\u521b\u5efa\u540d\u4e3atest\u7684topic\uff0c 2\u4e2a\u5907\u4efd\uff0c3\u4e2a\u5206\u533a<br \/>\n\/\/replication-factor<br \/>\n\/\/\u7528\u6765\u8bbe\u7f6e\u4e3b\u9898\u7684\u526f\u672c\u6570\u3002\u6bcf\u4e2a\u4e3b\u9898\u53ef\u4ee5\u6709\u591a\u4e2a\u526f\u672c\uff0c\u526f\u672c\u4f4d\u4e8e\u96c6\u7fa4\u4e2d\u4e0d\u540c\u7684broker\u4e0a\uff0c\u4e5f\u5c31\u662f\u8bf4\u526f\u672c\u7684\u6570\u91cf\u4e0d\u80fd\u8d85\u8fc7broker\u7684\u6570\u91cf\uff0c\u5426\u5219\u521b\u5efa\u4e3b\u9898\u65f6\u4f1a\u5931\u8d25\u3002<br \/>\n\/\/partions<br \/>\n\/\/\u4e3b\u9898\u5206\u533a\u6570<br \/>\n.\/bin\/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 3 --partitions 3 --topic test<br \/>\n\/\/\u5220\u9664\u540d\u4e3atest\u7684topic<br \/>\n.\/bin\/kafka-topics.sh --delete --zookeeper localhost:2181 --topic test<br \/>\n\/\/\u6d88\u606f\u751f\u4ea7\u8005<br \/>\n.\/bin\/kafka-console-producer.sh --broker-list localhost:9092 --topic test<br \/>\n\/\/\u6d88\u606f\u6d88\u8d39\u8005<br \/>\n.\/bin\/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test<br \/>\n\/\/\u4ece\u6700\u5f00\u5934\u6d88\u8d39\u540d\u4e3atest\u7684topic<br \/>\n.\/bin\/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning<br \/>\n\/\/\u5c55\u793a\u540d\u4e3atest\u7684topic\u7684offset\u4fe1\u606f<br \/>\n.\/bin\/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic test --time -1<br \/>\n\/\/\u663e\u793a\u5206\u7ec4\u4e3atestgroup\u7684\u4fe1\u606f<br \/>\n.\/bin\/kafka-consumer-groups.sh --describe --bootstrap-server localhost:9092 --group testgroup<\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><br \/>\n\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u4e0d\u80fd\u8fde\u63a5\u8fdc\u7a0bkafka\u96c6\u7fa4<br \/>\n\u9519\u8bef\u65e5\u5fd7\u51fa\u73b0 \u8fde\u63a5\u62d2\u7edd\uff1a<br \/>\nConnection refused:<br \/>\n\u4fee\u6539kafka\u914d\u7f6e\uff0c\u5f00\u542f\u8fdc\u7a0b\u8bbf\u95ee\u5373\u53ef<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><br \/>\n\u540c\u4e00\u4e2atopic \u9ad8\u7ea7\u6d88\u8d39\u548c\u4f4e\u7ea7\u6d88\u8d39\u4e0d\u53ef\u540c\u65f6\u4f7f\u7528\uff0c\u4e0d\u7136\u4f1a\u51fa\u73b0offse\u63d0\u4ea4\u51b2\u7a81\uff0c\u4e24\u4e2a\u4f4e\u7ea7\u6d88\u8d39\u8005\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528<\/p>\n<p><strong>\u5404\u9879\u53c2\u6570\uff1a<\/strong><br \/>\nhttps:\/\/github.com\/edenhill\/librdkafka\/blob\/master\/CONFIGURATION.md<\/p>\n<p><strong>rdkafka\u6587\u6863\uff1a<\/strong><br \/>\nhttps:\/\/arnaud.le-blanc.net\/php-rdkafka-doc\/phpdoc\/book.rdkafka.html<\/p>\n<p><strong>\u6d88\u606f\u987a\u5e8f\uff1a<\/strong><br \/>\n\u751f\u4ea7\u8005\u5728\u5199\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a key\uff0c\u6bd4\u5982\u8bf4\u6211\u4eec\u6307\u5b9a\u4e86\u67d0\u4e2a\u8ba2\u5355 id \u4f5c\u4e3a key\uff0c\u90a3\u4e48\u8fd9\u4e2a\u8ba2\u5355\u76f8\u5173\u7684\u6570\u636e\uff0c\u4e00\u5b9a\u4f1a\u88ab\u5206\u53d1\u5230\u540c\u4e00\u4e2a partition \u4e2d\u53bb\uff0c\u800c\u4e14\u8fd9\u4e2a partition \u4e2d\u7684\u6570\u636e\u4e00\u5b9a\u662f\u6709\u987a\u5e8f\u7684<br \/>\n<strong>\u5355\u6d88\u606f\u8005\u591a\u7ebf\u7a0b\u5904\u7406\u5efa\u8bae\uff1a<\/strong><br \/>\n\u4e00\u822c\u4e0d\u5efa\u8bae\u4f7f\u7528\uff0c\u56e0\u4e3akafka\u7ebf\u7a0b\u662f\u4e0d\u5b89\u5168\u7684\u3002\u591a\u7ebf\u7a0b\u5904\u7406\u65f6\u4e0d\u597d\u5904\u7406\u591a\u7ebf\u7a0b\u4e4b\u95f4\u7684offset\u786e\u8ba4<br \/>\n\u5982\u679c\u5bf9\u6d88\u606f\u987a\u5e8f\u6ca1\u6709\u8981\u6c42\uff0c\u5219\u65e0\u6240\u8c13\u3002<br \/>\n\u5efa\u8bae\uff1a\u6839\u636e\u60a8\u7684\u8981\u6c42\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e00\u79cd\u65b9\u6cd5\u6765\u89e3\u51b3\u4e0a\u8ff0\u53ef\u80fd\u7684\u95ee\u9898\uff0c\u5e76\u91c7\u7528\u56fa\u5b9a\u7684\u65b9\u6cd5\u3002\u4f46\u662f\uff0c\u6211\u5efa\u8bae\u4e00\u4e2a\u66f4\u53ef\u9760\u7684\u89e3\u51b3\u65b9\u6848\u662f\u4f7f\u7528\u5206\u533a\u6682\u505c\/\u6062\u590d\u3002\u60a8\u7684\u6d88\u8d39\u8005\u5e94\u8be5\u4ee5\u975e\u5e38\u62bd\u8c61\u7684\u65b9\u5f0f\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<br \/>\n1\uff1a\u8f6e\u8be2\uff08\uff09\u4ee5\u83b7\u53d6\u6d88\u606f\u3002<br \/>\n2\uff1a\u6682\u505c\u6240\u6709\u76f8\u5e94\u7684\u4e3b\u9898\/\u5206\u533a\u3002<br \/>\n3\uff1a\u5c06\u6d88\u606f\u5206\u914d\u7ed9\u5de5\u4f5c\u7ebf\u7a0b\u5e76\u7b49\u5f85\u5176\u5904\u7406\u3002<br \/>\n4\uff1a\u7ee7\u7eed\u8c03\u7528poll\uff08\uff09\uff0c\u4f46\u7531\u4e8e\u5206\u533a\u6682\u505c\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u65b9\u4fdd\u6301\u6d3b\u52a8\u72b6\u6001\u65f6\u4e0d\u4f1a\u6536\u5230\u4efb\u4f55\u989d\u5916\u7684\u6d88\u606f\u3002\uff08\u786e\u4fdd\u6b64\u65f6\u672a\u6ce8\u518c\u65b0\u4e3b\u9898\uff09<br \/>\n5\uff1a\u5982\u679c\u6240\u6709\u5de5\u4f5c\u7ebf\u7a0b\u90fd\u5e94\u62a5\u544a\u6d88\u606f\u5904\u7406\u6210\u529f\/\u5931\u8d25\uff0c\u5219\u76f8\u5e94\u5730\u63d0\u4ea4\u504f\u79fb\u91cf\u3002<br \/>\n6\uff1a\u6062\u590d\u6240\u6709\u5206\u533a\u3002<br \/>\n\u6ce8\u610f\uff1a\u6839\u636e\u60a8\u7684\u65b9\u6848\u548c\u8981\u6c42\uff0c\u53ef\u4ee5\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u6216\u5176\u4ed6\u89e3\u51b3\u65b9\u6848\u3002\u8fd9\u53ea\u662f\u4e00\u4e2a\u60f3\u6cd5\u6216\u53ef\u80fd\u7684\u89e3\u51b3\u65b9\u6848\u4e4b\u4e00\u3002<\/p>\n<p><strong>\u53c2\u6570\u9879\uff1a<\/strong><br \/>\n\u907f\u514dRebalance<br \/>\nRebalance \u53d1\u751f\u7684\u65f6\u673a\u6709\u4e09\u4e2a\uff1a<br \/>\n1\u3001\u7ec4\u6210\u5458\u6570\u91cf\u53d1\u751f\u53d8\u5316<br \/>\n2\u3001\u8ba2\u9605\u4e3b\u9898\u6570\u91cf\u53d1\u751f\u53d8\u5316<br \/>\n3\u3001\u8ba2\u9605\u4e3b\u9898\u7684\u5206\u533a\u6570\u53d1\u751f\u53d8\u5316<br \/>\n<strong>\u6307\u5b9a\u4e00\u4e2a\u4e3b\u9898\u7684\u67d0\u4e9b\u5206\u533a\u6d88\u8d39,\u4e0d\u4f1a\u89e6\u53d1rebalance.\u5373\u6307\u5b9apartition\u6d88\u8d39\u8005\u4e0d\u89e6\u53d1rebalance<\/strong><br \/>\n\u975e\u5fc5\u8981 Rebalance \u662f\u56e0\u4e3a\u672a\u80fd\u53ca\u65f6\u53d1\u9001\u5fc3\u8df3\uff0c\u5bfc\u81f4 Consumer \u88ab \u201c\u8e22\u51fa\u201dGroup \u800c\u5f15\u53d1\u7684\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e session.timeout.ms \u548c heartbeat.interval.ms \u7684\u503c\uff0c\u6765\u5c3d\u91cf\u907f\u514drebalance\u7684\u51fa\u73b0\u3002\uff08\u4ee5\u4e0b\u7684\u914d\u7f6e\u662f\u5728\u7f51\u4e0a\u627e\u5230\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u6682\u65f6\u8fd8\u6ca1\u6d4b\u8bd5\u8fc7\uff09<br \/>\n\u8bbe\u7f6e session.timeout.ms = 6s\u3002<br \/>\n\u8bbe\u7f6e heartbeat.interval.ms = 2s\u3002<br \/>\n\u8981\u4fdd\u8bc1 Consumer \u5b9e\u4f8b\u5728\u88ab\u5224\u5b9a\u4e3a \u201cdead\u201d \u4e4b\u524d\uff0c\u80fd\u591f\u53d1\u9001\u81f3\u5c11 3 \u8f6e\u7684\u5fc3\u8df3\u8bf7\u6c42\uff0c\u5373 session.timeout.ms &gt;= 3 * heartbeat.interval.ms\u3002<br \/>\n\u5c06 session.timeout.ms \u8bbe\u7f6e\u6210 6s \u4e3b\u8981\u662f\u4e3a\u4e86\u8ba9 Coordinator \u80fd\u591f\u66f4\u5feb\u5730\u5b9a\u4f4d\u5df2\u7ecf\u6302\u6389\u7684 Consumer\uff0c\u65e9\u65e5\u628a\u5b83\u4eec\u8e22\u51fa Group\u3002<br \/>\nmax.poll.interval.ms \/\/\u6d88\u8d39\u8005\u6700\u5927\u5141\u8bb8\u6267\u884c\u65f6\u95f4\uff0c\u5982\u679c\u8d85\u8fc7\u6b64\u65f6\u95f4\u95f4\u9694\uff0c\u800c\u6ca1\u6709\u8fdb\u884c\u4e0b\u4e00\u4e2apoll\uff0c\u5219\u8ba4\u4e3a\u8be5\u4f7f\u7528\u8005\u5931\u8d25\u4e86\uff0c\u8be5\u7ec4\u5c06\u91cd\u65b0\u5e73\u8861\uff0c\u4f1a\u4ea7\u751freblance\u64cd\u4f5c\uff0c\u6700\u597d\u5c06\u8be5\u53c2\u6570\u503c\u8bbe\u7f6e\u5f97\u5927\u4e00\u70b9\uff0c\u6bd4\u4f60\u7684\u4e0b\u6e38\u6700\u5927\u5904\u7406\u65f6\u95f4\u7a0d\u957f\u4e00\u70b9<\/p>\n<p><strong>PHP\u4f7f\u7528\uff1a<\/strong><\/p>\n<p>\u5b89\u88c5\u6269\u5c55\uff1a<\/p>\n<p>1\u3001\u5b89\u88c5librdkafka<\/p>\n<p>git clone https:\/\/github.com\/edenhill\/librdkafka.git<\/p>\n<p>cd \u00a0librdkafka<br \/>\n.\/configure<br \/>\nmake &amp;&amp; make install<\/p>\n<p>2\u3001\u5b89\u88c5rdkafka<\/p>\n<p>git clone https:\/\/github.com\/arnaud-lb\/php-rdkafka.git<br \/>\ncd php-rdkafka<br \/>\nphpize<br \/>\n.\/configure \u00a0--with-php-config=\/usr\/local\/php\/bin\/php-config\u00a0 ###\u4f60\u5b89\u88c5\u7684php\u4e0b\u7684php-config\u8def\u5f84<br \/>\nmake &amp;&amp; make install<\/p>\n<p>3\u3001\u542f\u7528\u6269\u5c55<\/p>\n<p>php.ini\u4e2d\u6dfb\u52a0\u884c ###\u5728php\u4e0b\u9762\u7684\/etc\/php.ini \u7f16\u8f91<\/p>\n<p><span class=\"hljs-attribute\">extension<\/span>=<span class=\"hljs-string\">rdkafka.so<\/span><\/p>\n<p><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"http:\/\/www.leohe.net.cn\/wp-content\/uploads\/2020\/06\/kafkaController.zip\">kafkaController<\/a><\/span><\/p>\n<p>\u4e0b\u8f7d\u67e5\u770b or github url :<a href=\"https:\/\/github.com\/leohe666\/kafka\">https:\/\/github.com\/leohe666\/kafka<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>\u4f7f\u7528\u95ee\u9898\u53c2\u8003\uff1a<\/p>\n<p>Kafka\u5982\u4f55\u4fdd\u8bc1\u6d88\u606f\u4e0d\u4e22\u5931\u4e0d\u91cd\u590d<\/p>\n<p><a href=\"https:\/\/www.cnblogs.com\/cherish010\/p\/9764810.html\">https:\/\/www.cnblogs.com\/cherish010\/p\/9764810.html<\/a><\/p>\n<p>Kafka\u4e22\u5931\u6570\u636e\u95ee\u9898\u4f18\u5316\u603b\u7ed3<\/p>\n<p><a href=\"https:\/\/www.cnblogs.com\/qiaoyihang\/p\/9229854.html\">https:\/\/www.cnblogs.com\/qiaoyihang\/p\/9229854.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>kafka\u4e0b\u8f7d\uff1a wget https:\/\/www.apache.org\/dyn\/closer.cgi?pat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[18,6,5],"tags":[],"_links":{"self":[{"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/posts\/361"}],"collection":[{"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=361"}],"version-history":[{"count":9,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/posts\/361\/revisions"}],"predecessor-version":[{"id":381,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=\/wp\/v2\/posts\/361\/revisions\/381"}],"wp:attachment":[{"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/china-abc.leohe.net.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}