這篇文章給大家分享的是有關(guān)如何安裝php+kafka的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
創(chuàng)新互聯(lián)建站"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機(jī)版的企業(yè)網(wǎng)站。實(shí)現(xiàn)跨屏營(yíng)銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動(dòng)網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營(yíng)銷需求!創(chuàng)新互聯(lián)建站具備承接各種類型的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)項(xiàng)目的能力。經(jīng)過(guò)10余年的努力的開(kāi)拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評(píng)。
1、 安裝java,并設(shè)置相關(guān)的環(huán)境變量
> wget https://download.java.net/openjdk/jdk7u75/ri/openjdk-7u75-b13-linux-x64-18_dec_2014.tar.gz > tar zxvf openjdk-7u75-b13-linux-x64-18_dec_2014.tar.gz > mv java-se-7u75-ri/ /opt/ > export JAVA_HOME=/opt/java-se-7u75-ri > export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin > export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar #驗(yàn)證安裝 > java -verison openjdk version "1.7.0_75" OpenJDK Runtime Environment (build 1.7.0_75-b13) OpenJDK 64-Bit Server VM (build 24.75-b04, mixed mode)
2、安裝kafka,這里以0.10.2版本為例
> wget http://archive.apache.org/dist/kafka/0.10.2.0/kafka_2.11-0.10.2.0.tgz > tar zxvf kafka_2.11-0.10.2.0.tgz > mv kafka_2.11-0.10.2.0/ /opt/kafka > cd /opt/kafka #啟動(dòng)zookeeper > bin/zookeeper-server-start.sh config/zookeeper.properties [2013-04-22 15:01:37,495] INFO Reading configuration from: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig) ... #啟動(dòng)kafka > bin/kafka-server-start.sh config/server.properties [2013-04-22 15:01:47,028] INFO Verifying properties (kafka.utils.VerifiableProperties) [2013-04-22 15:01:47,051] INFO Property socket.send.buffer.bytes is overridden to 1048576 (kafka.utils.VerifiableProperties) ... #嘗試創(chuàng)建一個(gè)topic > bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test > bin/kafka-topics.sh --list --zookeeper localhost:2181 test #生產(chǎn)者寫入消息 > bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test This is a message This is another message #消費(fèi)者消費(fèi)消息 > bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning This is a message This is another message
3、安裝kafka的C操作庫(kù)
> wget https://github.com/edenhill/librdkafka/archive/v1.3.0.tar.gz > tar zxvf v1.3.0.tar.gz > cd librdkafka-1.3.0 > ./configure > make && make install
4、安裝php的kafka擴(kuò)展 ,這里選擇php-rdkafka擴(kuò)展 https://github.com/arnaud-lb/php-rdkafka
> wget https://github.com/arnaud-lb/php-rdkafka/archive/4.0.2.tar.gz > tar 4.0.2.tar.gz > cd php-rdkafka-4.0.2 > /opt/php7/bin/phpize > ./configure --with-php-config=/opt/php7/bin/php-config > make && make install
修改php.ini,加入 extension=rdkafka.so
5、安裝rdkafka的IDE代碼提示文件
> composer create-project kwn/php-rdkafka-stubs php-rdkafka-stubs
以phpstrom為例,在你的項(xiàng)目的External Libraries右鍵,選擇Configure PHP Include Paths,把剛剛的路徑添加進(jìn)來(lái)。
6、編寫php測(cè)試代碼
producer:
set('log_level', LOG_ERR); $conf->set('debug', 'admin'); $conf->set('metadata.broker.list', 'localhost:9092'); //If you need to produce exactly once and want to keep the original produce order, uncomment the line below //$conf->set('enable.idempotence', 'true'); $producer = new RdKafka\Producer($conf); $topic = $producer->newTopic("test2"); for ($i = 0; $i < 10; $i++) { $topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message $i"); $producer->poll(0); } for ($flushRetries = 0; $flushRetries < 10; $flushRetries++) { $result = $producer->flush(10000); if (RD_KAFKA_RESP_ERR_NO_ERROR === $result) { break; } } if (RD_KAFKA_RESP_ERR_NO_ERROR !== $result) { throw new \RuntimeException('Was unable to flush, messages might be lost!'); }
low-level consumer:
set('log_level', LOG_ERR); $conf->set('debug', 'admin'); // Set the group id. This is required when storing offsets on the broker $conf->set('group.id', 'myConsumerGroup'); $rk = new RdKafka\Consumer($conf); $rk->addBrokers("127.0.0.1"); $topicConf = new RdKafka\TopicConf(); $topicConf->set('auto.commit.interval.ms', 100); // Set the offset store method to 'file' $topicConf->set('offset.store.method', 'broker'); // Alternatively, set the offset store method to 'none' // $topicConf->set('offset.store.method', 'none'); // Set where to start consuming messages when there is no initial offset in // offset store or the desired offset is out of range. // 'smallest': start from the beginning $topicConf->set('auto.offset.reset', 'smallest'); $topic = $rk->newTopic("test2", $topicConf); // Start consuming partition 0 $topic->consumeStart(0, RD_KAFKA_OFFSET_STORED); while (true) { $message = $topic->consume(0, 10000); switch ($message->err) { case RD_KAFKA_RESP_ERR_NO_ERROR: print_r($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; } }
high-level consumer:
setRebalanceCb(function (RdKafka\KafkaConsumer $kafka, $err, array $partitions = null) { switch ($err) { case RD_KAFKA_RESP_ERR__ASSIGN_PARTITIONS: echo "Assign: "; var_dump($partitions); $kafka->assign($partitions); break; case RD_KAFKA_RESP_ERR__REVOKE_PARTITIONS: echo "Revoke: "; var_dump($partitions); $kafka->assign(NULL); break; default: throw new \Exception($err); } }); // Configure the group.id. All consumer with the same group.id will consume // different partitions. $conf->set('group.id', 'myConsumerGroup'); // Initial list of Kafka brokers $conf->set('metadata.broker.list', '127.0.0.1'); // Set where to start consuming messages when there is no initial offset in // offset store or the desired offset is out of range. // 'smallest': start from the beginning $conf->set('auto.offset.reset', 'smallest'); $consumer = new RdKafka\KafkaConsumer($conf); // Subscribe to topic 'test2' $consumer->subscribe(['test2']); echo "Waiting for partition assignment... (make take some time when\n"; echo "quickly re-joining the group after leaving it.)\n"; while (true) { $message = $consumer->consume(1000); switch ($message->err) { case RD_KAFKA_RESP_ERR_NO_ERROR: print_r($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; } sleep(2); }
感謝各位的閱讀!關(guān)于“如何安裝php+kafka”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!