1,'end_city_id'=>100];
* $replay ='GPBMetadata\Workorder\ResultReply 或 直接使用文件夹名 Workorder';
* $clien = new App\Services\GrpcService();
* $clien->execute($method, $request_obj,$replay);
*
* 5,使用例子2,查2次grpc
* $grpc = new GrpcService(config("grpc.service_url.ducms"));
//main_line_repository
$method = '/ducms_main_line_repository.MainLineRepositoryService/GetMainLineRepositoryList';
$query = new Query();
//$query->setSqlQuery("status=10 and repositoryid=" . $repository_id);
$line = new MainLineRepository();
$line->setStatus(10);
$line->setRepositoryid($repository_id);
$query->setEntityQuery($line);
$res = $grpc->execute($method, $query, new ListResult());
$repos = $res->getData();
if(!count($repos)){
return true;
}
$mainLineIds='';
foreach ($repos as $k=>$v){
if ($v instanceof MainLineRepository ) {
$mainLineIds.=$v->getMainLineId().',';
}
}
$mainLineIds = trim($mainLineIds,',');
//检查另一张表数据 main_line
$method = '/ducms_main_line.MainLineService/GetMainLine';
$query = new \GPBMetadata\Dcms_main_line\Query();
$query->setSqlQuery("id in({$mainLineIds})");
$res = $grpc->execute($method, $query, new \GPBMetadata\Ducms_main_line\EntityResult());
$repos = $res->getData();
if ($repos instanceof MainLine ) {
return false;
}else{
return true;
}
*
* @filename GrpcService.php
* @author Zhenxun Du <5552123@qq.com>
* @date 2019/6/22 14:28
* @version $Id$
*/
namespace App\Services;
use App\Entity\Result;
use App\Library\Common;
use Illuminate\Support\Facades\Log;
class GrpcService extends \Grpc\BaseStub
{
public $server_url;
public function __construct($server_url = '')
{
$this->server_url = $server_url ? $server_url : config("grpc.service_url.default");
parent::__construct($this->server_url, ['credentials' => \Grpc\ChannelCredentials::createInsecure()]);
}
/**
* 执行rpc
* @param string $method 服务端提供 /workorder.workOrderService/GetWorkOrderList
* @param object $request_obj 要发送的数据对像
* @param object $response_obj 响应结果对像
* @return Result
*/
public function execute($method, $request_obj, $response_obj)
{
$result = new Result();
try {
//尝试2次
$i = 0;
while ($i < 2) {
$res = $this->_simpleRequest($method, $request_obj, [$response_obj, 'decode']);
list($reply, $status) = $res->wait();
if ($reply instanceof $response_obj) {
break;
} else {
//echo $i.PHP_EOL;
usleep(1000000);
$i++;
}
}
$res_data = $reply->getData();
if (is_string($res_data)) {
$data = json_decode($reply->getData(), true) ?: $reply->getData();
} else {
$data = $res_data;
}
$result->setCode(Result::CODE_SUCCESS)->setData($data);
} catch (\Exception $e) {
//记录日志 ....
$result->setCode(Result::CODE_ERROR)->setMsg('系统繁忙,请重试!');
}
return $result;
}
}