PHP message queuing Kafka uses

Install Kafka service

Go directly to kafka website and download the latest

wget https://mirror.bit.edu.cn/apache/kafka/2.5.0/kafka_2.13-2.5.0.tgz

  

Unzip, enter directory

tar -zxvf kafka_2.13-2.5.0.tgz
cd kafka_2.13-2.5.0

  

Start Kafka service

Starting a single node Zookeeper instance using the script in the installation package

bin/zookeeper-server-start.sh -daemon config/zookeeper.properties

  

Using kafka server- start.sh Start kafka service

 

bin/kafka-server-start.sh config/server.properties

  

Create topic

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

  

Check the topic list to see if it is created successfully

bin/kafka-topics.sh --list --zookeeper localhost:2181
$ test

  

Producers, sending messages

bin/kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic test

  

The service aspect is almost here. Next is php.

Install PHP extension

The installation of rdkafka depends on librdkafka, so install librdkafka first

git clone https://github.com/edenhill/librdkafka.git
cd librdkafka
./configure
make && make install

  

Install PHP rdkafka extension

git clone https://github.com/arnaud-lb/php-rdkafka.git
cd php-rdkafka
phpize
./configure --with-php-config=/usr/local/Cellar/php@7.2/7.2.24/bin/php-config  ## Here, fill in the path according to your own situation
make && make install

  

Add PHP ini

 

extension=rdkafka.so

  

Restart PHP FPM, and you should see the extension.

Using Kafka

Create a producer class

<?php
class KafkaProducer
{
    public static $brokerList = '127.0.0.1:9092';
    public static function send($message, $topic)
 {
        self::producer($message, $topic);
    }
    public static function producer($message, $topic = 'test')
    {
        $conf = new \RdKafka\Conf();
        $conf->set('metadata.broker.list', self::$brokerList);
        $producer = new \RdKafka\Producer($conf);
        $topic = $producer->newTopic($topic);
        $topic->produce(RD_KAFKA_PARTITION_UA, 0, json_encode($message));
        $producer->poll(0);
        $result = $producer->flush(10000);
        if (RD_KAFKA_RESP_ERR_NO_ERROR !== $result) {
            throw new \RuntimeException('Was unable to flush, messages might be lost!');

      }
    }
}

  

Create a consumption class

<?php
class KafkaConsumer
{
    public static $brokerList = '127.0.0.1:9092';
      public static function consumer()
    {
        $conf = new \RdKafka\Conf();
        $conf->set('group.id', 'test');
        $rk = new \RdKafka\Consumer($conf);
   $rk->addBrokers("127.0.0.1");
        $topicConf = new \RdKafka\TopicConf();
        $topicConf->set('auto.commit.interval.ms', 100);
        $topicConf->set('offset.store.method', 'broker');
        $topicConf->set('auto.offset.reset', 'smallest');
        $topic = $rk->newTopic('test', $topicConf);
        $topic->consumeStart(0, RD_KAFKA_OFFSET_STORED);
        while (true) {
            $message = $topic->consume(0, 120*10000);
            switch ($message->err) {
                case RD_KAFKA_RESP_ERR_NO_ERROR:
                    var_dump($message);
                    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;
            }
        }
    }
}

  

Problem summary

1, No Java runtime present, requesting install

Because kafka needs java environment support, install java environment. You can go to javase-jdk14-downloads and choose your own version to download and install

2. Create topic: Replication factor: 1 larger than available brokers: 0

That is to say, there is no effective brokers available. You need to make sure your kafka is activated

Tags: PHP kafka Zookeeper git

Posted on Tue, 19 May 2020 07:36:32 -0700 by dmeade