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的用户,根据粉 ...
随机推荐
- 关于ShareSDK接入的各种问题,以及解决方案
随着社交网络的流行,游戏接入分享已经是必然.毕竟这是非常好的一种推广方式.ShareSDK是一个非常好的内分享提供商!但是接入后发生的各种问题,下面给大家提供几个本人遇到的问题,以及解决方法: 1)微 ...
- SeaJS学习笔记(一) ./ 和 ../ 区别
最近要去实习,公司里使用sea.js进行模块化开发 具体下载安装就不多说了,请参见SeaJS官网 <!DOCTYPE html> <html> <head> < ...
- [Hive - LanguageManual ] ]SQL Standard Based Hive Authorization
Status of Hive Authorization before Hive 0.13 SQL Standards Based Hive Authorization (New in Hive 0. ...
- Deep Learning Practice【开篇】
Chapter 0 初入深度学习实战 最近一直在学习深度学习相关的知识,看文献,看博客,看书,与别人讨论,等等,但是总觉得这样的学习只是停留在表面,无法去深入的学习到深度学习的内幕.于是,决定开始深度 ...
- pomelo windows 环境
1.先安装 Python; 通过Python 官网 http://www.python.org/getit/ 下载并安装最新版本. 然后将Python 的安装目录(如: C:\Program File ...
- 查看Linux里某文件的前面/后面几行中的某一行
如,我想看/etc/profile文件的前5行里的第5行. 则, head -5 /etc/profile | tail -1 管道|啊,很简单,就是把左边命令的结果,作为右边的输入. 如, ...
- delphi提示错误行号之Assert(断言)
一.用法:Assert(表达式)1.如果为假 Assert会产生一个EAssertionFailed异常,显示为 Assertion Failed (C:/src/unit1.pas, [size=+ ...
- poj 1469 COURSES(匈牙利算法模板)
http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- thymeleaf中的th:each用法
一.th:eath迭代集合用法: <table> <thead> <tr> <th>序号</th> <th>用户名</th ...
- POJ 3672 Long Distance Racing (模拟)
题意:给定一串字符,u表示是上坡,d表示下坡,f表示平坦的,每个有不同的花费时间,问你从开始走,最远能走到. 析:直接模拟就好了,没什么可说的,就是记下时间时要记双倍的,因为要返回来的. 代码如下: ...