Beispiel #1
0
 /**
  * 消费初始化
  * @param $groupName
  * @param string $configKey
  * @throws Exception
  */
 function getCustomerInstance($groupName, $configKey = "default")
 {
     $configObj = new Config("kfk");
     $config = $configObj->get($configKey);
     try {
         $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', $groupName);
         $conf->set('metadata.broker.list', $config);
         $topicConf = new \RdKafka\TopicConf();
         $topicConf->set('auto.offset.reset', 'smallest');
         $conf->setDefaultTopicConf($topicConf);
         $rk = new \RdKafka\KafkaConsumer($conf);
         $this->type = 1;
         $this->rk = $rk;
     } catch (\Exception $e) {
         throw new \Exception($e->getMessage());
     }
 }