This commit is contained in:
2022-09-23 18:55:46 +08:00
parent c190c749f4
commit c01dc5f41a
14 changed files with 239 additions and 331 deletions
+43 -34
View File
@@ -5,19 +5,17 @@ namespace Kiri\Rpc;
use Exception;
use JetBrains\PhpStorm\ArrayShape;
use Kiri;
use Kiri\Di\LocalService;
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
use Kiri\Annotation\Annotation;
use Kiri\Consul\Agent;
use Kiri\Context;
use Kiri\Core\Json;
use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException;
use Kiri\Message\Constrict\RequestInterface;
use Kiri\Message\Handler\DataGrip;
use Kiri\Message\Handler\Router;
use Kiri\Message\Handler\RouterCollector;
use Kiri\Message\ServerRequest;
use Kiri\Server\Contract\OnCloseInterface;
use Kiri\Server\Contract\OnConnectInterface;
use Kiri\Server\Contract\OnReceiveInterface;
@@ -26,7 +24,6 @@ use Kiri\Server\Events\OnServerBeforeStart;
use Psr\Container\ContainerExceptionInterface;
use Kiri\Di\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ServerRequestInterface;
use ReflectionException;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
@@ -41,6 +38,8 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
private array $consul = [];
public RouterCollector $collector;
/**
* @param ContainerInterface $container
@@ -48,7 +47,6 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
* @param Annotation $annotation
* @param DataGrip $dataGrip
* @param RpcManager $manager
* @param RouterCollector $collector
* @param EventProvider $eventProvider
* @param array $config
* @throws Exception
@@ -58,7 +56,6 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
public Annotation $annotation,
public DataGrip $dataGrip,
public RpcManager $manager,
public RouterCollector $collector,
public EventProvider $eventProvider,
array $config = [])
{
@@ -68,17 +65,39 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
/**
* @return void
* @throws ReflectionException|ConfigException
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function init(): void
{
$this->eventProvider->on(OnBeforeShutdown::class, [$this, 'onBeforeShutdown']);
scan_directory(APP_PATH . 'rpc', 'app\Rpc');
$this->consul = Config::get('rpc.consul', null);
if (!empty($this->consul)) {
$this->eventProvider->on(OnServerBeforeStart::class, [$this, 'register']);
}
// $this->consul = Config::get('rpc.consul', null);
// if (!empty($this->consul)) {
// $this->eventProvider->on(OnServerBeforeStart::class, [$this, 'register']);
// }
$this->collector = $this->dataGrip->get('rpc');
$this->registerConsumers();
}
/**
* @return void
* @throws ConfigException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function registerConsumers(): void
{
$consumers = Config::get('rpc.consumers', []);
if (!is_array($consumers)) {
return;
}
$local = $this->container->get(LocalService::class);
foreach ($consumers as $consumer) {
$local->set($consumer['id'], $consumer);
}
}
@@ -134,10 +153,12 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
* @param int $fd
* @param int $reactor_id
* @param string $data
* @return bool
*/
public function onReceive(Server $server, int $fd, int $reactor_id, string $data): void
public function onReceive(Server $server, int $fd, int $reactor_id, string $data): bool
{
try {
if (!isJson($data)) return $server->send($fd, 'success', $reactor_id);
$data = json_decode($data, true);
if (!is_array($data)) {
throw new Exception('Parse error语法解析错误', -32700);
@@ -145,11 +166,11 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
if (!isset($data['jsonrpc']) || !isset($data['method']) || $data['jsonrpc'] != '2.0') {
throw new Exception('Invalid Request无效请求', -32600);
}
$server->send($fd, $this->batchDispatch($data));
return $server->send($fd, $this->batchDispatch($data), $reactor_id);
} catch (\Throwable $throwable) {
$this->logger->error('JsonRpc: ' . $throwable->getMessage());
$response = Json::encode($this->failure(-32700, $throwable->getMessage()));
$server->send($fd, $response);
return $server->send($fd, $response, $reactor_id);
}
}
@@ -208,19 +229,18 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
private function dispatch($data): array
{
try {
$handler = $this->collector->find($data['service'], 'GET');
if (is_integer($handler) || is_null($handler)) {
$class = $this->container->get(LocalService::class)->get($data['service']);
if (!$this->container->has($class)) {
throw new Exception('Handler not found', -32601);
}
$controller = $this->container->get($handler->callback[0]);
$controller = $this->container->get($class);
if (!method_exists($controller, $data['method'])) {
throw new Exception('Method not found', -32601);
}
$params = $this->container->getArgs($data['method'], $controller::class);
Context::setContext(RequestInterface::class, $this->createServerRequest($params));
return $this->handler($controller, $data['method'], $params);
if (!isset($data['params']) || !is_array($data['params'])) {
$data['params'] = [];
}
return $this->handler($controller, $data['method'], $data['params']);
} catch (\Throwable $throwable) {
$code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode();
return $this->failure($code, jTraceEx($throwable), [], $data['id'] ?? null);
@@ -228,17 +248,6 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
}
/**
* @param $params
* @return ServerRequestInterface
* @throws Exception
*/
private function createServerRequest($params): ServerRequestInterface
{
return (new ServerRequest())->withParsedBody($params);
}
/**
* @param $controller
* @param string $method