高阶API是指API侧提供了自动负载均衡,不需要业务去处理。
abstract class KafkaConsumerHighService extends KafkaConsumerBaseService
{
function consumer($partion_id, $worker)
{
$conf = new \RdKafka\Conf();
//这里就是自动均衡分配分区给消费者的意思
$conf->setRebalanceCb(function(\RdKafka\KafkaConsumer $kafka, $err, array $partitions = null){
switch($err) {
case RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS:
$kafka->assign($partitions);
break;
case RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS:
$kafka->assign(null);
break;
default:
throw new \Exception($err);
}
});
$conf->set('group.id', 'comsumer-group-'.$this->topic);
$conf->set('client.id', 'client-'.$this->topic);
$conf->set('bootstrap.servers', $this->brokers);
$topicConf = new \R