yar安装使用
来源:互联网 发布:嵌入式linux内核详解 编辑:程序博客网 时间:2024/05/16 10:39
1.安装
pecl install yar
vim /etc/php.ini 加上extension=yar.so
查看支持的配置:
php --re yar
- Dependencies {
Dependency [ json (Required) ]
}
- INI {
Entry [ yar.packager <PERDIR> ] //打包协议
Current = 'php'
}
Entry [ yar.transport <PERDIR> ]
Current = 'curl'
}
Entry [ yar.debug <ALL> ]
Current = 'Off'
}
Entry [ yar.expose_info <PERDIR> ]
Current = 'On'
}
Entry [ yar.connect_timeout <ALL> ]
Current = '1000'
}
Entry [ yar.timeout <ALL> ]
Current = '5000'
}
Entry [ yar.content_type <ALL> ]
Current = 'application/octet-stream'
}
Entry [ yar.allow_persistent <ALL> ]
Current = '0'
}
}
2.代码
服务端,放到网站根目录/var/www/html:
<?php/** yar_server.php */class API {/** * the doc info will be generated automatically into service info page. * @params * @return */public function api2() { echo "api function";}public function test1(){ sleep(1); //模拟实际情况 $arg = func_get_args(); echo "test1 method with arg 0[$arg[0]] \n"; return "ret1:".date("y-m-d H:i:s");}public function test2(){ sleep(2); //模拟实际情况 $arg = func_get_args(); echo "test2 method with arg 0[$arg[0]] \n"; return "ret2:".date("y-m-d H:i:s");}public function test3(){ sleep(3); <span style="font-family: Arial, Helvetica, sans-serif;">//模拟实际情况</span> $arg = func_get_args();echo "test3 method with arg 0[$arg[0]] \n"; return "ret3:".date("y-m-d H:i:s");}public function test4(){ sleep(4); <span style="font-family: Arial, Helvetica, sans-serif;">//模拟实际情况</span> $arg = func_get_args(); echo "test4 method with arg 0[$arg[0]] \n"; return "ret4:".date("y-m-d H:i:s");}protected function client_can_not_see() { echo __FILE__;}protected function client_can_not_see2() { echo __FILE__;}}$service = new Yar_Server(new API());$service->handle();通过浏览器访问,如下:
客户端:
<?php/** *yar_client.php */$url = "http://localhost/yar_server.php";echo str_repeat("-",40),"single request",str_repeat("-",40),"\n";$start = time();$c = new Yar_Client($url);$c->test1("single arg1");$c->test2("single arg2");$c->test3("single arg3");$c->test4("single arg4");echo str_repeat("-",30),"time:",time()-$start,str_repeat("-",30),"\n";echo str_repeat("-",40),"concurrent",str_repeat("-",40),"\n";$start = time(); Yar_Concurrent_Client::call($url,"test1",array("arg1"),"callback"); Yar_Concurrent_Client::call($url,"test2",array("arg1"),"callback"); Yar_Concurrent_Client::call($url,"test3",array("arg1"),"callback"); Yar_Concurrent_Client::call($url,"test4",array("arg1"),"callback"); Yar_concurrent_Client::loop(); function callback($retval,$info){ $args = func_get_args(); $args['time'] = date("y-m-d H:i:s"); var_dump($args); }echo str_repeat("-",30),"time:",time()-$start,str_repeat("-",30),"\n";
php yar_client.php
----------------------------------------single request----------------------------------------
test1 method with arg 0[single arg1]
test2 method with arg 0[single arg2]
test3 method with arg 0[single arg3]
test4 method with arg 0[single arg4]
------------------------------total time:10s------------------------------
----------------------------------------concurrent----------------------------------------
array(3) {
[0]=>
string(22) "ret1:14-06-12 19:35:03" //进入时间
[1]=>
array(3) {
["sequence"]=>
int(1)
["uri"]=>
string(31) "http://localhost/yar_server.php"
["method"]=>
string(5) "test1"
}
["time"]=>
string(17) "14-06-12 19:35:03"
}
array(3) {
[0]=>
string(22) "ret2:14-06-12 19:35:04" //进入时间
[1]=>
array(3) {
["sequence"]=>
int(2)
["uri"]=>
string(31) "http://localhost/yar_server.php"
["method"]=>
string(5) "test2"
}
["time"]=>
string(17) "14-06-12 19:35:04"
}
array(3) {
[0]=>
string(22) "ret3:14-06-12 19:35:05" //进入时间
[1]=>
array(3) {
["sequence"]=>
int(3)
["uri"]=>
string(31) "http://localhost/yar_server.php"
["method"]=>
string(5) "test3"
}
["time"]=>
string(17) "14-06-12 19:35:05"
}
array(3) {
[0]=>
string(22) "ret4:14-06-12 19:35:06" //进入时间
[1]=>
array(3) {
["sequence"]=>
int(4)
["uri"]=>
string(31) "http://localhost/yar_server.php"
["method"]=>
string(5) "test4"
}
["time"]=>
string(17) "14-06-12 19:35:06"
}
------------------------------total time:4s------------------------------
可见,四个函数在串行时,用时10s,在并发时,仅4s.提高了效率。
- yar安装使用
- 安装PHP扩展yar
- 在Yaf中使用Yar
- prc框架安装msgpack,yar扩展
- ubuntu 上给PHP安装扩展 Msgpack 和 Yar
- Ubuntu 下安装 Yar 扩展遇到的问题以及解决方案
- centos6.4 php5.4.13安装扩展yar-rpc笔记
- 编译安装PHP7及yaf,yar,redis等扩展
- 248 php7,ubuntu16.04扩展安装 redis,memcached,yar,rdkafka
- PHP 7下安装Swoole和Yar、Yaf
- 中间件rpc 鸟哥 Yar 的原理和基本使用
- php+yar+apache+windows7
- 通过 yar 初识RPC
- CI整合YAR
- 【Yar】Yar异步调用的超时时间问题以及解决方案
- YAR 并行RPC框架研究
- PHP的RPC框架:Yar
- yar 二次并发请求问题
- neo4j2.0.2使用记录
- 关于android开发中使用系统权限报错的问题
- DockerCon上露脸的开源项目
- usb转串口小板子(2)
- 第1次实验 - NPC问题(回溯算法、聚类分析)
- yar安装使用
- 结构-02. 有理数加法(15)
- 用Fiddler抓取Android、Iphone网络数据包
- 事件委托
- 解決 undefined symbol / reference
- 黑马程序员--编程基础IV--
- 让我们来嚼makefile: include
- 数据库 学生成绩管理系统
- 无法正确输入英文双引号(关闭直引号为弯引号)