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的更多相关文章

  1. Yar并行的RPC框架的简单使用

    前言: RPC,就是Remote Procedure Call的简称呀,翻译成中文就是远程过程调用 RPC要解决的两个问题: 解决分布式系统中,服务之间的调用问题. 远程调用时,要能够像本地调用一样方 ...

  2. thinkphp rpc

    RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议的存在 ...

  3. [转]各种有用的PHP开源库精心收集

    FROM : http://my.oschina.net/caroltc/blog/324024 1.html2ps and html2pdf    下载地址: http://www.tufat.co ...

  4. GitHub中国区前100名到底是什么样的人?

    本文根据Github公开API,抓取了地址显示China的用户,根据粉丝关注做了一个排名,分析前一百名的用户属性,剖析这些活跃在技术社区的牛人到底是何许人也!后续会根据我的一些经验出品<技术人员 ...

  5. GitHub 中国区前 100 名到底是什么样的人?

    本文根据Github公开API,抓取了地址显示China的用户,根据粉丝关注做了一个排名,分析前一百名的用户属性,剖析这些活跃在技术社区的牛人到底是何许人也!后续会根据我的一些经验出品<技术人员 ...

  6. 各种有用的PHP开源库精心收集

    转自:http://my.oschina.net/caroltc/blog/324024 摘要 各种有用的PHP开源库精心收集,包含图片处理,pdf生成,网络协议,网络请求,全文索引,高性能搜索,爬虫 ...

  7. [转]20位活跃在Github上的国内技术大牛

    FROM : http://blog.csdn.net/yaoxtao/article/details/38518933 20位活跃在Github上的国内技术大牛 本文列举了20位在Github上非常 ...

  8. 【转】GitHub 中国区前 100 名到底是什么样的人?

    原文网址:http://mt.sohu.com/20160407/n443539407.shtml 本文根据Github公开API,抓取了地址显示China的用户,根据粉丝关注做了一个排名,分析前一百 ...

  9. GitHub 中国区前100 名技术专家

    [本文是在一片新闻上摘录的,原地址为:http://mt.sohu.com/20160407/n443539407.shtml] 本文根据Github公开API,抓取了地址显示China的用户,根据粉 ...

随机推荐

  1. 2015NOIP简单说说

    在机房度过最后两节课然后滚回去赶文化课,准备期中考试,高考.AFO的称号毫无悬念的归来了.DAY1T2的失误不能拿下230,只能190滚粗,DAY2一上午都在混沌.旁边的哥们求我给看第一题,于是他就对 ...

  2. BestCoder Round #75 解题报告

    King's Cake [思路] 递推 公式:f(n,m)=f(max(m,n-m),min(m,n-m))+1,n>m [代码] #include<cstdio> #include ...

  3. Camera图片特效处理综述(Bitmap的Pixels处理、Canvas/paint的drawBitmap处理、旋转图片、裁截图片、播放幻灯片浏览图片<线程固定时间显示一张>)

    一种是直接对Bitmap的像素进行操作,如:叠加.边框.怀旧.(高斯)模糊.锐化(拉普拉斯变换). Bitmap.getPixels(srcPixels, 0, width, 0, 0, width, ...

  4. C++ 之高效使用STL ( STL 算法分类)

    http://blog.csdn.net/zhoukuo1981/article/details/3452118

  5. vi--文本编辑常用快捷键之复制-粘贴-替换-删除

    这几天刚开始接触vi编辑器,慢慢开始熟悉vi,但是还是感觉诸多不便,比如说复制粘贴删除操作不能用鼠标总是感觉不自在,而且我一般习惯用方向键移动光标,更增加了操作的复杂度,今天在网上搜索了一下,vim编 ...

  6. 内核源码分析之进程地址空间(基于3.16-rc4)

    所谓进程的地址空间,指的就是进程的虚拟地址空间.当创建一个进程时,内核会为该进程分配一个线性的地址空间(虚拟地址空间),有了虚拟地址空间后,内核就可以通过页表将进程的物理地址地址空间映射到其虚拟地址空 ...

  7. Java集合排序(看完秒懂)

    比如将一个List<Student>排序,则有两种方式: 1:Student实现Comparable接口: 2:给排序方法传递一个Comparator参数: 请看下面的举例: Studen ...

  8. 第二百八十四天 how can I 坚持

    又是一个周一.今天感觉过得好艰辛啊,幸好晚上程秀通过生日请客,吃了顿大餐,还拿回了一瓶酒.哈哈. 其他也没什么了.晚上玩的挺好.不过,回来,老是渴,一直想喝水,现在是又困,又累啊,睡觉了.

  9. 【现代程序设计】【homework-08】

    1. 理解C++变量的作用域和生命周期 #include<stdio.h> char * test() { ]="; return s; } main() { puts(test ...

  10. 软件工程个人项目--Word frequency program

    (一)工程设计时间预计 1.代码编写:2小时 (1)文件夹的遍历以及筛选: (2)文件夹的读取,以及对读取字符的操作: (3)所得结果排序,以及文件输出. 2.程序调试:1小时 (1)编写数据. (2 ...