modify plugin name

This commit is contained in:
2022-03-02 15:15:36 +08:00
parent a3dfcf02e6
commit 219d246d4a
2 changed files with 32 additions and 42 deletions
+19 -10
View File
@@ -12,6 +12,7 @@ use Kiri\Pool\Pool;
use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Kiri\Annotation\Inject;
/** /**
* *
@@ -26,6 +27,20 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface
public Pool $pool; public Pool $pool;
/**
* @var RpcManager
*/
#[Inject(RpcManager::class)]
public RpcManager $manager;
/**
* @var RpcClientInterface
*/
#[Inject(RpcClientInterface::class)]
public RpcClientInterface $client;
protected string $name = ''; protected string $name = '';
@@ -38,9 +53,7 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface
*/ */
public function notify(string $method, mixed $data, string $version = '2.0'): void public function notify(string $method, mixed $data, string $version = '2.0'): void
{ {
$config = $this->get_consul($this->name); $this->client->withConfig($this->get_consul($this->name))->sendRequest(
$transporter = Kiri::getDi()->get(RpcClientInterface::class);
$transporter->withConfig($config)->sendRequest(
$this->requestBody([ $this->requestBody([
'jsonrpc' => $version, 'jsonrpc' => $version,
'service' => $this->name, 'service' => $this->name,
@@ -75,9 +88,7 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface
{ {
if (empty($id)) $id = Number::create(time()); if (empty($id)) $id = Number::create(time());
$config = $this->get_consul($this->name); return $this->client->withConfig($this->get_consul($this->name))->sendRequest(
$transporter = Kiri::getDi()->get(RpcClientInterface::class);
return $transporter->withConfig($config)->sendRequest(
$this->requestBody([ $this->requestBody([
'jsonrpc' => $version, 'jsonrpc' => $version,
'service' => $this->name, 'service' => $this->name,
@@ -97,9 +108,7 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface
*/ */
public function batch(array $data): mixed public function batch(array $data): mixed
{ {
$config = $this->get_consul($this->name); return $this->client->withConfig($this->get_consul($this->name))->sendRequest(
$transporter = Kiri::getDi()->get(RpcClientInterface::class);
return $transporter->withConfig($config)->sendRequest(
$this->requestBody($data) $this->requestBody($data)
); );
} }
@@ -116,7 +125,7 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface
if (empty($service)) { if (empty($service)) {
throw new RpcServiceException('You need set rpc service name if used.'); throw new RpcServiceException('You need set rpc service name if used.');
} }
$sf = Kiri::getDi()->get(RpcManager::class)->getServices($service); $sf = $this->manager->getServices($service);
if (empty($sf) || !is_array($sf)) { if (empty($sf) || !is_array($sf)) {
throw new RpcServiceException('You need set rpc service name if used.'); throw new RpcServiceException('You need set rpc service name if used.');
} }
+13 -32
View File
@@ -5,6 +5,7 @@ namespace Kiri\Rpc;
use Exception; use Exception;
use Kiri; use Kiri;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Annotation\Inject;
use Kiri\Consul\Agent; use Kiri\Consul\Agent;
use Kiri\Consul\Health; use Kiri\Consul\Health;
use Kiri\Message\Handler\Handler; use Kiri\Message\Handler\Handler;
@@ -20,27 +21,8 @@ class RpcManager extends Component
private array $_rpc = []; private array $_rpc = [];
/** #[Inject(Health::class)]
* @param $serviceName public Health $health;
* @return void
* @throws Exception
*/
public function async($serviceName): void
{
$this->reRegister($serviceName);
$lists = Kiri::getDi()->get(Health::class)->setQuery('passing=true')->service($serviceName);
if ($lists->getStatusCode() != 200) {
return;
}
$body = json_decode($lists->getBody(), true);
$file = storage('.rpc.clients.' . md5($serviceName), 'rpc');
if (!empty($body) && is_array($body)) {
file_put_contents($file, json_encode(array_column($body, 'Service')), LOCK_EX);
} else {
file_put_contents($file, json_encode([]), LOCK_EX);
}
}
/** /**
@@ -71,7 +53,7 @@ class RpcManager extends Component
{ {
try { try {
foreach ($this->_rpc as $name => $list) { foreach ($this->_rpc as $name => $list) {
$this->async($name); $this->reRegister($name);
} }
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
$this->logger->error(error_trigger_format($throwable)); $this->logger->error(error_trigger_format($throwable));
@@ -81,20 +63,19 @@ class RpcManager extends Component
/** /**
* @param $serviceName * @param $serviceName
* @return array * @return array|null
* @throws Exception
*/ */
public function getServices($serviceName): array public function getServices($serviceName): ?array
{ {
$file = storage('.rpc.clients.' . md5($serviceName), 'rpc'); $lists = $this->health->setQuery('passing=true')->service($serviceName);
if (!file_exists($file) || filesize($file) < 10) { if ($lists->getStatusCode() != 200) {
$this->async($serviceName); return null;
} }
$content = json_decode(file_get_contents($file), true); $body = json_decode($lists->getBody(), true);
if (empty($content) || !is_array($content)) { if (empty($body)) {
return []; return null;
} }
return $content; return array_column($body, 'Service');
} }