php使用Yar实现RPC调用

Yar轻量级、可串行也可并行、可通过get方法查看接口列表和注释,只需要把接口信息整理好,就能在查看接口的时候同时查看接口文档了。

Yar是啥

轻量级、并行php RPC框架,只支持php程序之间的相互调用。
优点:轻量级、可串行也可并行、可通过get方法查看接口列表和注释,只需要把接口信息整理好,就能在查看接口的时候同时查看接口文档了。
缺点:不支持跨语言调用。

安装

git clone https://github.com/laruence/yar.git
phpize
./configure
make & make install

// 安装成功后,在php.ini中添加extension=yar.so,重启fpm即可

./configure报错
configure: error: Please reinstall the libcurl distribution - easy.h in <curl-dir>/include/curl/
需要安装apt-get install libcurl4-gnutls-dev

yar支持php、json和Msgpack三种打包工具,默认是php,如果要用Msgpack的话要先安装Msgpack扩展,然后配置时加上--enable-msgpack参数。但实际使用中发现,php的serialize和msgpack的效率几乎差不太多,甚至serialize性能还要好一点点,不过serialize所占的空间要稍大一些,个人认为没有必要用msgpack。

php服务端代码

<?php       
// this is a yar server
Class Api{
   
    /**  
     * this is a comments
     * api documents writes here
     */  
    function add($a,$b){
   
       return $a+$b;                                                                                                                                                                        
    }       
            
    function mult($a,$b){
   
       return $a*$b;
    }       
            
    protected function _add(){
   
       return 'this can\'t be seen by client';
    }       
}           
            
$server = new Yar_Server(new Api(
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值