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