美文网首页
安装kafka

安装kafka

作者: 王小板爱吃鱼 | 来源:发表于2023-02-22 17:09 被阅读0次

安装java

安装kafka

下载

https://kafka.apache.org/downloads

解压
tar -xzf kafka_2.13-3.4.0.tgz
cd kafka_2.13-3.4.0
Kafka with ZooKeeper
# Start the ZooKeeper service
$ bin/zookeeper-server-start.sh config/zookeeper.properties

启动另一个窗口启动kafka

# Start the Kafka broker service
$ bin/kafka-server-start.sh config/server.properties
创建一个topic
bin/kafka-topics.sh --create --topic quickstart-events --bootstrap-server localhost:9092

查看topic信息

bin/kafka-topics.sh --describe --topic quickstart-events --bootstrap-server localhost:9092
安装PHP扩展

rdkafka 安装需要依赖 librdkafka , 所以先安装 librdkafka

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

安装 php-rdkafka 扩展
在pecl

phpize
./configure --with-php-config=/usr/local/Cellar/php@7.2/7.2.24/bin/php-config  ## 这里根据自己的情况填写路径
make && make install

php.ini添加

extension=rdkafka.so
php使用
<?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!');
        }
    }
}
<?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;
            }
        }
    }
}
安装扩展类提示包,对ide友好,因为pecl包ide不会自动补全
composer require kwn/php-rdkafka-stubs --dev

相关文章

网友评论

      本文标题:安装kafka

      本文链接:https://www.haomeiwen.com/subject/kzuakdtx.html