我已经在php中使用RDKAFKA来实现运行中的使用者并行性。
但是第一位使用者消耗了该主题的所有消息,因此第二位使用者没有获得该主题的消息。
所以我对不同的消费者使用了不同的组ID,仍然存在相同的问题。
请帮助我。
$
conf-> set('group.id','myConsumerGroup');
$
conf-> set('metadata.broker.list','127.0.0.2');
$
topicConf = new RdKafka \ TopicConf();
$
topicConf-> set('auto.offset.reset','smallest');
$
conf-> setDefaultTopicConf($
topicConf);
$
consumer1 =新的RdKafka \ KafkaConsumer($
conf);
$
consumer1-> subscribe(['dbtest']);
而(true){
$
j = 0;
$
消息= $
consumer1-> consume(120 * 1000);
开关($
消息->错误){
情况RD_KAFKA_RESP_ERR_NO_ERROR:
$
dataEx = json_decode($
message-> payload,true);
var_dump($
data);
$
sql =“将INSERT插入emp(名称,电子邮件)值('”。$
dataEx ['name']。“','.. $
dataEx ['email'] 。“')”;
`$`servername = "localhost";
`$`username = "A";
`$`password = "ASD";
`$`dbname = "test";
`$`conn = new mysqli(`$`servername, `$`username, `$`password, `$`dbname);
if (`$`conn->connect_error) {
die("Connection failed: " . `$`conn->connect_error);
}
if (`$`conn->query(`$`sql) === TRUE) {
echo "New record created successfully to datbase ".`$`dbname."/n";
} else {
echo "Error: " . `$`sql . "<br>" . `$`conn->error;
}
`$`conn->close();
echo "produced `$`j----------------------<br> ";
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";
break;
default:
throw new \Exception(`$`message->errstr(), `$`message->err);
break;
}
$j++;
}
回声“这里”;
$
conf-> set('group.id','myConsumerGroup1');
$
conf-> set('metadata.broker.list','127.0.0.2');
$
topicConf = new RdKafka \ TopicConf();
$
topicConf-> set('auto.offset.reset','smallest');
$
conf-> setDefaultTopicConf($
topicConf);
$
consumer2 =新的RdKafka \ KafkaConsumer($
conf);
回声“ sonali”;
$
consumer2-> subscribe(['dbtest']);
而(true){
$
message2 = $
consumer2-> consume(120 * 1000);
切换($
message2-> err){
情况RD_KAFKA_RESP_ERR_NO_ERROR:
$
dataEx = json_decode($
message2-> payload,true);
``$ sql =“将INSERT插入emp(名称,电子邮件)值('”。$ dataEx ['name']。“','”。$
dataEx ['email']。“')”;
$ servername1 =“本地主机”;
$ username1 =“ A”;
$ password1 =“ ASD”;
$ dbname1 =“ test1”;
$
conn1 =新的mysqli($
servername1,$
用户名1,$
密码1,$
dbname1);
如果($
conn1-> connect_error){
die(“连接失败:”。$
conn1-> connect_error);
}
if (`$`conn1->query(`$`sql) === TRUE) {
echo "New record created successfully ".`$`dbname1;
} else {
echo "Error: " . `$`sql . "<br>" . `$`conn1->error;
}
`$`conn1->close();
echo "produced `$`j----------------------<br> ";
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";
break;
default:
throw new \Exception(`$`message->errstr(), `$`message->err);
break;
}
`$`j++;
}