改名
This commit is contained in:
+59
-47
@@ -18,66 +18,78 @@ use Swoole\Coroutine\Client as CClient;
|
||||
class Client extends Component
|
||||
{
|
||||
|
||||
private array $config = [];
|
||||
private array $config = [];
|
||||
|
||||
|
||||
private CClient $client;
|
||||
private string $service = '';
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*/
|
||||
public function setConfig($name)
|
||||
{
|
||||
$this->config = $name;
|
||||
}
|
||||
private CClient $client;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param $name
|
||||
*/
|
||||
public function setConfig($name)
|
||||
{
|
||||
$this->config = $name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
public function setService($value)
|
||||
{
|
||||
$this->service = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $cmd
|
||||
* @param array $param
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function request(string $route, array $param): mixed
|
||||
{
|
||||
$service = $this->config;
|
||||
if (empty($service)) {
|
||||
return null;
|
||||
}
|
||||
if (!($this->client instanceof CClient)) {
|
||||
$this->client = $this->getClient();
|
||||
}
|
||||
if (!$this->client->isConnected()) {
|
||||
if (!$this->client->connect($service['host'], $service['port'], $service['timeout'])) {
|
||||
return $this->client->errCode . ':' . $this->client->errMsg;
|
||||
}
|
||||
}
|
||||
$isSend = $this->client->send(serialize(['route' => $route, 'body' => $param]));
|
||||
if ($isSend === false) {
|
||||
return $this->client->errCode . ':' . $this->client->errMsg;
|
||||
}
|
||||
return unserialize($this->client->recv());
|
||||
}
|
||||
public function dispatch(string $cmd, array $param): mixed
|
||||
{
|
||||
$service = $this->config;
|
||||
if (empty($service)) {
|
||||
return null;
|
||||
}
|
||||
if (!($this->client instanceof CClient)) {
|
||||
$this->client = $this->getClient();
|
||||
}
|
||||
if (!$this->client->isConnected()) {
|
||||
if (!$this->client->connect($service['host'], $service['port'], $service['timeout'])) {
|
||||
return $this->client->errCode . ':' . $this->client->errMsg;
|
||||
}
|
||||
}
|
||||
$isSend = $this->client->send(serialize(['cmd' => $cmd, 'body' => $param]));
|
||||
if ($isSend === false) {
|
||||
return $this->client->errCode . ':' . $this->client->errMsg;
|
||||
}
|
||||
return unserialize($this->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,
|
||||
]);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @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,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ interface IProducer
|
||||
{
|
||||
|
||||
|
||||
public function getConfig(): array;
|
||||
public function initClient(): array;
|
||||
|
||||
|
||||
}
|
||||
|
||||
+69
-48
@@ -21,63 +21,84 @@ use Snowflake\Snowflake;
|
||||
class Producer extends Component
|
||||
{
|
||||
|
||||
private array $producers = [];
|
||||
private array $producers = [];
|
||||
|
||||
|
||||
private array $classAlias = [];
|
||||
|
||||
|
||||
private array $consumers = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $handler
|
||||
* @param array $node
|
||||
*/
|
||||
public function addProducer(string $name, array $handler, array $node)
|
||||
{
|
||||
$this->classAlias[get_class($handler[0])] = $name;
|
||||
|
||||
$this->consumers[$name] = $handler[0];
|
||||
|
||||
$this->producers[$name] = $node;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return Producer|bool
|
||||
* @throws ReflectionException
|
||||
* @throws ComponentException
|
||||
* @throws ConfigException
|
||||
* @throws NotFindClassException
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get($name): Client|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
|
||||
]);
|
||||
}
|
||||
public function get($name): mixed
|
||||
{
|
||||
if (!isset($this->consumers[$name])) {
|
||||
throw new Exception('Unknown rpc client.');
|
||||
}
|
||||
return $this->consumers[$name];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return Client|bool
|
||||
* @throws ReflectionException
|
||||
* @throws ComponentException
|
||||
* @throws ConfigException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function __get($name): Client|bool
|
||||
{
|
||||
return $this->get($name); // TODO: Change the autogenerated stub
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getClient(string $name): Client
|
||||
{
|
||||
if (!is_array($producer = $this->producers[$name] ?? null)) {
|
||||
throw new Exception('Unknown rpc client config.');
|
||||
}
|
||||
$producerName = $this->getName($name, $producer);
|
||||
|
||||
$snowflake = Snowflake::app();
|
||||
if (!$snowflake->has($producerName)) {
|
||||
return $snowflake->set($producerName, ['class' => Client::class, 'config' => $producer]);
|
||||
} else {
|
||||
return $snowflake->get($producerName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $rand
|
||||
* @return string
|
||||
*/
|
||||
private function getName($name, $rand): string
|
||||
{
|
||||
return 'rpc.client.' . $name . '.' . $rand['host'];
|
||||
}
|
||||
/**
|
||||
* @param $name
|
||||
* @return Client|bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __get($name): Client|bool
|
||||
{
|
||||
return $this->get($name); // TODO: Change the autogenerated stub
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $rand
|
||||
* @return string
|
||||
*/
|
||||
private function getName($name, $rand): string
|
||||
{
|
||||
return 'rpc.client.' . $name . '.' . $rand['host'];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+29
-4
@@ -68,11 +68,9 @@ class Service extends Component
|
||||
]);
|
||||
$router->addPortListen($service['port'], function () use ($service, $mode) {
|
||||
try {
|
||||
/** @var Request $request */
|
||||
$request = Context::getContext('request');
|
||||
$request->headers->replace('request_method', Request::HTTP_CMD);
|
||||
|
||||
$router = Snowflake::app()->getRouter();
|
||||
if (($node = $router->find_path($request)) === null) {
|
||||
if (($node = router()->find_path($this->replace($request, $service))) === null) {
|
||||
throw new Exception('Cmd not find.');
|
||||
}
|
||||
return $node->dispatch();
|
||||
@@ -86,4 +84,31 @@ class Service extends Component
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param array $service
|
||||
* @return Request
|
||||
* @throws Exception
|
||||
*/
|
||||
public function replace(Request $request, array $service): Request
|
||||
{
|
||||
$body = $request->params->getBodyAndClear();
|
||||
if (is_null($serialize = unserialize($body))) {
|
||||
throw new Exception('Protocol format error.');
|
||||
}
|
||||
if (!isset($serialize['cmd'])) {
|
||||
throw new Exception('Protocol format error.');
|
||||
}
|
||||
$request->params->setPosts($serialize);
|
||||
|
||||
$serialize['cmd'] = ltrim($serialize['cmd'], '/');
|
||||
|
||||
$header = $request->headers;
|
||||
$header->replace('request_uri', 'rpc/' . $service['port'] . '/' . $serialize['cmd']);
|
||||
$header->replace('request_method', Request::HTTP_CMD);
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user