Yar - Yet Another RPC framework for PHP
Introduction
Yar is a RPC framework which aims to provide a simple and easy way to do communication between PHP applications
It has the ability to concurrently call multiple remote services.
Features
- Fast, Easy, Simple
- Concurrent RPC calls
- Multiple data packager supported (php, json, msgpack built-in)
- Multiple transfer protocols supported (http implemented, tcp/unix will be supported later)
- Detailed debug informations
Install
Install Yar
Yar is an PECL extension, thus you can simply install it by:
pecl install yar
Compile Yar in Linux
$/path/to/phpize
$./configure --with-php-config=/path/to/php-config/
$make && make install
Install Yar with msgpack
first you should install msgpack-ext
pecl install msgpack
or , you can get the github source here: https://github.com/msgpack/msgpack-php
then:
$phpize
$configure --with-php-config=/path/to/php-config/ --enable-msgpack
$make && make install
Runtime Configure
- yar.timeout //default 5000 (ms)
- yar.connect_timeout //default 1000 (ms)
- yar.packager //default "php", when built with --enable-msgpack then default "msgpack", it should be one of "php", "json", "msgpack"
- yar.debug //default Off
- yar.expose_info // default On, whether output the API info for GET requests
- yar.content_type // default "application/octet-stream"
- yar.allow_persistent // default Off
NOTE yar.connect_time is a value in milliseconds, and was measured in seconds in 1.2.1 and before.
Constants
- YAR_VERSION
- YAR_OPT_PACKAGER
- YAR_OPT_PERSISTENT
- YAR_OPT_TIMEOUT
- YAR_OPT_CONNECT_TIMEOUT
Server
It's very easy to setup a Yar HTTP RPC Server
<?php
class API {
/**
* the doc info will be generated automatically into service info page.
* @params
* @return
*/
public function some_method($parameter, $option = "foo") {
} protected function client_can_not_see() {
}
} $service = new Yar_Server(new API());
$service->handle();
?>
Usual RPC calls will be issued as HTTP POST requests. If a HTTP GET request is issued to the uri, the service information (commented section above) will be printed on the page:
Client
It's very easy for a PHP client to call remote RPC:
Synchronous call
<?php
$client = new Yar_Client("http://host/api/");
/* the following setopt is optinal */
$client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, 1000); /* call remote service */
$result = $client->some_method("parameter");
?>
Concurrent call
<?php
function callback($retval, $callinfo) {
var_dump($retval);
} function error_callback($type, $error, $callinfo) {
error_log($error);
} Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback");
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters")); // if the callback is not specificed,
// callback in loop will be used
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", "error_callback", array(YAR_OPT_PACKAGER => "json"));
//this server accept json packager
Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", "error_callback", array(YAR_OPT_TIMEOUT=>1));
//custom timeout Yar_Concurrent_Client::loop("callback", "error_callback"); //send the requests,
//the error_callback is optional
?>
Protocols
Yar Header
Since Yar will support multi transfer protocols, so there is a Header struct, I call it Yar Header
#ifdef PHP_WIN32
#pragma pack(push)
#pragma pack(1)
#endif
typedef struct _yar_header {
unsigned int id; // transaction id
unsigned short version; // protocl version
unsigned int magic_num; // default is: 0x80DFEC60
unsigned int reserved;
unsigned char provider[32]; // reqeust from who
unsigned char token[32]; // request token, used for authentication
unsigned int body_len; // request body len
}
#ifndef PHP_WIN32
__attribute__ ((packed))
#endif
yar_header_t;
#ifdef PHP_WIN32
#pragma pack(pop)
#endif
Packager Header
Since Yar also supports multi packager protocl, so there is a char[8] at the begining of body, to identicate which packager the body is packaged by.
Request
When a Client request a remote server, it will send a struct (in PHP):
<?php
array(
"i" => '', //transaction id
"m" => '', //the method which being called
"p" => array(), //parameters
)
Server
When a server response a result, it will send a struct (in PHP):
<?php
array(
"i" => '',
"s" => '', //status
"r" => '', //return value
"o" => '', //output
"e" => '', //error or exception
)
Yar - Yet Another RPC framework for PHP的更多相关文章
- Yar并行的RPC框架的简单使用
前言: RPC,就是Remote Procedure Call的简称呀,翻译成中文就是远程过程调用 RPC要解决的两个问题: 解决分布式系统中,服务之间的调用问题. 远程调用时,要能够像本地调用一样方 ...
- thinkphp rpc
RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的存在 ...
- [转]各种有用的PHP开源库精心收集
FROM : http://my.oschina.net/caroltc/blog/324024 1.html2ps and html2pdf 下载地址: http://www.tufat.co ...
- GitHub中国区前100名到底是什么样的人?
本文根据Github公开API,抓取了地址显示China的用户,根据粉丝关注做了一个排名,分析前一百名的用户属性,剖析这些活跃在技术社区的牛人到底是何许人也!后续会根据我的一些经验出品<技术人员 ...
- GitHub 中国区前 100 名到底是什么样的人?
本文根据Github公开API,抓取了地址显示China的用户,根据粉丝关注做了一个排名,分析前一百名的用户属性,剖析这些活跃在技术社区的牛人到底是何许人也!后续会根据我的一些经验出品<技术人员 ...
- 各种有用的PHP开源库精心收集
转自:http://my.oschina.net/caroltc/blog/324024 摘要 各种有用的PHP开源库精心收集,包含图片处理,pdf生成,网络协议,网络请求,全文索引,高性能搜索,爬虫 ...
- [转]20位活跃在Github上的国内技术大牛
FROM : http://blog.csdn.net/yaoxtao/article/details/38518933 20位活跃在Github上的国内技术大牛 本文列举了20位在Github上非常 ...
- 【转】GitHub 中国区前 100 名到底是什么样的人?
原文网址:http://mt.sohu.com/20160407/n443539407.shtml 本文根据Github公开API,抓取了地址显示China的用户,根据粉丝关注做了一个排名,分析前一百 ...
- GitHub 中国区前100 名技术专家
[本文是在一片新闻上摘录的,原地址为:http://mt.sohu.com/20160407/n443539407.shtml] 本文根据Github公开API,抓取了地址显示China的用户,根据粉 ...
随机推荐
- linux下mysql数据库的学习
转载博客:http://freedomljtt.blog.163.com/blog/static/72294949201210145441701/ ubuntu12.04 卸载和安装mysql 卸载m ...
- 《Genesis-3D开源游戏引擎-官方录制系列视频教程:进阶实例篇》
注:本系列教程仅针对引擎编辑器:v1.2.2及以下版本 G3D进阶实例 第四课<2D编辑与脚本的统一入口> 使用G3D完成一个简单的类飞机大战游戏,介绍了G3D2d游戏制作的流程包括: ...
- 机器学习框架Scikit Learn的学习
一 安装 安装pip 代码如下:# wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=83 ...
- Android 用MediaCodec实现视频硬解码(转)
本文向你讲述如何用android标准的API (MediaCodec)实现视频的硬件编解码.例程将从摄像头采集视频开始,然后进行H264编码,再解码,然后显示.我将尽量讲得简短而清晰,不展示 那些不相 ...
- 第二百六十六天 how can I 坚持
有些小郁闷,上班没事干,技术.要买房,还要结婚,买了房哪还有彩礼钱,还得借钱,愁人. 该咋办,毕梦琪. 哎.今天还和程秀通说好的去看房呢,整天犹豫不定的.烦. 今天平安夜啊.吃了个苹果,一生平平安安, ...
- HDU 4901 The Romantic Hero (计数DP)
The Romantic Hero 题目链接: http://acm.hust.edu.cn/vjudge/contest/121349#problem/E Description There is ...
- 修改eclipse默认编码方式
设置js文件的默认编码格式为UTF-8 在Windows->Preference页面中,选择General->Content Types ...
- CodeForces 705A Hulk (水题)
题意:输入一个 n,让你输出一行字符串. 析:很水题,只要判定奇偶性,输出就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,10240 ...
- VS清除缓存
今天不小心在项目里面把一个 == 写成了 =,结果数据一下子崩溃了. 后来测试,发现,换一个编译环境,或者换一个编译模式比如debug改成release,就好使了. 1 测试流程 2 测试数据 3 ...
- I/O流的学习
一.I/O流 1.判定是输入还是输出我们应该站在程序的立场: 2.判断传输的是字节还是字符,从而决定管道的大小,字节传递是根本,可以传递所有的数据类型,字符传递专门用来传递文本数据,字节主要用来传递二 ...