Browse Source

feat: 优化微服务节点数据加载方式;兼容旧rpc调用;新增自定义rpc异常

base-server
lorik 3 years ago
parent
commit
22394ae57f
  1. 39
      src/Client.php
  2. 32
      src/RpcServer.php
  3. 16
      src/exception/RpcException.php

39
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);
}

32
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'])) ) {

16
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;
}
}
Loading…
Cancel
Save