2021-03-23 15:13:33 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Annotation\Rpc;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Annotation\Attribute;
|
2021-03-23 17:00:17 +08:00
|
|
|
use Exception;
|
2021-03-23 15:13:33 +08:00
|
|
|
use Snowflake\Snowflake;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class RpcClient
|
|
|
|
|
* @package Annotation\Rpc
|
|
|
|
|
*/
|
2021-03-24 11:26:46 +08:00
|
|
|
#[\Attribute(\Attribute::TARGET_CLASS)] class RpcClient extends Attribute
|
2021-03-23 15:13:33 +08:00
|
|
|
{
|
|
|
|
|
|
2021-05-03 03:48:52 +08:00
|
|
|
private array $config;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RpcClient constructor.
|
|
|
|
|
* @param string $cmd
|
|
|
|
|
* @param int $port
|
|
|
|
|
* @param int $timeout
|
|
|
|
|
* @param int $mode
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
public string $cmd,
|
|
|
|
|
public int $port,
|
|
|
|
|
public int $timeout,
|
|
|
|
|
public int $mode
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
$this->config = ['port' => $port, 'mode' => $mode, 'timeout' => $timeout];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $handler
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function execute(mixed $class, mixed $method = ''): mixed
|
|
|
|
|
{
|
|
|
|
|
$rpc = Snowflake::app()->getRpc();
|
|
|
|
|
$rpc->addProducer($this->cmd, [$class, $method], $this->config);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-03-23 15:13:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|