Laravel uses rdkafka

The development environment uses laradock, install kafka and expand, you can read historical articles

After configuration, laravel uses kafka code

Add the following lines to the .env file in your Laravel project to configure Kafka:

KAFKA_BROKERS=kafka:9092
KAFKA_TOPIC=my-topic

producer

use RdKafka\Producer;
use RdKafka\ProducerTopic;
use RdKafka\Conf;

$config = new Conf();
$config->set('metadata.broker.list', env('KAFKA_BROKERS'));

$producer = new Producer($config);
$topic = $producer->newTopic(env('KAFKA_TOPIC'));

$message = "Hello, Kafka!";
$topic->produce(RD_KAFKA_PARTITION_UA, 0, $message);

$producer->poll(0);

In this example, we use  .env the Kafka broker and topic configured in Laravel's files. Since we set RD_KAFKA_PARTITION_UA, the message will be broadcast to all Kafka partitions.

consumer

use RdKafka\Conf;
use RdKafka\KafkaConsumer;
use RdKafka\TopicPartition;

$conf = new Conf();
$conf->set('group.id', 'my-group');
$conf->set('metadata.broker.list', env('KAFKA_BROKERS'));

$consumer = new KafkaConsumer($conf);
$consumer->subscribe([env('KAFKA_TOPIC')]);

while (true) {
    $message = $consumer->consume(120 * 1000);
    
    if ($message) {
        switch ($message->err) {
            case RD_KAFKA_RESP_ERR_NO_ERROR:
                // 处理消息
                echo sprintf(
                    "Message payload: %s\n",
                    $message->payload
                );
                break;
            case RD_KAFKA_RESP_ERR__TIMED_OUT:
                break;
            default:
                throw new \Exception($message->errstr(), $message->err);
        }
    }
}

In this example, we first create a Kafka consumer and subscribe to the topic we want to consume. We then enter an infinite loop where we use  consume methods to read messages from Kafka. If there is no message, we wait for a timeout (we set it to 120 seconds). If a new message arrives, we first check its error code to see if it is valid, and then process the message if necessary.

The core is the configuration item kafka:9092

When using the web interface fpm to consume, it will prompt code -185, local time out

Network problem, use cli mode consumption to solve the problem

Guess you like

Origin blog.csdn.net/qiuziqiqi/article/details/130205062