版权声明:转载请注明来源 https://blog.csdn.net/u013702678/article/details/88235051
php的kafka低阶API是指需要自行管理topic的partition信息,如果partition变动(增加或者减少),client侧需要自行处理负载均衡等,不然可能出现部分消息不处理的情形。
abstract class KafkaConsumerLowService extends KafkaConsumerBaseService
{
function consumer($partion_id, $worker)
{
$conf = new \RdKafka\Conf();
$conf->set('bootstrap.servers', $this->brokers);
$conf->set('group.id', 'test');
$rk = new \RdKafka\Consumer($conf);
$topicConf = new \RdKafka\TopicConf();
//自动提交offset间隔时间
$topicConf->set('auto.commit.interval.ms',self::CONSUMER_OFFSET_COMMIT_TIME);
//保存offset的方式,可以选择broker或者file
$topicConf->set('offset.store.method', 'broker');
//如果没有检测到有保存的offset,就从最小开始
$topicConf->set('auto.offset.reset', 'smallest');
$topic = $rk->newTopic($this->topic, $topicConf);
$topic->consumeStart($partion_id, RD_KAFKA_OFFSET_STORED);
while(true) {
$msg = $topic->consume($partion_id,self::CONSUMER_TIME_OUT);
if(empty($msg))
{
continue;
}
switch($msg->err) {
case RD_KAFKA_RESP_ERR_NO_ERROR:
if($this->worker_process_num>0)
{
$worker->push(\json_encode($msg));
} else {
$this->handleLogic(\json_encode($msg));
}
break;
case RD_KAFKA_RESP_ERR__PARTITION_EOF:
case RD_KAFKA_RESP_ERR__TIMED_OUT:
break;
default:
$this->error("kafka consumer failed");
throw new \Exception($msg->errstr(), $msg->err);
break;
}
}
$topic->consumeStop(0);
}
}