最近項目開發中需要使用 Kafka
消息隊列。經過檢索,PHP下面有通用的兩種方式來調用 Kafka
。
php-rdkafka 擴展
以 PHP 擴展的形式進行使用是非常高效的。另外,該項目也提供了非常完備的 文檔 。
不過在 Mac 環境中安裝的過程中出現了以下報錯:
$ sudo pecl install rdkafka
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... no
checking for nawk... no
checking for awk... awk
checking if awk is broken... no
checking for rdkafka support... yes, shared
checking for librdkafka/rdkafka.h" in default path... not found
configure: error: Please reinstall the rdkafka distribution
開始以為是因為 pecl
安裝缺少了一些依賴。然后使用了源碼編譯的方式進行安裝:
$ git clone https://github.com/arnaud-lb/php-rdkafka.git
$ cd php-rdkafka
$ phpize
$ ./configure
$ make all -j 5
....
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... no
checking for nawk... no
checking for awk... awk
checking if awk is broken... no
checking for rdkafka support... yes, shared
checking for librdkafka/rdkafka.h" in default path... not found
configure: error: Please reinstall the rdkafka distribution
同樣報錯了。后來仔細看文檔才發現。這里有一個依賴:librdkafka 。
然后安裝它:
$ git clone https://github.com/edenhill/librdkafka.git
$ cd librdkafka
$ ./configure
$ make && make install
再執行 sudo pecl install rdkafka
,執行OK。
然后將 rdkafka.so
添加到相應的 /path/to/php.ini
的末尾即可。
執行 php -m | grep rdkafka
,驗證是否添加完成。
kafka-php 擴展包
Kafka-php 使用純粹的 PHP 編寫的 Kafka
客戶端,目前支持 0.8.x
以上版本的 Kafka
。由於使用 PHP 語言編寫所以不用編譯任何的擴展就可以使用,降低了接入與維護成本。