关于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/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

发表回复

登录后才能评论