Files
kiri-rpc/src/JsonRpcConsumers.php
T

142 lines
3.0 KiB
PHP
Raw Normal View History

2021-10-26 18:58:09 +08:00
<?php
namespace Kiri\Rpc;
2021-10-28 10:20:53 +08:00
use Exception;
use Kiri\Context;
2021-10-28 15:40:50 +08:00
use Kiri\Core\Number;
2021-10-26 18:58:09 +08:00
use Kiri\Pool\Pool;
2021-10-28 10:20:53 +08:00
use Swoole\Client;
use Swoole\Coroutine;
2021-10-26 18:58:09 +08:00
/**
*
*/
2021-10-28 10:55:28 +08:00
abstract class JsonRpcConsumers implements OnRpcConsumerInterface
2021-10-26 18:58:09 +08:00
{
/**
* @var Pool
*/
public Pool $pool;
/**
2021-10-27 19:05:37 +08:00
* @param string $service
2021-10-26 18:58:09 +08:00
* @param string $method
* @param mixed $data
* @param string $version
2021-10-28 10:20:53 +08:00
* @throws Exception
2021-10-26 18:58:09 +08:00
*/
2021-10-27 19:05:37 +08:00
public function notify(string $service, string $method, mixed $data, string $version = '2.0'): void
2021-10-26 18:58:09 +08:00
{
2021-10-28 10:20:53 +08:00
$config = $this->get_consul($service);
if (Context::inCoroutine()) {
$client = $this->clientOnCoroutine($config);
} else {
$client = $this->clientNotCoroutine($config);
}
2021-10-28 15:14:21 +08:00
$client->send(json_encode(['jsonrpc' => $version, 'service' => $service, 'method' => $method, 'params' => $data]));
2021-10-28 10:20:53 +08:00
$client->close();
2021-10-26 18:58:09 +08:00
}
/**
2021-10-27 19:05:37 +08:00
* @param string $service
2021-10-26 18:58:09 +08:00
* @param string $method
* @param mixed $data
* @param string $version
* @param string $id
* @return mixed
2021-10-28 10:20:53 +08:00
* @throws Exception
2021-10-26 18:58:09 +08:00
*/
2021-10-27 19:05:37 +08:00
public function get(string $service, string $method, mixed $data, string $version = '2.0', string $id = ''): mixed
2021-10-26 18:58:09 +08:00
{
2021-10-28 10:20:53 +08:00
$config = $this->get_consul($service);
if (Context::inCoroutine()) {
$client = $this->clientOnCoroutine($config);
} else {
$client = $this->clientNotCoroutine($config);
}
2021-10-28 15:40:50 +08:00
if (empty($id)) $id = Number::create(time());
2021-10-28 15:14:21 +08:00
$client->send(json_encode(['jsonrpc' => $version, 'service' => $service, 'method' => $method, 'params' => $data, 'id' => $id]));
2021-10-28 10:20:53 +08:00
$read = $client->recv();
$client->close();
return json_decode($read, true);
}
2021-10-26 18:58:09 +08:00
2021-10-28 10:20:53 +08:00
/**
* @param string $service
* @param array $data
* @return mixed
* @throws Exception
*/
public function batch(string $service, array $data): mixed
{
$config = $this->get_consul($service);
if (Context::inCoroutine()) {
$client = $this->clientOnCoroutine($config);
} else {
$client = $this->clientNotCoroutine($config);
}
$client->send(json_encode($data, true));
$read = $client->recv();
$client->close();
return json_decode($read, true);
2021-10-26 18:58:09 +08:00
}
2021-10-28 10:20:53 +08:00
/**
* @param $service
* @return array
*/
private function get_consul($service): array
2021-10-26 18:58:09 +08:00
{
2021-10-28 15:39:34 +08:00
// $sf = Kiri::getDi()->get(Catalog::class);
//
// $content = $sf->service($service)->getBody()->getContents();
//
// $content = json_decode($content, true);
//
// return $content[array_rand($content)];
return ['ServiceAddress' => '127.0.0.1', 'ServicePort' => 9526];
2021-10-26 18:58:09 +08:00
}
2021-10-28 10:20:53 +08:00
/**
* @param $config
* @return Coroutine\Client
* @throws Exception
*/
private function clientOnCoroutine($config): Coroutine\Client
{
$client = new Coroutine\Client(SWOOLE_SOCK_TCP);
if (!$client->connect($config['ServiceAddress'], $config['ServicePort'], 60)) {
throw new Exception('connect fail.');
}
return $client;
}
/**
* @param $config
* @return Client
* @throws Exception
*/
private function clientNotCoroutine($config): Client
{
$client = new Client(SWOOLE_SOCK_TCP);
if (!$client->connect($config['ServiceAddress'], $config['ServicePort'], 60)) {
throw new Exception('connect fail.');
}
return $client;
}
2021-10-26 18:58:09 +08:00
}