• yar框架使用笔记


    Yar是什么

    Yar是并行的RPC框架(Concurrent RPC framework),Laruence开发。

    安装

    下载地址:http://pecl.php.net/package/yar

    windows版本下载对应的扩展放到ext目录并更新php.ini:

    [yar]
    extension=php_yar.dll
    

    Linux版本下载扩展的源码进行编译,将编译出来的so动态库放到extensions目录(例如/usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/)并更新php.ini:

    [yar]
    extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/yar.so
    

    如何使用

    Server端示例:

    <?php
    class API {
        /**
         * the doc info will be generated automatically into service info page.
         * @params
         * @return
         */
        public function api($parameter, $option = "foo") {
        }
     
        protected function client_can_not_see() {
        }
    }
     
    $service = new Yar_Server(new API());
    $service->handle();
    ?>
    
    

    Yar为了方便开发, 把文档和接口绑定到了一起, 对于上面的例子, 如果我们是简单的GET请求这个接口地址的话, 我们就会看到如下的信息页面:

    Yar Server: API
    +API::api($parameter, $option = 'foo')
    

    Client端也很简单,有2种:
    1)串行:

    <?php
    $client = new Yar_Client("http://host/api/");
    $result = $client->api("parameter);
    ?>
    

    2)并行化调用

    <?php
    function callback($retval, $callinfo) {
         var_dump($retval);
    }
     
    Yar_Concurrent_Client::call("http://host/api/", "api", array("parameters"), "callback");
    Yar_Concurrent_Client::call("http://host/api/", "api", array("parameters"), "callback");
    Yar_Concurrent_Client::call("http://host/api/", "api", array("parameters"), "callback");
    Yar_Concurrent_Client::call("http://host/api/", "api", array("parameters"), "callback");
    Yar_Concurrent_Client::loop(); //send
    ?>
    

    这样, 所有的请求会一次发出, 只要有任何一个请求完成, 回调函数”callback”就会被立即调用.

    这里还有一个细节, Yar见缝插针的不会浪费任何时间, 在这些请求发送完成以后, Yar会调用一次callback, 和普通的请求返回回调不同, 这次的调用的$callinfo参数为空.

    示例

    server端:yar.php

    <?php
    
    class API {
        /**
         * the doc info will be generated automatically into service info page.
         * @params
         * @return
         */
        public function test() {
            sleep(1);
            return 't';
            
        }
        
        public function test2() {
            sleep(3);
            return 'test2';
        }
    
    }
     
    $service = new Yar_Server(new API());
    $service->handle();
    
    

    直接在浏览器打开http://localhost/yar.php会显示API文档。

    client端:yar_client.php

    <?php
    
    //串行调用
    //$client = new Yar_Client("http://localhost/yar.php");
    //$client->test();
    //$client->test2();
    
    function callback($retval, $callinfo) {
        //var_dump($retval);
        
        error_log(time().':callinfo:'.json_encode($callinfo).PHP_EOL, 3, 't.log');
        
        if ($callinfo == NULL) {
           //做本地的逻辑
           //return TRUE;
           error_log(time().':'.'send req success'.PHP_EOL, 3, 't.log');
        }else{
            error_log(time().':'.$retval.PHP_EOL, 3, 't.log');
        }
        
        
         
    }
    
    function callback2($retval, $callinfo) {
       
         
    }
    
    function error_callback($type, $error, $callinfo) {
        error_log($error);
    }
    
    //并行调用:
    //1、所有请求发送成功,Yar会调用一次callback,其中$callinfo为null
    //2、每个请求执行完成,获取到了结果,也会去调用callback,其中$callinfo不为null
    $res = Yar_Concurrent_Client::call("http://localhost/yar.php", "test");
    $res1 = Yar_Concurrent_Client::call("http://localhost/yar.php", "test2");
    $res2 = Yar_Concurrent_Client::loop("callback", "error_callback");  //send
    

    t.log:

    1472832899:callinfo:null
    1472832899:send req success
    1472832900:callinfo:{"sequence":1,"uri":"http://localhost/yar.php","method":"test"}
    1472832900:t
    1472832902:callinfo:{"sequence":2,"uri":"http://localhost/yar.php","method":"test2"}
    1472832902:test2
    

    log验证了yar的执行过程。那么,实际应用中,我们就可以先发送请求, 请求发送完毕,然后得到第一次回调($callinfo为null), 继续做我们当前进程的工作; 等所有工作结束以后, 再交给Yar去获取并行RPC的响应:

    <?php
    function callback($retval, $callinfo) {
        if ($callinfo == NULL) {
            //请求发送完毕,会运行到这里
            //做本地的逻辑
           return TRUE;
        }
     
         //RPC请求返回, callback会再次调用。返回值在$retval
    }
    

    实际项目里,Server端里为避免每次实例化当前类,可以写个父类:

    <?php
    
    /**
     *Yar控制器类
     */
    class YarAction{
    
    	/**
    	 * 架构函数
    	 * @access public
    	 */
    	public function __construct() {
    
    		//判断扩展是否存在
    		if(!extension_loaded('yar'))
    			die('yar not support');
    		//实例化Yar_Server
    		$server     =   new Yar_Server($this);
    		// 启动server
    		$server->handle();
    	}
    }
    
    

    资料

    1、文档: http://www.laruence.com/2012/09/15/2779.html
    2、扩展下载: http://pecl.php.net/package/yar/
    3、github: https://github.com/laruence/yar

    参考:
    yar粗略使用记录 - 轩脉刃 - 博客园
    http://www.cnblogs.com/yjf512/p/3448474.html

  • 相关阅读:
    HIDS逐渐的成为主流 java程序员
    怎样做反向域名解析(反向DNS解析)? java程序员
    入侵检测系统的性能的辨别(2) java程序员
    Codeforces Round #146 (Div. 2)
    usaco1.34Prime Cryptarithm
    poj3667 hotel(线段树区间合并)
    poj1330Nearest Common Ancestors(水LCA)
    hdu4135Coprime(容斥原理)
    hdu1541Stars(树状数组)
    usaco 1.43Arithmetic Progressions
  • 原文地址:https://www.cnblogs.com/52fhy/p/5836065.html
Copyright © 2020-2023  润新知