modify plugin name

This commit is contained in:
2022-06-16 17:38:23 +08:00
parent 8f9de7a508
commit 6371d5271e
4 changed files with 334 additions and 311 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ namespace PHPSTORM_META {
// Reflect
use Kiri\Di\Container;
use Psr\Container\ContainerInterface;
use Kiri\Di\ContainerInterface;
override(ContainerInterface::get(0), map('@'));
override(Container::get(0), map('@'));
+103
View File
@@ -0,0 +1,103 @@
<?php
namespace Kiri\Rpc;
use Exception;
use Kiri;
use Kiri\Abstracts\Component;
use Kiri\Consul\Agent;
use Psr\Container\ContainerExceptionInterface;
use Kiri\Di\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Swoole\Coroutine;
class Consul extends Component
{
public Agent $agent;
private array $config = [];
/**
* @param ContainerInterface $container
* @param array $settings
* @param array $config
* @throws Exception
*/
public function __construct(public ContainerInterface $container, array $settings, array $config = [])
{
parent::__construct($config);
$this->config = $settings;
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function init(): void
{
$this->agent = $this->container->get(Agent::class);
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function deregister()
{
if (env('environmental') != Kiri::WORKER && env('environmental_workerId') != 0) {
return;
}
$agent = $this->container->get(Agent::class);
$this->logger->debug("disconnect consul.");
$agent->service->deregister($this->config['ID']);
$agent->checks->deregister($this->config['Check']['CheckId']);
}
/**
* @return void
*/
public function service_health(): void
{
$info = $this->agent->service->service_health($this->config['ID']);
if ($info->getStatusCode() == 200) {
return;
}
$this->agent->service->register($this->config);
}
public function watches()
{
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function register(): void
{
$this->deregister();
$data = $this->agent->service->register($this->config);
if ($data->getStatusCode() != 200) {
$this->logger->error($data->getBody());
}
}
}
+29 -63
View File
@@ -26,7 +26,7 @@ use Kiri\Server\Events\OnTaskerStart;
use Kiri\Server\Events\OnWorkerExit;
use Kiri\Server\Events\OnWorkerStart;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Kiri\Di\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ServerRequestInterface;
use ReflectionException;
@@ -42,7 +42,7 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
{
private int $timerId = -1;
private array $consul = [];
/**
@@ -71,18 +71,16 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
/**
* @return void
* @throws ReflectionException
* @throws ReflectionException|ConfigException
*/
public function init(): void
{
$this->eventProvider->on(OnBeforeShutdown::class, [$this, 'onBeforeShutdown']);
scan_directory(APP_PATH . 'rpc', 'app\Rpc');
$this->eventProvider->on(OnWorkerStart::class, [$this, 'consulWatches']);
$this->eventProvider->on(OnWorkerExit::class, [$this, 'onWorkerExit']);
$this->consul = Config::get('rpc.consul', null);
if (!empty($this->consul)) {
$this->eventProvider->on(OnServerBeforeStart::class, [$this, 'register']);
}
$this->collector = $this->dataGrip->get('rpc');
}
@@ -91,52 +89,20 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
* @param OnBeforeShutdown $beforeShutdown
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface|ConfigException
* @throws NotFoundExceptionInterface
*/
public function onBeforeShutdown(OnBeforeShutdown $beforeShutdown): void
{
if (env('environmental_workerId') != 0) {
if (env('environmental') != Kiri::WORKER && env('environmental_workerId') != 0) {
return;
}
$agent = $this->container->get(Agent::class);
$value = Config::get("rpc.consul", []);
if (empty($value)) {
return;
}
$this->logger->debug("disconnect consul.");
$agent->service->deregister($value['ID']);
$agent->checks->deregister($value['Check']['CheckId']);
}
/**
* @param OnWorkerStart|OnTaskerStart $server
* @throws ConfigException
*/
public function consulWatches(OnWorkerStart|OnTaskerStart $server)
{
if ($server->workerId != 0) {
return;
}
$async_time = (int)Config::get('consul.async_time', 1000);
$this->timerId = Timer::tick($async_time, static function () {
Kiri::getDi()->get(RpcManager::class)->tick();
});
}
/**
* @param OnWorkerExit $exit
* @return void
*/
public function onWorkerExit(OnWorkerExit $exit): void
{
if ($this->timerId) {
Timer::clear($this->timerId);
}
$agent->service->deregister($this->consul['ID']);
$agent->checks->deregister($this->consul['Check']['CheckId']);
}
@@ -148,12 +114,11 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
{
$consumers = Config::get("rpc.consumers", []);
if (!empty($consumers)) {
$manager = Kiri::getDi()->get(RpcManager::class);
foreach ($consumers as $service => $consumer) {
$manager->add($service, $consumer);
$this->manager->add($service, $consumer);
}
}
$this->manager->register();
$this->manager->register($this->consul);
}
@@ -175,31 +140,33 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
*/
public function onReceive(Server $server, int $fd, int $reactor_id, string $data): void
{
try {
$data = json_decode($data, true);
if (is_null($data)) {
$this->failure(-32700, 'Parse error语法解析错误');
} else if (!isset($data['jsonrpc']) || !isset($data['method']) || $data['jsonrpc'] != '2.0') {
$this->failure(-32600, 'Invalid Request无效请求');
} else {
$this->batchDispatch($server, $fd, $data);
if (!is_array($data)) {
throw new Exception('Parse error语法解析错误', -32700);
}
if (!isset($data['jsonrpc']) || !isset($data['method']) || $data['jsonrpc'] != '2.0') {
throw new Exception('Invalid Request无效请求', -32600);
}
$server->send($fd, $this->batchDispatch($data));
} catch (\Throwable $throwable) {
$this->logger->error('JsonRpc: ' . $throwable->getMessage());
$server->send($fd, $this->failure(-32700, 'Parse error语法解析错误'));
}
}
/**
* @param Server $server
* @param int $fd
* @param array $data
* @return void
* @return string|bool
*/
private function batchDispatch(Server $server, int $fd, array $data): void
private function batchDispatch(array $data): string|bool
{
if (isset($data['jsonrpc'])) {
$dispatch = $this->dispatch($data);
$result = $this->dispatch($data);
if (!isset($data['id'])) {
$dispatch = [1];
$result = [1];
}
$result = json_encode($dispatch, JSON_UNESCAPED_UNICODE);
} else {
$channel = new Channel($total = count($data));
foreach ($data as $datum) {
@@ -209,9 +176,8 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
for ($i = 0; $i < $total; $i++) {
$result[] = $channel->pop();
}
$result = json_encode($result, JSON_UNESCAPED_UNICODE);
}
$server->send($fd, $result);
return json_encode($result, JSON_UNESCAPED_UNICODE);
}
@@ -252,7 +218,7 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
if (!method_exists($controller, $data['method'])) {
throw new Exception('Method not found', -32601);
}
$params = $this->container->getMethodParameters($controller::class, $data['method']);
$params = $this->container->getArgs($controller::class, $data['method']);
Context::setContext(RequestInterface::class, $this->createServerRequest($params));
+10 -56
View File
@@ -11,51 +11,20 @@ use Kiri\Consul\Agent;
use Kiri\Consul\Health;
use Kiri\Message\Handler\Router;
/**
* class RpcManager
*/
class RpcManager extends Component
{
/**
* @var array
* @var Health
*/
private array $_rpc = [];
#[Inject(Health::class)]
public Health $health;
/**
* @return void
* @throws Kiri\Exception\ConfigException
*/
public function reRegister(): void
{
$service = Kiri::getDi()->get(Agent::class);
$config = Config::get("rpc.consul", null, true);
$info = $service->service->service_health($config['ID']);
if ($info->getStatusCode() == 200) {
return;
}
$service->service->register($config);
}
/**
* @throws Exception
*/
public function tick(): void
{
try {
$this->reRegister();
} catch (\Throwable $throwable) {
$this->logger->error(error_trigger_format($throwable));
}
}
/**
* @param $serviceName
* @return array|null
@@ -89,30 +58,15 @@ class RpcManager extends Component
/**
* @return array
*/
public function doneList(): array
{
$array = [];
foreach ($this->_rpc as $list) {
$array[] = $list;
}
return $array;
}
/**
* @param array $config
* @return void
* @throws Kiri\Exception\ConfigException
*/
public function register(): void
public function register(array $config): void
{
$agent = Kiri::getDi()->get(Agent::class);
$list = Config::get("rpc.consul", null, true);
$agent->service->deregister($list['ID']);
$data = $agent->service->register($list);
$agent->checks->deregister($config['ID']);
$agent->service->deregister($config['ID']);
$data = $agent->service->register($config);
if ($data->getStatusCode() != 200) {
$this->logger->error($data->getBody());
}