說明:有不少同窗在服務器上搭建好,kafka,在應用端使用kafka時候出現不少問題,這裏提供下個人kafka生產和消費的php函數php
環境說明:git
1:首先php要有kafka擴展,在命令行中輸入 php -m 看是否有rdkafka github
沒有的話須要安裝配置下:json
--------------- kafka php客戶端安裝(php-rdkafka) --------------
1.安裝 librdkafka
git clone https://github.com/edenhill/librdkafka
cd librdkafka
./configure
make
sudo make install數組
2.安裝php-rdkafka
git clone https://github.com/arnaud-lb/php-rdkafka.git
cd php-rdkafka
phpize
./configure
make all -j 5
sudo make install服務器
vi /usr/local/lib/php.ini
加入 extension=rdkafka.sosocket
2:在kafka控制器中我直接貼出來個人生產和消費函數:函數
/** * 生產單個消息 * @param string $topic * @param null $post */ function kafka_produce($key=null,$post=null) { $rk = new \RdKafka\producer(); $rk->setLogLevel(LOG_DEBUG); $rk->addBrokers("localhost:9092,localhost:9093,localhost:9094,localhost:9095"); $topics = $rk->newTopic('engine.com'); $topics->produce(1, 0,$post,$key); echo 'kafka_produce success!!!'; }
/** * 高級消費模式 * @param $topic * @return int * @throws Exception */ function kafka_high_consume($topic='engine.com'){ $conf = new \RdKafka\Conf(); $conf->setRebalanceCb(function (RdKafka\KafkaConsumer $kafka, $err, array $partitions = null) { switch ($err) { case RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS: echo "Assign: "; var_dump($partitions); $kafka->assign($partitions); break; case RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS: echo "Revoke: "; var_dump($partitions); $kafka->assign(NULL); break; default: throw new \Exception($err); } }); $conf->set('group.id', '0'); $conf->set('metadata.broker.list', 'localhost:9092,localhost:9093,localhost:9094,localhost:9095'); // 針對低延遲進行了優化的配置。這容許PHP進程/請求儘快發送消息並快速終止 $conf->set('socket.timeout.ms', 50); if (function_exists('pcntl_sigprocmask')) { pcntl_sigprocmask(SIG_BLOCK, array(SIGIO)); $conf->set('internal.termination.signal', SIGIO); } else { $conf->set('queue.buffering.max.ms', 1); } $topicConf = new \RdKafka\TopicConf(); $topicConf->set('auto.commit.interval.ms', 100); $topicConf->set('auto.offset.reset', 'smallest'); $topicConf->set('offset.store.path', 'kafka_offset.log'); $conf->setDefaultTopicConf($topicConf); $consumer = new \RdKafka\KafkaConsumer($conf); // $topics->consumeStart(1, RD_KAFKA_OFFSET_STORED); // 更新訂閱集(自動分配partitions ) $consumer->subscribe([$topic]); // 指定topic分配partitions使用那個分區 // $consumer->assign([ // new \RdKafka\TopicPartition("zzy8", 0), // new \RdKafka\TopicPartition("zzy8", 1), // ]); while (true) { // 設置120s爲超時 $message = $consumer->consume(120 * 1000); if (!empty($message)) { switch ($message->err) { case RD_KAFKA_RESP_ERR_NO_ERROR: info('New message received :', $message); // 拆解對象爲數組 $payload = json_decode($message->payload,true); $Orders = new OrdersController(); $key = $message->key; // 根據kafka中不一樣key,調用對應方法傳遞處理數據 ...
...
...
break; case RD_KAFKA_RESP_ERR__PARTITION_EOF: echo "No more messages; will wait for more\n"; break; case RD_KAFKA_RESP_ERR__TIMED_OUT: echo "Timed out\n"; var_dump("##################"); break; default: var_dump("nothing"); throw new \Exception($message->errstr(), $message->err); break; } } else { var_dump('this is empty obj!!!'); } } return 0; }
在這裏說明下,我放的是我項目中的使用函數,一些參數配置,你們能夠根據個人提示自行註釋和使用。post