This commit is contained in:
as2252258@163.com
2021-03-24 01:00:43 +08:00
parent bdbd91d8ab
commit 305d250195
+101 -64
View File
@@ -16,84 +16,121 @@ use Snowflake\Snowflake;
class Producer extends Component class Producer extends Component
{ {
private array $producers = []; private array $producers = [];
private array $classAlias = []; private array $classAlias = [];
private array $consumers = []; private array $consumers = [];
/** private array $cmds = [];
* @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 * @param string $name
* @return mixed * @param array $handler
* @throws Exception * @param array $node
*/ */
public function get($name): mixed public function addProducer(string $name, array $handler, array $node)
{ {
if (!isset($this->consumers[$name])) { $this->classAlias[get_class($handler[0])] = $name;
throw new Exception('Unknown rpc client.');
} $this->consumers[$name] = $handler[0];
return $this->consumers[$name];
} $this->producers[$name] = $node;
}
/** /**
* @param string $name * @param string $name
* @return mixed * @param array $handler
* @throws Exception * @param array $node
*/ */
public function getClient(string $name): Client public function addConsumer(string $cmd, array $handler)
{ {
if (!is_array($producer = $this->producers[$name] ?? null)) { $class = get_class($handler[0]);
throw new Exception('Unknown rpc client config.');
}
$producerName = $this->getName($name, $producer);
$snowflake = Snowflake::app(); if (!isset($this->classAlias[$class])) {
if (!$snowflake->has($producerName)) { return;
return $snowflake->set($producerName, ['class' => Client::class, 'config' => $producer]); }
} else {
return $snowflake->get($producerName); $name = $this->classAlias[$class];
}
} $this->cmds[$name . '.' . $cmd] = $handler;
}
/** /**
* @param $name * @param $cmd
* @return Client|bool * @param mixed ...$params
* @throws Exception * @return false|mixed
*/ */
public function __get($name): Client|bool public function dispatch($cmd, mixed ...$params)
{ {
return $this->get($name); // TODO: Change the autogenerated stub $handler = $this->cmds[$cmd] ?? null;
} if (empty($handler)) {
return false;
}
return call_user_func($handler, ...$params);
}
/** /**
* @param $name * @param $name
* @param $rand * @return mixed
* @return string * @throws Exception
*/ */
private function getName($name, $rand): string public function get($name): mixed
{ {
return 'rpc.client.' . $name . '.' . $rand['host']; if (!isset($this->consumers[$name])) {
} throw new Exception('Unknown rpc client.');
}
return $this->consumers[$name];
}
/**
* @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
* @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'];
}
} }