關於phpthriftserver的信息

本文目錄一覽:

thrift server timed out超時

寫於2020-01-13

一、故障描述

時間是2017年的某天:

有個服務,Python+Thrift做的Server;

對應庫及版本:Cython==0.23.5,thriftpy==0.3.7。

相關配置: 服務超時時間3s

線上使用Supervisor管理進程。

一直跑着沒問題,有天運維同學提問題,該服務日誌出現大量time out(那時候Kibana還沒搭起來,日誌只能在服務器上看)。日誌如下:

二、排查過程

猜測:

驗證:

三、溝通問題

四、收穫

Thrift server比較和使用

Thrift提供了多種服務器實現。

TSimplerServer在while循環中每次接受一個連接,處理連接請求,直到客戶端關閉了連接,它才會去接受一個新的連接。由於它只在一個單獨的線程中以阻塞I/O的方式完成這些工作,所以它只能服務一個客戶端連接,其他所有客戶端在被服務器端接受之前都只能等待。其使用方法如下:

TNonblockingServer 使用非阻塞的 I/O 解決了TSimpleServer一個客戶端阻塞其他所有客戶端的問題。它使用了java.nio.channels.Selector,通過調用select(),它使得你阻塞在多個連接上,而不是阻塞在單一的連接上。當一或多個連接準備好被接受/讀/寫時,select()調用便會返回。TNonblockingServer處理這些連接的時候,要麼接受它,要麼從它那讀數據,要麼把數據寫到它那裡,然後再次調用select()來等待下一個可用的連接。通用這種方式,server可同時服務多個客戶端,而不會出現一個客戶端把其他客戶端全部“餓死”的情況。

使用方法:

ThreadedSelectorServer允許你用多個線程來處理網絡 I/O。它維護了兩個線程池,一個用來處理網絡 I/O,另一個用來進行請求的處理。使用方法:

TThreadPoolServer有一個專用的線程用來接受連接旦接受了一個連接,它就會被放入ThreadPoolExecutor中的一個 worker 線程里處理。worker 線程被綁定到特定的客戶端連接上,直到它關閉。一旦連接關閉,該worker線程就又回到了線程池中。你可以配置線程池的最小、最大線程數,默認值分別是5(最小)和Integer.MAX_VALUE(最大)。使用方法:

如何設置php thrift 超時時間

最近需要用到Thrift接口,

是Facebook開發的apache開源項目,公司要用,研究了一下

所以寫了個PHP調用Thrift的方法事例

以下是代碼,以免以後別人再走彎路

我是在Yii框架中實現的,和原生代碼應該是一樣的

官方下載包里也有PHP調用Thrift的例子

?php

/**

* @author fzxa

* @create 2012/05/22

Thrift推薦職位相關接口

註:詳細說明文檔在 \protected\components\thrift\推薦系統API.docx

@desc 說明Thrift接口數據格式:

EntityType接口數據格式

enum EntityType {

POSITION = 0,

RESUME = 1

}

EntityInfo接口數據格式

struct EntityInfo {

1: EntityType type,

2: string id, // 實體id

3: double rank, // rank值

4: string address, // 工作地點

5: i32 salary, // 薪水

6: string industry, // 行業類別

7: string profession, // 職業類別

8: i32 workingage, // 工作年限

9: i32 degree, // 學歷

10: string time, // 過期時間或更新時間

11: mapstring,string descriptions, // 文字描述,其中key為字段名,value為字段內容

12: mapstring,i32 tags, // 技能標籤

}

ResultInfo接口數據格式

struct ResultInfo {

1: EntityType type,

2: string id, // 實體id

3: i32 score, // 推薦分數,取值範圍:[0, 100]

}

*/

$GLOBALS[‘THRIFT_ROOT’] = $_SERVER[‘DOCUMENT_ROOT’].’/p/protected/components/thrift’;

require_once( $GLOBALS[‘THRIFT_ROOT’] . ‘/Thrift.php’ );

require_once( $GLOBALS[‘THRIFT_ROOT’] . ‘/transport/TSocket.php’ );

require_once( $GLOBALS[‘THRIFT_ROOT’] . ‘/transport/TBufferedTransport.php’ );

require_once( $GLOBALS[‘THRIFT_ROOT’] . ‘/protocol/TBinaryProtocol.php’ );

//生成的文件 RecommendService.php

require_once( $GLOBALS[‘THRIFT_ROOT’] . ‘/RecommendService.php’ );

//ini_set(‘display_errors’, E_ALL);

class ThriftInterface

{

private static $thriftHost = ‘*.*.*.*’; //Thrift接口服務器IP

private static $thriftPort = 8080; //Thrift端口

/**

* 建立Thrift連接

* @return boolean

*/

private static function client(){

$socket = new TSocket(self::$thriftHost, self::$thriftPort);

$socket-setSendTimeout(10000);

$socket-setRecvTimeout(20000);

$transport = new TBufferedTransport($socket);

$protocol = new TBinaryProtocol($transport);

$client = new RecommendServiceClient($protocol);

$transport-open();

$socket-setDebug(TRUE);

return $client;

}

/**

* 獲取職位推薦列表ById

*

* @param Number $type 0:代表職位,1:代表簡歷

* @param Number $id 實體ID

* @param Array 推薦結果列表

* @example: $Recommend = ThriftInterface::getRecommendedResultById(‘1′,’1982’);

*/

public static function getRecommendedResultById($type,$id){

$client = self::client();

$ResultById = $client-getRecommendedResultById($type, $id);

return $ResultById;

}

/**

* 獲取推薦列表ByEntity

* @param EntityInfo $entity 職位或簡歷實體信息

*/

public static function getRecommendedResultByEntity($entity){

$client = self::client();

$EntityInfo = new EntityInfo();

$EntityInfo-type = (string)$entity[‘type’];

$EntityInfo-id = (string)$entity[‘id’];

$EntityInfo-rank = (float)$entity[‘rank’];

$EntityInfo-address = (string)$entity[‘address’];

$EntityInfo-salary = (int)$entity[‘salary’];

$EntityInfo-industry = (string)$entity[‘industry’];

$EntityInfo-profession = (string)$entity[‘profession’];

$EntityInfo-workingage = (int)$entity[‘workingage’];

$EntityInfo-degree = (int)$entity[‘degree’];

$EntityInfo-time = (string)$entity[‘time’];

$EntityInfo-descriptions = (array)$entity[‘descriptions’];

$EntityInfo-tags = (array)$entity[‘tags’];

$ResultByEntity = $client-getRecommendedResultByEntity($EntityInfo);

return $ResultByEntity;

}

/**

* 計算任一簡歷與職位的匹配分數ById

*

* @param Number $type1 0:代表職位,1:代表簡歷

* @param Number $id1 實體ID

* @param Number $type2 0:代表職位,1:代表簡歷

* @param Number $id2 實體ID

* @example Number

*/

public static function getRecommendedScoreById($type1, $id1, $type2, $id2){

$client = self::client();

$ScoreById = $client-getRecommendedScoreById($type1, $id1, $type2, $id2);

return $ScoreById;

}

/**

* 計算任一簡歷與職位的匹配分數ByEntity

*

* @param EntityInfo $entity 實體的所有信息

* @param EntityType $type2

* @param string $id2 實體ID2

* @return i32 簡歷與職位的推薦分數,取值範圍:[0, 100]

*/

public static function getRecommendedScoreByEntity($entity, $type2, $id2){

$client = self::client();

$EntityInfo = new EntityInfo();

$EntityInfo-type = (string)$entity[‘type’];

$EntityInfo-id = (string)$entity[‘id’];

$EntityInfo-rank = (float)$entity[‘rank’];

$EntityInfo-address = (string)$entity[‘address’];

$EntityInfo-salary = (int)$entity[‘salary’];

$EntityInfo-industry = (string)$entity[‘industry’];

$EntityInfo-profession = (string)$entity[‘profession’];

$EntityInfo-workingage = (int)$entity[‘workingage’];

$EntityInfo-degree = (int)$entity[‘degree’];

$EntityInfo-time = (string)$entity[‘time’];

$EntityInfo-descriptions = (array)$entity[‘descriptions’];

$EntityInfo-tags = (array)$entity[‘tags’];

$ResultInfo = new ResultInfo();

$ResultInfo-type = (string)$type2[‘type’];

$ResultInfo-id = (string)$type2[‘id’];

$ResultInfo-score = (int)$type2[‘score’];

$ScoreByEntity = $client-getRecommendedScoreByEntity($EntityInfo, $type2, $id2);

return $ScoreByEntity;

}

}

轉載

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/194776.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-02 14:41
下一篇 2024-12-02 14:41

相關推薦

  • Java 監控接口返回信息報錯信息怎麼處理

    本文將從多個方面對 Java 監控接口返回信息報錯信息的處理方法進行詳細的闡述,其中包括如何捕獲異常、如何使用日誌輸出錯誤信息、以及如何通過異常處理機制解決報錯問題等等。以下是詳細…

    編程 2025-04-29
  • 使用Python爬蟲獲取電影信息的實現方法

    本文將介紹如何使用Python編寫爬蟲程序,來獲取和處理電影數據。需要了解基本的Python編程語言知識,並使用BeautifulSoup庫和Requests庫進行爬取。 一、準備…

    編程 2025-04-28
  • Python爬取網頁信息

    本文將從多個方面對Python爬取網頁信息做詳細的闡述。 一、爬蟲介紹 爬蟲是一種自動化程序,可以模擬人對網頁進行訪問獲取信息的行為。通過編寫代碼,我們可以指定要獲取的信息,將其從…

    編程 2025-04-28
  • 如何使用Python執行Shell命令並獲取執行過程信息

    本文將介紹如何使用Python執行Shell命令並獲取執行過程信息。我們將從以下幾個方面進行闡述: 一、執行Shell命令 Python內置的subprocess模塊可以方便地執行…

    編程 2025-04-28
  • Python實現身份信息模擬生成與查驗

    本文將從以下幾個方面對Python實現身份信息模擬生成與查驗進行詳細闡述: 一、身份信息生成 身份信息生成是指通過代碼生成符合身份信息規範的虛假數據。Python中,我們可以使用f…

    編程 2025-04-27
  • Dapper使用getschema獲取表信息

    本文旨在介紹Dapper中使用getschema獲取表信息的方法和注意事項。 一、獲取某張表的所有列信息 使用Dapper獲取某張表信息,可以使用 `IDbConnection.G…

    編程 2025-04-27
  • 已裝備我軍的空中信息化作戰平台

    本文將會從多個方面詳細闡述已裝備我軍的空中信息化作戰平台。 一、平台概述 已裝備我軍的空中信息化作戰平台是一個全新的作戰系統,具備實時數據採集、處理、分析、共享的能力。它可以在不同…

    編程 2025-04-27
  • 通過提交信息搜索-使用git

    本篇文章重點講解如何使用git通過提交信息來搜索。我們將從多個方面介紹如何使用git來搜索提交信息,並提供相應的代碼示例以供參考。 一、搜索方式 Git提供了三種搜索方式,分別為:…

    編程 2025-04-27
  • Linux查看系統信息

    一、CPU信息 Linux系統下,查看CPU的信息最常用的命令是lscpu。該命令可以顯示CPU架構、核心數量、線程數、緩存大小、CPU頻率等信息。例如: lscpu 該命令會輸出…

    編程 2025-04-24
  • 軟考 信息安全工程師

    軟考 信息安全工程師是一項技能型國家級資格認證考試,主要測試考生在信息安全領域的理論知識和實踐技能,是證明個人信息安全能力的重要證書。本文將從多個方面對軟考 信息安全工程師做詳細的…

    編程 2025-04-23

發表回復

登錄後才能評論