客户端php代码 test.php
php 需要安装 yar 扩展
<?php
/*
//单个rpc调用
$client = new Yar_Client("http://localhost/test");
$client->SetOpt(YAR_OPT_PACKAGER, "json");
$result = $client->api("quqi...@gmail.com");
var_dump($result);
*/
//并行化rpc
function callback($retval, $callinfo) {
var_dump($retval);
}
function error_callback($type, $error, $callinfo) {
error_log($error);
}
Yar_Concurrent_Client::call("http://localhost/test", "api", array("qqf"), "callback", "error_callback", array(YAR_OPT_PACKAGER => "json",YAR_OPT_TIMEOUT=>1));
Yar_Concurrent_Client::call("http://localhost/test", "api", array("qqf"), "callback", "error_callback", array(YAR_OPT_PACKAGER => "json",YAR_OPT_TIMEOUT=>1));
Yar_Concurrent_Client::call("http://localhost/test", "api", array("qqf"), "callback", "error_callback", array(YAR_OPT_PACKAGER => "json",YAR_OPT_TIMEOUT=>1));
Yar_Concurrent_Client::loop("callback", "error_callback");
openresty 服务端代码
nginx.conf
location /test
{
default_type text/plain;
content_by_lua '
ngx.req.read_body()
local body = ngx.req.get_body_data()
local header = string.sub(body,1,82)
local package = string.sub(body,83,90)
local _body = string.sub(body,91)
local cjson = require "cjson"
local result = cjson.decode(_body)
local id = result.i
local respone = {i=id,s="",r="qqf332",o="output",e="error"}
local result = cjson.encode(respone)
local sendout = table.concat({header,package,result})
ngx.say(sendout)
return
';
}
访问 http://localhost/test.php 输出
NULL string(6) "qqf332" string(6) "qqf332" string(6) "qqf332"