This commit is contained in:
2021-03-24 15:06:02 +08:00
parent a7cde1fb93
commit fea6ede406
3 changed files with 86 additions and 88 deletions
+2 -6
View File
@@ -33,8 +33,8 @@ use Snowflake\Snowflake;
public function __construct( public function __construct(
public string $cmd, public string $cmd,
public int $port, public int $port,
public int $timeout = 1, public int $timeout,
public int $mode = SWOOLE_SOCK_TCP6 public int $mode
) )
{ {
$this->config = ['port' => $port, 'mode' => $mode, 'timeout' => $timeout]; $this->config = ['port' => $port, 'mode' => $mode, 'timeout' => $timeout];
@@ -54,10 +54,6 @@ use Snowflake\Snowflake;
$rpc = Snowflake::app()->getRpc(); $rpc = Snowflake::app()->getRpc();
$rpc->addProducer($this->cmd, $handler, $this->config); $rpc->addProducer($this->cmd, $handler, $this->config);
if ($handler[0] instanceof IProducer) {
$handler[0]->initClient();
}
return parent::execute($handler); return parent::execute($handler);
} }
-2
View File
@@ -6,9 +6,7 @@ namespace Rpc;
use Exception; use Exception;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Swoole;
use Snowflake\Core\Json; use Snowflake\Core\Json;
use Snowflake\Exception\ConfigException;
use Swoole\Coroutine\Client as CClient; use Swoole\Coroutine\Client as CClient;
+84 -80
View File
@@ -16,49 +16,49 @@ 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 = []; private array $cods = [];
/** /**
* @param string $name * @param string $name
* @param array $handler * @param array $handler
* @param array $node * @param array $node
*/ */
public function addProducer(string $name, array $handler, array $node) public function addProducer(string $name, array $handler, array $node)
{ {
$this->classAlias[get_class($handler[0])] = $name; $this->classAlias[get_class($handler[0])] = $name;
$this->consumers[$name] = $handler[0]; $this->consumers[$name] = $handler[0];
$this->producers[$name] = $node; $this->producers[$name] = $node;
} }
/** /**
* @param string $cmd * @param string $cmd
* @param array $handler * @param array $handler
*/ */
public function addConsumer(string $cmd, array $handler) public function addConsumer(string $cmd, array $handler)
{ {
$class = get_class($handler[0]); $class = get_class($handler[0]);
if (!isset($this->classAlias[$class])) { if (!isset($this->classAlias[$class])) {
return; return;
} }
$name = $this->classAlias[$class]; $name = $this->classAlias[$class];
$this->cmds[$name . '.' . $cmd] = $handler; $this->cods[$name . '.' . $cmd] = $handler;
} }
/** /**
@@ -66,70 +66,74 @@ class Producer extends Component
* @param mixed ...$params * @param mixed ...$params
* @return mixed * @return mixed
*/ */
public function dispatch($cmd, mixed ...$params): mixed public function dispatch($cmd, mixed ...$params): mixed
{ {
$handler = $this->cmds[$cmd] ?? null; $handler = $this->cods[$cmd] ?? null;
if (empty($handler)) { if (empty($handler)) {
return false; return false;
} }
return call_user_func($handler, ...$params); return call_user_func($handler, ...$params);
} }
/** /**
* @param $name * @param $name
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function get($name): mixed public function get($name): mixed
{ {
if (!isset($this->consumers[$name])) { if (!isset($this->consumers[$name])) {
throw new Exception('Unknown rpc client.'); throw new Exception('Unknown rpc client.');
} }
return $this->consumers[$name]; return $this->consumers[$name];
} }
/** /**
* @param string $name * @param string $name
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function getClient(string $name): Client public function getClient(string $name): Client
{ {
if (!is_array($producer = $this->producers[$name] ?? null)) { if (!is_array($producer = $this->producers[$name] ?? null)) {
throw new Exception('Unknown rpc client config.'); throw new Exception('Unknown rpc client config.');
} }
$producerName = $this->getName($name, $producer); $producerName = $this->getName($name, $producer);
$snowflake = Snowflake::app(); $snowflake = Snowflake::app();
if (!$snowflake->has($producerName)) { if (!$snowflake->has($producerName)) {
return $snowflake->set($producerName, ['class' => Client::class, 'config' => $producer]); return $snowflake->set($producerName, [
} else { 'class' => Client::class,
return $snowflake->get($producerName); 'service' => $name,
} 'config' => $producer
} ]);
} else {
return $snowflake->get($producerName);
}
}
/** /**
* @param $name * @param $name
* @return Client|bool * @return Client|bool
* @throws Exception * @throws Exception
*/ */
public function __get($name): Client|bool public function __get($name): Client|bool
{ {
return $this->get($name); // TODO: Change the autogenerated stub return $this->get($name); // TODO: Change the autogenerated stub
} }
/** /**
* @param $name * @param $name
* @param $rand * @param $rand
* @return string * @return string
*/ */
private function getName($name, $rand): string private function getName($name, $rand): string
{ {
return 'rpc.client.' . $name . '.' . $rand['host']; return 'rpc.client.' . $name . '.' . $rand['host'];
} }
} }