diff --git a/src/Client.php b/src/Client.php index a1c09bc..c9a8529 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,10 +2,15 @@ namespace blobt\airpc; use blobt\airpc\exception\ErrorRpcException; -use Yii; class Client { + /** + * 微服务节点配置 + * @var array + */ + public $servicesList; + /** * 远程调用 * @param $service @@ -14,16 +19,19 @@ class Client * @param $params * @return array */ - public static function remoteCall($service, $controller, $method, $params) : array + public function remoteCall($service, $controller, $method, $params) : array { try{ //找到service对应服务器 - $serviceData = self::serviceData($service); + $serviceData = $this->serviceData($service); //打包数据 $data = [ 'controller' => $controller, 'method' => $method, - 'params' => $params + 'params' => $params, + 'userToken'=> $params['jwt'] ?? null, //兼容旧rpc + 'endPoint' => 'services', //兼容旧rpc + 'rpcVersion' => 'v1' ]; $data = json_encode($data); $length = strlen($data); @@ -54,6 +62,18 @@ class Client if( $decodeData == false ) throw new ErrorRpcException('remote call result json decode failed', 88500005); + //兼容旧rpc + if( isset($decodeData['code']) ){ + $decodeData['statusCode'] = $decodeData['code']; + unset($decodeData['code']); + if( $decodeData['statusCode'] != 200 ){ + if( isset($decodeData['data']['code']) ) + $decodeData['errorCode'] = $decodeData['data']['code']; + if( isset($decodeData['data']['info']) ) + $decodeData['msg'] = $decodeData['data']['info']; + unset($decodeData['data']); + } + } return $decodeData; }catch(\Throwable $e){ return ['statusCode'=>500,'errorCode'=>$e->getCode(),'msg'=>$e->getMessage()]; @@ -66,13 +86,12 @@ class Client * @return array * @throws ErrorRpcException */ - private static function serviceData($service): array + private function serviceData($service): array { - if( isset(Yii::$app->params['services']) ){ - $servicesList = Yii::$app->params['services']; - if( isset($servicesList[$service]) && !empty($servicesList[$service]) ){ - $randomKey = array_rand($servicesList[$service],1); //随机取一个节点 - return $servicesList[$service][$randomKey]; + if( !empty($this->servicesList) ){ + if( isset($this->servicesList[$service]) && !empty($this->servicesList[$service]) ){ + $randomKey = array_rand($this->servicesList[$service],1); //随机取一个节点 + return $this->servicesList[$service][$randomKey]; }else{ throw new ErrorRpcException('services information not found', 88500006); } diff --git a/src/RpcServer.php b/src/RpcServer.php index b60d38a..ccef257 100644 --- a/src/RpcServer.php +++ b/src/RpcServer.php @@ -33,16 +33,29 @@ class RpcServer extends BaseService { */ public function onReceive(Swoole\Server $server, int $fd, int $reactorId, string $pack) { $pack = new Pack($pack); + $data = json_decode($pack->data,true); //调用rpc对应的处理函数 try { - $ret = $this->handle($pack->data); - $ret = json_encode(['statusCode'=>200,'data'=>$ret]); + $ret = $this->handle($data); + if( isset($data['rpcVersion']) ) + $ret = json_encode(['statusCode'=>200,'data'=>$ret]); + else //兼容旧版本rpc + $ret = json_encode(['code'=>200,'data'=>$ret]); }catch (\Throwable $e){ - if( $e instanceof RpcException ){ - $ret = json_encode(['statusCode'=>$e->statusCode,'errorCode'=>$e->getCode(),'msg'=>$e->getMessage()]); - }else{ - $ret = json_encode(['statusCode'=>500,'errorCode'=>$e->getCode(),'msg'=>$e->getMessage()]); - Yii::error($e); + if( isset($data['rpcVersion']) ){ + if( $e instanceof RpcException ){ + $ret = json_encode(['statusCode'=>$e->statusCode,'errorCode'=>$e->getCode(),'msg'=>$e->getMessage()]); + }else{ + $ret = json_encode(['statusCode'=>500,'errorCode'=>$e->getCode(),'msg'=>$e->getMessage()]); + Yii::error($e); + } + }else{ //兼容旧版本rpc + if( $e instanceof RpcException ){ + $ret = json_encode(['code'=>$e->statusCode, 'data'=>['code'=>$e->getCode(),'info'=>$e->getMessage()]]); + }else{ + $ret = json_encode(['code'=>500, 'data'=>['code'=>$e->getCode(),'info'=>$e->getMessage()]]); + Yii::error($e); + } } } //返回结果 @@ -52,13 +65,12 @@ class RpcServer extends BaseService { /** * 请求处理函数 - * @param string $data + * @param array $data * @return mixed * @throws BadRequestRpcException * @throws NotFoundRpcException */ - private function handle(string $data) { - $data = json_decode($data,true); + private function handle(array $data) { if( is_array($data) && isset($data['controller']) && isset($data['method']) ){ $className = 'rpc\\controllers\\'.ucfirst($data['controller']).'Controller'; if( class_exists($className) && method_exists($className,'action'.ucfirst($data['method'])) ) { diff --git a/src/exception/RpcException.php b/src/exception/RpcException.php index a8e63ee..64610be 100644 --- a/src/exception/RpcException.php +++ b/src/exception/RpcException.php @@ -7,4 +7,20 @@ namespace blobt\airpc\exception; class RpcException extends \Exception { public $statusCode = 500; + + /** + * 创建一个自定义状态码异常 + * @param int $statusCode + * @param string $msg + * @param int $code + * @return RpcException + */ + public static function getInstance(int $statusCode,string $msg='', int $code=0): RpcException + { + $exception = new self; + $exception->statusCode = $statusCode; + $exception->message = $msg; + $exception->code = $code; + return $exception; + } } \ No newline at end of file