首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 安裝 yar + msgpack 擴展實現高性能 RPC 通信

     編輯:PHP 相關知識

擴展介紹

  • YAR (Yet Another RPC):PHP 官方 RPC 框架,支持 TCP/HTTP 協議、同步/異步調用、多種打包協議
  • Msgpack:高效二進制序列化格式,體積小、解析快、跨語言支持好
  • YAF:PHP 高性能 MVC 框架,常與 YAR 配合使用

編譯安裝步驟

1. 安裝依賴

# Ubuntu/Debian
apt-get install php-dev php-pear libmsgpack-dev

# CentOS/RHEL
yum install php-devel php-pear msgpack-devel

2. 安裝 Msgpack PHP 擴展

pecl install msgpack
# 或源碼編譯
wget https://pecl.php.net/get/msgpack-2.2.0.tgz
tar xzf msgpack-2.2.0.tgz
cd msgpack-2.2.0
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

3. 安裝 YAR 擴展(關鍵:啟用 msgpack 支持)

pecl install yar
# 或源碼編譯
wget https://pecl.php.net/get/yar-2.3.2.tgz
tar xzf yar-2.3.2.tgz
cd yar-2.3.2
phpize
./configure --enable-msgpack --with-php-config=/usr/local/php/bin/php-config
make && make install

關鍵參數:--enable-msgpack 啟用 YAR 對 Msgpack 打包器的支持,不加則默認僅支持 JSON/serialize。

4. 安裝 YAF(可選)

pecl install yaf

5. 啟用擴展

# php.ini 添加
extension=msgpack.so
extension=yar.so
extension=yaf.so

# 重載 PHP-FPM / Apache
systemctl reload php-fpm

驗證安裝

php -i | grep msgpack
# 輸出應包含:
# yar.packager => msgpack => msgpack

php -m | grep -E 'yar|msgpack|yaf'

YAR 客戶端調用示例

<?php
class YarKeywordClient
{
    protected const HOST = 'kwdt.yarc.service.weibo.com';
    protected const PORT = 7002;

    public function detect(string $text, array $types = [1,2,3], int $returnText = 1): array
    {
        if (!class_exists('Yar_Client')) {
            throw new RuntimeException('Yar_Client not available');
        }

        $client = new Yar_Client("tcp://" . self::HOST . ":" . self::PORT);
        $client->opt(YAR_OPT_PACKAGER, 'msgpack'); // 顯式指定 msgpack
        $client->opt(YAR_OPT_TIMEOUT, 5000);       // 5秒超時

        try {
            return $client->detect($text, $returnText, $types);
        } catch (Exception $e) {
            error_log('YAR call failed: ' . $e->getMessage());
            return ['error' => $e->getMessage()];
        }
    }
}

// 使用
$client = new YarKeywordClient();
$result = $client->detect('測試文本內容');
print_r($result);
/*
Array
(
    [0] => -1
    [1] => no keyword occured
)
*/
?>

YAR 服務端示例(簡略)

<?php
class KeywordService
{
    public function detect(string $text, int $returnText, array $types): array
    {
        // 業務邏輯:關鍵詞檢測...
        return [-1, 'no keyword occured'];
    }
}

$server = new Yar_Server(new KeywordService());
$server->handle();

常見問題排查

問題原因解決
Class 'Yar_Client' not foundyar.so 未加載或版本不匹配檢查 php -m、php.ini、重載 PHP-FPM
Unsupported packager: msgpack編譯 YAR 時未加 --enable-msgpack重新編譯 YAR 並加入參數
連接超時網絡不通、端口錯誤、防火牆telnet host port 測試連通性
返回亂碼打包器不一致客戶端 server 統一使用 msgpack

性能優化建議

  • 使用 持久連接:Yar_Client::setOpt(YAR_OPT_CONNECT_TIMEOUT, ...) 配合連接池
  • 異步並發:Yar_Concurrent_Client 批量發起調用,loop() 統一收集結果
  • 連接池:Swoole/RoadRunner 環境下維護 YAR 連接池,避免頻繁建連
  • 監控:記錄調用延遲、錯誤率、QPS,接入 Prometheus/Grafana
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved