Librdkafka (librdkafka) librdkafka (librdkafka) librdkafka (librdkafka) (librdkafka) But I directly use this development of the time, do not know why not run up, has been reporting errors (should be my too dish, ha ha ha ha ha ha)…… I found some packages on Github that I wanted to use directly, but found that many of them were years old and basically kafka 0.x.

I have a look at the most star package, the link is: github.com/weiboad/kaf…

Enqueue /rdkafka: github.com/php-enqueue…

Here’s the code I tested. It just worked, but it didn’t do what I wanted it to

producers

$connFactory = new RdKafkaConnectionFactory([
    'global'= > ['metadata.broker.list'= >'127.0.0.1:9092'.'socket.timeout.ms'= >'50']]); $context = $connFactory->createContext(); $message = $context->createMessage('hello world! ');
$topic = $context->createTopic('app');
$context->createProducer()->send($topic, $message);
Copy the code

consumers

$config = [
    'global'= > ['group.id' => uniqid(' '.true),
        'metadata.broker.list'= >'127.0.0.1:9092'.'enable.auto.commit'= >'false',].'topic'= > [// Sets the message to be read from the last offset, and no previous messages will be read
        'auto.offset.reset'= >'latest',]]; $connFactory =new RdKafkaConnectionFactory($config);
$context = $connFactory->createContext();
$topic = $context->createTopic('app');
$consumer = $context->createConsumer($topic);
while (true) {
    $message = $consumer->receive(30 * 1000);
    if(! $messageinstanceofRdKafkaMessage && ! $messageinstanceof Message) {
        var_dump($message);
        continue;
    }
    $consumer->acknowledge($message);
    var_dump($message->getBody());
}
Copy the code

That is able to run up, can normal send and receive data, the truth is I’d like to do a queue, producers have multiple, produce the data to a topic, then there are multiple consumers in the consumer, but have a question about this, when I was in launched multiple consumers, every consumer can accept to producers sent message, It is more like group publishing and group subscription, which is not what I want. I went to other tutorials online, and some people said that it is ok to set different group_id, but my group_id is all random, so it is impossible to be the same. Theoretically, it can be realized, but it is not possible, I have tried to use Queue to operate. But all consumers will still receive the message.

At present only understood this place, still need to spend some time to see again, if there is a big god to see, for guidance!!