osx - 請教大神們:用php用戶端怎麼連kafka,怎麼做到監控broker變化而來重新整理用戶端資料的?

來源:互聯網
上載者:User
最近在研究用php連kafka.
用的是githup上的nmred/kafka-php項目代碼
目前:
1.已經可以串連伺服器上的kafka,
2.測試:命令列執行php Produce.php,consumer端也能擷取得到資料
問題:
1.consumer端怎麼一直執行,難道寫 while死迴圈?
2.kafka-php是怎麼做到用戶端隔段時間拉取新的資訊,並重新整理用戶端資料的?
3.在README.md有這麼一句話,又是什麼意思:

 Watches broker state, if broker changes, the client will refresh     broker and topic metadata stored in the client?

請使用過該工具的賜教思路,不甚感激!

補充:現在我的php用戶端 consumer能收到資料了,但是一直都包含亂碼,比如我在producer段輸入:nihao,則php consumer.php後,會輸出:
�m�5����3�SNAPPY`���;����nihao

現在的疑問是:
1.為什麼producer端輸入的是:nihao,但在consumer端輸出的資訊為什麼不是 nihao?
2.為什麼會有一串亂碼?
3.怎麼解決?
4.下面附上socket讀取的核心代碼:

// 擷取consumer端的資訊,其中$this->stream是一個socket串連成功的socket對象:
$msg = $this->stream->read($messageSize, true);

read方法核心代碼如下:
$readable = @stream_select($read, $null, $null, $this->recvTimeoutSec, $this->recvTimeoutUsec);

    if ($readable > 0) {        $remainingBytes = $len;        $data = $chunk = '';        while ($remainingBytes > 0) {            $chunk = fread($this->stream, $remainingBytes);            if ($chunk === false) {                $this->close();                throw new \Kafka\Exception\SocketEOF('Could not read '.$len.' bytes from stream (no data)');            }            if (strlen($chunk) === 0) {                // Zero bytes because of EOF?                if (feof($this->stream)) {                    $this->close();                    throw new \Kafka\Exception\SocketEOF('Unexpected EOF while reading '.$len.' bytes from stream (no data)');                }                // Otherwise wait for bytes                $readable = @stream_select($read, $null, $null, $this->recvTimeoutSec, $this->recvTimeoutUsec);                if ($readable !== 1) {                    throw new \Kafka\Exception\SocketTimeout('Timed out reading socket while reading ' . $len . ' bytes with ' . $remainingBytes . ' bytes to go');                }                continue; // attempt another read            }            $data .= $chunk;            $remainingBytes -= strlen($chunk);        }

通過這個while迴圈來拼接資訊。重點在於 fread出來的 $chunk就包含亂碼

呼高手!

回複內容:

最近在研究用php連kafka.
用的是githup上的nmred/kafka-php項目代碼
目前:
1.已經可以串連伺服器上的kafka,
2.測試:命令列執行php Produce.php,consumer端也能擷取得到資料
問題:
1.consumer端怎麼一直執行,難道寫 while死迴圈?
2.kafka-php是怎麼做到用戶端隔段時間拉取新的資訊,並重新整理用戶端資料的?
3.在README.md有這麼一句話,又是什麼意思:

 Watches broker state, if broker changes, the client will refresh     broker and topic metadata stored in the client?

請使用過該工具的賜教思路,不甚感激!

補充:現在我的php用戶端 consumer能收到資料了,但是一直都包含亂碼,比如我在producer段輸入:nihao,則php consumer.php後,會輸出:
�m�5����3�SNAPPY`���;����nihao

現在的疑問是:
1.為什麼producer端輸入的是:nihao,但在consumer端輸出的資訊為什麼不是 nihao?
2.為什麼會有一串亂碼?
3.怎麼解決?
4.下面附上socket讀取的核心代碼:

// 擷取consumer端的資訊,其中$this->stream是一個socket串連成功的socket對象:
$msg = $this->stream->read($messageSize, true);

read方法核心代碼如下:
$readable = @stream_select($read, $null, $null, $this->recvTimeoutSec, $this->recvTimeoutUsec);

    if ($readable > 0) {        $remainingBytes = $len;        $data = $chunk = '';        while ($remainingBytes > 0) {            $chunk = fread($this->stream, $remainingBytes);            if ($chunk === false) {                $this->close();                throw new \Kafka\Exception\SocketEOF('Could not read '.$len.' bytes from stream (no data)');            }            if (strlen($chunk) === 0) {                // Zero bytes because of EOF?                if (feof($this->stream)) {                    $this->close();                    throw new \Kafka\Exception\SocketEOF('Unexpected EOF while reading '.$len.' bytes from stream (no data)');                }                // Otherwise wait for bytes                $readable = @stream_select($read, $null, $null, $this->recvTimeoutSec, $this->recvTimeoutUsec);                if ($readable !== 1) {                    throw new \Kafka\Exception\SocketTimeout('Timed out reading socket while reading ' . $len . ' bytes with ' . $remainingBytes . ' bytes to go');                }                continue; // attempt another read            }            $data .= $chunk;            $remainingBytes -= strlen($chunk);        }

通過這個while迴圈來拼接資訊。重點在於 fread出來的 $chunk就包含亂碼

呼高手!

consumer應該只能通過迴圈來判斷broker是否有變化,然後更新說。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.