yar简介:

Yet Another RPC framework for PHP>

Light, concurrent RPC framework for PHP(c, java etc will be supported soon)
它是一个轻量级、可并发的用于php的RPC框架,后续会支持C java等主流语言。

yar官方链接:https://github.com/laruence/yar

作者的关于yar的博文:http://www.laruence.com/2012/09/15/2779.html

pecl链接:http://pecl.php.net/package/yar

yar的安装:

sudo apt-get install make
#安装make

sudo apt-get install php5-dev 
#安装php5-dev

sudo apt-get install php-pear
#安装php-pear
以上安装了基本的系统需要的pear和make,便于通过pecl进行yar的安装。
sudo pecl upgrade
#进行pecl的升级

sudo pecl install msgpack
#系统提示错误,提示没有stable

sudo pecl install msgpack-0.5.5
#进行msgpack-0.5.5版本的安装
以上进行pecl的更新和对于msgpack的更新。 msgpack介绍 > It's like JSON.but fast and small. 官方链接: [http://msgpack.org](http://msgpack.org/) 安装完成msgpack后需增加到php.ini中 如下,进行msgpack在php中是否安装成功
php -m | grep msgpack
#查看是否安装了msgpack这个php的模块

php -r "var_dump(extension_loaded('msgpack'));"
#也可直接调用上述extension_loaded进行检测msgpack是否安装
安装yar
sudo pecl install yar

yar服务端代码

<?php

class API {

    /**
      * @params null
      * @return string 'hello'
      * @brief just test api
      */
    public function hello() {
        return 'hello';
    }

    /**
      * @params
      * @return int
      * @brief get server current timestamp
      */
    public function time() {
        return time();
    }
}

$api = new API;

$service = new Yar_Server($api);

$service->handle();

如上,我们创建了一个Yar_Server对象,传入了api实例化对象,然后handle()即可;
访问api地址,显示结果如下:
yar_server

如上,页面显示了可供调用的api方法,并且会将api方法注释显示出来。

yar客户端调用

yar客户端通常有两种调用方法,一种是单线程单一调用,另外一种是多线程并发调用。
单线程单一调用:

#!/usr/bin/env php
<?php

$client = new Yar_Client(&#039;http://kohana/api.php&#039;);

$client-&gt;setOpt(YAR_OPT_CONNECT_TIMEOUT, 1);
$client-&gt;setOpt(YAR_OPT_TIMEOUT, 3);
$client-&gt;setOpt(YAR_OPT_PACKAGER, &#039;php&#039;);

$result = $client-&gt;hello();
var_dump($result);

$time = $client-&gt;time();
var_dump($time);

如上,执行如下几步: ①,创建Yar_Client对象 ②,设定client相关option ③,调用远程方法 执行结果如下: [![Yar_Client](http://www.mmmmm.io/wp-content/uploads/2013/08/Screen-Shot-2013-09-22-at-10.44.15-PM.png)](http://www.mmmmm.io/wp-content/uploads/2013/08/Screen-Shot-2013-09-22-at-10.44.15-PM.png) 并发调用: 客户端脚本如下:

#!/usr/bin/env php
<?php
function callback($retval, $callinfo) {
     var_dump($retval);
}

function error_callback($type, $error, $callinfo) {
    error_log($error);
}

Yar_Concurrent_Client::call(&#039;http://host/api/&#039;, &#039;api&#039;, array(&#039;parameters&#039;), &#039;callback&#039;);
Yar_Concurrent_Client::call(&#039;http://host/api/&#039;, &#039;api&#039;, array(&#039;parameters&#039;));   
// if the callback is not specificed,                                                                                
// callback in loop will be used

Yar_Concurrent_Client::call(&#039;http://host/api/&#039;, &#039;api&#039;, array(&#039;parameters&#039;), &#039;callback&#039;, array(YAR_OPT_PACKAGER =&gt; &#039;json&#039;));                                                    
//this server accept json packager

Yar_Concurrent_Client::call(&#039;http://host/api/&#039;, &#039;api&#039;, array(&#039;parameters&#039;), &#039;callback&#039;, array(YAR_OPT_TIMEOUT=&gt;1));                                                                              
//custom timeout 

Yar_Concurrent_Client::loop(&#039;callback&#039;, &#039;error_callback&#039;); 
//send the requests, 
//the error_callback is optional

执行如下几步:
①、通过Yar_Concurrent_Client::call函数进行并发设定
②、调用Yar_Concurrent_Client::loop进行并发rpc访问,同时绑定callback,error_callback(可选)
③、在callback中进行处理

拓展阅读:

https://github.com/laruence/yar