Rpc-Client
在服務(wù)端章節(jié)已注冊商品及公共服務(wù)。
控制器調(diào)用
<?php
namespace App\HttpController;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Rpc\Protocol\Response;
class Index extends Controller
{
public function index()
{
// 如果在同server中 直接用保存的rpc實(shí)例調(diào)用即可
// 如果不是需要重新new一個rpc 注意config的配置 節(jié)點(diǎn)管理器 以及所在ip是否能被其他服務(wù)廣播到 如果不能請調(diào)整其他服務(wù)的廣播地址
$config = new \EasySwoole\Rpc\Config();
$rpc = new \EasySwoole\Rpc\Rpc($config);
$ret = [];
$client = $rpc->client();
// client 全局參數(shù)
$client->setClientArg([1,2,3]);
/**
* 調(diào)用商品列表
*/
$ctx1 = $client->addRequest('Goods.GoodsModule.list');
// 設(shè)置請求參數(shù)
$ctx1->setArg(['a','b','c']);
// 設(shè)置調(diào)用成功執(zhí)行回調(diào)
$ctx1->setOnSuccess(function (Response $response) use (&$ret) {
$ret[] = [
'list' => [
'msg' => $response->getMsg(),
'result' => $response->getResult()
]
];
});
/**
* 調(diào)用信箱公共
*/
$ctx2 = $client->addRequest('Common.CommonModule.mailBox');
// 設(shè)置調(diào)用成功執(zhí)行回調(diào)
$ctx2->setOnSuccess(function (Response $response) use (&$ret) {
$ret[] = [
'mailBox' => [
'msg' => $response->getMsg(),
'result' => $response->getResult()
]
];
});
/**
* 獲取系統(tǒng)時間
*/
$ctx2 = $client->addRequest('Common.CommonModule.serverTime');
// 設(shè)置調(diào)用成功執(zhí)行回調(diào)
$ctx2->setOnSuccess(function (Response $response) use (&$ret) {
$ret[] = [
'serverTime' => [
'msg' => $response->getMsg(),
'result' => $response->getResult()
]
];
});
// 執(zhí)行調(diào)用
$client->exec();
$this->writeJson(200, $ret);
}
}