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
)
相关文章
- Java基础笔记 – 增强的for循环For each循环 自动装箱拆箱 可变参数
- XAMPP(v1.83)中的PHP(v5.5.15)访问SQLServer2014
- PHP写文件函数
- 配置php.ini实现PHP文件上传功能
- PHP函数call_user_func和call_user_func_array详解
- 谈谈PHP中require和include的区别
- PHP Socket编程 之 php cc攻击代码与防范方法
- PHP 性能优化 - php.ini 配置
- Http Requests for PHP
- php:PHP解析xml的4种方法
- PHP笔试题
- [获取行数]php读取大文件提供性能的方法,PHP的stream_get_line函数读取大文件获取文件的行数的方...
- php jquery ajax select 二级联动【get方式】
- php判断为空就插入,判断不为空就更新
- PHP邮件发送类
- PHP Extension 扩展开发
- LZW算法PHP实现方法 lzw_decompress php
- php类库PHP QR Code 二维码
- [PHP] 如何让 php-fpm 的循环 echo 实时输出到浏览器
- [php-src] 窥探Php内核中的变量
- [Url]使用C语言来扩展PHP,写PHP扩展dll
- Atitit 通过调用gui接口杀掉360杀毒 360卫士 qq保镖等难以结束的进程(javac# php )
- PHP 循环 - For 循环
- PHP面试题:你所知道的php数组相关的函数?
- PHP面试题:php读取文件内容的几种方法和函数?
- php中json_decode()的使用方法
- PHP Client for Mysql Binlog
- PHP中冒号、endif、endwhile、endfor使用介绍
- PHP 零基础入门笔记(4):PHP数据类型
- Linux php/php-fpm 安装,配置
- PHP如何实现字符串翻转?