modify
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Rpc;
|
||||
|
||||
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Swoole\Coroutine\Client as CClient;
|
||||
|
||||
|
||||
/**
|
||||
* Class Client
|
||||
* @package Rpc
|
||||
*/
|
||||
class Client extends Component
|
||||
{
|
||||
|
||||
private array $config = [];
|
||||
|
||||
|
||||
private CClient $client;
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*/
|
||||
public function setConfig($name)
|
||||
{
|
||||
$this->config = $name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param array $param
|
||||
* @return mixed|string|null
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function request(string $route, array $param)
|
||||
{
|
||||
$service = $this->config;
|
||||
if (empty($service)) {
|
||||
return null;
|
||||
}
|
||||
if (!($this->client instanceof CClient)) {
|
||||
$client = $this->getClient();
|
||||
}
|
||||
if (!$this->client->isConnected()) {
|
||||
if (!$client->connect($service['host'], $service['port'], $service['timeout'])) {
|
||||
return $client->errCode . ':' . $client->errMsg;
|
||||
}
|
||||
}
|
||||
$isSend = $client->send(serialize(['route' => $route, 'body' => $param]));
|
||||
if ($isSend === false) {
|
||||
return $client->errCode . ':' . $client->errMsg;
|
||||
}
|
||||
return unserialize($client->recv());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getClient(): CClient
|
||||
{
|
||||
return objectPool(CClient::class, function () {
|
||||
$client = new CClient(SWOOLE_SOCK_TCP6);
|
||||
$client->set([
|
||||
'timeout' => 0.5,
|
||||
'connect_timeout' => 1.0,
|
||||
'write_timeout' => 10.0,
|
||||
'read_timeout' => 0.5,
|
||||
'open_tcp_keepalive' => true,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Rpc;
|
||||
|
||||
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
|
||||
/**
|
||||
* Class Producer
|
||||
* @package Rpc
|
||||
*/
|
||||
class Producer extends Component
|
||||
{
|
||||
|
||||
private array $producers = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return Producer|bool
|
||||
* @throws \ReflectionException
|
||||
* @throws \Snowflake\Exception\ComponentException
|
||||
* @throws \Snowflake\Exception\ConfigException
|
||||
* @throws \Snowflake\Exception\NotFindClassException
|
||||
*/
|
||||
public function get($name): Producer|bool
|
||||
{
|
||||
if (empty($this->producers)) {
|
||||
$this->producers = Config::get('rpc.producers');
|
||||
}
|
||||
if (empty($this->producers)) {
|
||||
return $this->addError('Empty by rpc service');
|
||||
}
|
||||
if (!isset($this->producers[$name])) {
|
||||
return $this->addError('Unknown rpc service');
|
||||
}
|
||||
$rand = $this->producers[$name][array_rand($this->producers[$name])];
|
||||
|
||||
if (Snowflake::app()->has($this->getName($name, $rand))) {
|
||||
return Snowflake::app()->get($this->getName($name, $rand));
|
||||
}
|
||||
return Snowflake::app()->set($this->getName($name, $rand), [
|
||||
'class' => Client::class,
|
||||
'config' => $rand
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
* @throws \ReflectionException
|
||||
* @throws \Snowflake\Exception\ComponentException
|
||||
* @throws \Snowflake\Exception\ConfigException
|
||||
* @throws \Snowflake\Exception\NotFindClassException
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
return $this->get($name); // TODO: Change the autogenerated stub
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $rand
|
||||
* @return string
|
||||
*/
|
||||
private function getName($name, $rand)
|
||||
{
|
||||
return 'rpc.client.' . $name . '.' . $rand['host'];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Rpc;
|
||||
|
||||
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
|
||||
/**
|
||||
* Class Service
|
||||
* @package Rpc
|
||||
*/
|
||||
class Service extends Component
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @throws \Snowflake\Exception\ConfigException
|
||||
*/
|
||||
public function instance(): void
|
||||
{
|
||||
$services = Config::get('rpc.service', false, []);
|
||||
if (empty($services)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$server = Snowflake::app()->getSwoole();
|
||||
foreach ($services as $service) {
|
||||
$mode = $service['mode'] ?? SWOOLE_SOCK_TCP6;
|
||||
$rpcServer = $server->addlistener($service['host'], $service['port'], $mode);
|
||||
$rpcServer->set([
|
||||
'open_tcp_keepalive' => true,
|
||||
'tcp_keepidle' => 30,
|
||||
'tcp_keepinterval' => 10,
|
||||
'tcp_keepcount' => 10,
|
||||
'open_http_protocol' => false,
|
||||
'open_websocket_protocol' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -22,6 +22,7 @@ use Snowflake\Event;
|
||||
use Snowflake\Jwt\Jwt;
|
||||
use Snowflake\Pool\Connection;
|
||||
use Snowflake\Pool\Pool as SPool;
|
||||
use Rpc\Producer as RPCProducer;
|
||||
|
||||
/**
|
||||
* Trait TraitApplication
|
||||
@@ -46,6 +47,7 @@ use Snowflake\Pool\Pool as SPool;
|
||||
* @property Curl $curl
|
||||
* @property Crontab $crontab
|
||||
* @property HttpFilter $filter
|
||||
* @property RPCProducer $rpc
|
||||
*/
|
||||
trait TraitApplication
|
||||
{
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
"Database\\": "Database/",
|
||||
"Gii\\": "Gii/",
|
||||
"Kafka\\": "Kafka/",
|
||||
"Rpc\\": "Rpc/",
|
||||
"Annotation\\": "Annotation/"
|
||||
},
|
||||
"files": [
|
||||
|
||||
Reference in New Issue
Block a user