如何设置php thrift 超时时间

如何设置php thrift 超时时间,第1张

最近需要用到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: map<string,string>descriptions, // 文字描述,其中key为字段名,value为字段内容

12: map<string,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

}

}

转载

写于2020-01-13

一、故障描述

时间是2017年的某天:

有个服务,Python+Thrift做的Server;

对应库及版本:Cython==0.23.5,thriftpy==0.3.7。

相关配置: 服务超时时间3s

线上使用Supervisor管理进程。

一直跑着没问题,有天运维同学提问题,该服务日志出现大量time out(那时候Kibana还没搭起来,日志只能在服务器上看)。日志如下:

二、排查过程

猜测:

验证:

三、沟通问题

四、收获


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/7755994.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存