改名
This commit is contained in:
+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'];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user