装置 Kafka 效劳
间接到 kafka 官网 , 下载最新的
wget https://mirror.bit.edu.cn/apache/kafka/2.5.0/kafka_2.13-2.5.0.tgz
解压,进入目次
tar -zxvf kafka_2.13-2.5.0.tgz cd kafka_2.13-2.5.0
启动 Kafka 效劳
应用装置包中的剧本启动单节点 Zookeeper 实例
bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
应用 kafka-server-start.sh 启动 kafka 效劳
bin/kafka-server-start.sh config/server.properties
创立 topic
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
查看 topic 列表,反省能否创立胜利
bin/kafka-topics.sh --list --zookeeper localhost:2181 $ test
消费者,发送音讯
bin/kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic test
效劳方面到这里就差没有多了,接上去就是 php 的事了。
装置 PHP 扩大
rdkafka 装置需求依赖 librdkafka , 以是先装置 librdkafka
git clone https://github.com/edenhill/librdkafka.git cd librdkafka ./configure make && make install
装置 php-rdkafka 扩大
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 ## 这里依据本人的状况填写门路 make && make install
正在 php-ini 加之
extension=rdkafka.so
重启,php-fpm,就应该能够看到该扩大。
应用Kafka
创立一个消费者类
<?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.co妹妹it.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; } } } }
成绩汇总
一、 No Java runtime present, requesting install
由于 kafka 需求 java 环境支持,以是装置 java 环境。能够到 javase-jdk14-downloads 抉择本人的版本进行下载装置
二、创立 topic 呈现:Replication factor: 1 larger than available brokers: 0
意义是至多有一个 brokers. 也就是说并无无效的 brokers 能够用。你要确保你的 kafka 曾经启动了
保举教程:《PHP教程》
以上就是PHP 音讯行列步队 Kafka 应用的具体内容,更多请存眷资源魔其它相干文章!
标签: php php开发教程 php开发资料 php开发自学 kafka
抱歉,评论功能暂时关闭!