改名
This commit is contained in:
@@ -5,6 +5,7 @@ namespace Annotation;
|
||||
|
||||
|
||||
use DirectoryIterator;
|
||||
use Exception;
|
||||
use Snowflake\Abstracts\Component;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
|
||||
@@ -51,7 +52,7 @@ class Annotation extends Component
|
||||
* @param string $namespace
|
||||
* @param string $alias
|
||||
* @return void
|
||||
* @throws ComponentException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function read(string $path, string $namespace, string $alias = 'root'): void
|
||||
{
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace Annotation;
|
||||
|
||||
use Attribute;
|
||||
use DirectoryIterator;
|
||||
use Exception;
|
||||
use ReflectionClass;
|
||||
use ReflectionMethod;
|
||||
use ReflectionProperty;
|
||||
@@ -121,7 +122,7 @@ class Loader extends BaseObject
|
||||
/**
|
||||
* @param DirectoryIterator $paths
|
||||
* @param $namespace
|
||||
* @throws ComponentException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function _scanDir(DirectoryIterator $paths, $namespace)
|
||||
{
|
||||
@@ -216,6 +217,11 @@ class Loader extends BaseObject
|
||||
}
|
||||
|
||||
$annotations = $this->_classes[$className];
|
||||
if (isset($_array['target']) && !empty($_array['target'])) {
|
||||
foreach ($_array['target'] as $value) {
|
||||
$value->execute([$annotations['handler']]);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($annotations['methods'] as $name => $attribute) {
|
||||
foreach ($attribute as $value) {
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace Annotation\Rpc;
|
||||
|
||||
use Annotation\Attribute;
|
||||
use ReflectionException;
|
||||
use Rpc\IProducer;
|
||||
use Snowflake\Exception\ComponentException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
@@ -18,6 +19,8 @@ use Snowflake\Snowflake;
|
||||
#[\Attribute(\Attribute::TARGET_CLASS)] class RpcService extends Attribute
|
||||
{
|
||||
|
||||
private array $config;
|
||||
|
||||
|
||||
/**
|
||||
* RpcClient constructor.
|
||||
@@ -33,6 +36,7 @@ use Snowflake\Snowflake;
|
||||
public int $mode = SWOOLE_SOCK_TCP6
|
||||
)
|
||||
{
|
||||
$this->config = ['host' => $this->host, 'port' => $this->port, 'mode' => $this->mode];
|
||||
}
|
||||
|
||||
|
||||
@@ -45,13 +49,15 @@ use Snowflake\Snowflake;
|
||||
*/
|
||||
public function execute(array $handler): mixed
|
||||
{
|
||||
// TODO: Change the autogenerated stub
|
||||
if ($handler[0] instanceof IProducer) {
|
||||
call_user_func([$handler[0], 'instance']);
|
||||
}
|
||||
|
||||
$rpc = Snowflake::app()->getRpc();
|
||||
$rpc->addProducer($this->cmd, $handler, [
|
||||
'host' => $this->host,
|
||||
'port' => $this->port,
|
||||
'mode' => $this->mode
|
||||
]);
|
||||
return parent::execute($handler); // TODO: Change the autogenerated stub
|
||||
$rpc->addProducer($this->cmd, $handler, $this->config);
|
||||
|
||||
return parent::execute($handler);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -77,6 +77,33 @@ class HttpParams
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除参数
|
||||
*/
|
||||
public function clearBody()
|
||||
{
|
||||
$this->body = [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除参数
|
||||
*/
|
||||
public function clearGet()
|
||||
{
|
||||
$this->gets = [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 清空文件上传信息
|
||||
*/
|
||||
public function clearFile()
|
||||
{
|
||||
$this->files = [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
@@ -86,6 +113,17 @@ class HttpParams
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getBodyAndClear(): mixed
|
||||
{
|
||||
$data = $this->body;
|
||||
$this->clearBody();
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param string $value
|
||||
@@ -222,10 +260,10 @@ class HttpParams
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param string $name
|
||||
* @param bool $isNeed
|
||||
* @param null $min
|
||||
* @param null $max
|
||||
* @param array|int|null $min
|
||||
* @param int|null $max
|
||||
* @return int|null
|
||||
* @throws RequestException
|
||||
*/
|
||||
@@ -265,12 +303,12 @@ class HttpParams
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param string $name
|
||||
* @param bool $isNeed
|
||||
* @param null $length
|
||||
* @param int|array|null $length
|
||||
*
|
||||
* @return string
|
||||
* @throws
|
||||
* @return string|null
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function string(string $name, bool $isNeed = FALSE, int|array|null $length = NULL): ?string
|
||||
{
|
||||
@@ -285,9 +323,10 @@ class HttpParams
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $_length
|
||||
* @param $string
|
||||
* @param $min
|
||||
* @param $max
|
||||
* @return mixed
|
||||
* @throws RequestException
|
||||
*/
|
||||
private function between($string, $min, $max): mixed
|
||||
@@ -323,10 +362,10 @@ class HttpParams
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param string $name
|
||||
* @param bool $isNeed
|
||||
*
|
||||
* @return bool|string
|
||||
* @return bool
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function bool(string $name, bool $isNeed = FALSE): bool
|
||||
@@ -339,8 +378,8 @@ class HttpParams
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $default
|
||||
* @param string $name
|
||||
* @param int|null $default
|
||||
*
|
||||
* @return int|string|null
|
||||
* @throws RequestException
|
||||
@@ -364,10 +403,10 @@ class HttpParams
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $default
|
||||
* @param string $name
|
||||
* @param string|null $default
|
||||
*
|
||||
* @return mixed
|
||||
* @return string|null
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function datetime(string $name, string $default = NULL): string|null
|
||||
@@ -386,9 +425,9 @@ class HttpParams
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param null $default
|
||||
* @return mixed
|
||||
* @param string $name
|
||||
* @param string|null $default
|
||||
* @return string|null
|
||||
* @throws RequestException
|
||||
*/
|
||||
public function ip(string $name, string $default = NULL): string|null
|
||||
|
||||
@@ -476,6 +476,8 @@ class Server extends HttpService
|
||||
|
||||
$annotation = Snowflake::app()->getAttributes();
|
||||
$annotation->instanceDirectoryFiles(CONTROLLER_PATH);
|
||||
$annotation->instanceDirectoryFiles(RPC_SERVICE_PATH);
|
||||
$annotation->instanceDirectoryFiles(RPC_CLIENT_PATH);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+15
-3
@@ -21,6 +21,9 @@ class Client extends Component
|
||||
private array $config = [];
|
||||
|
||||
|
||||
private string $service = '';
|
||||
|
||||
|
||||
private CClient $client;
|
||||
|
||||
|
||||
@@ -34,12 +37,21 @@ class Client extends Component
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param $value
|
||||
*/
|
||||
public function setService($value)
|
||||
{
|
||||
$this->service = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $cmd
|
||||
* @param array $param
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function request(string $route, array $param): mixed
|
||||
public function dispatch(string $cmd, array $param): mixed
|
||||
{
|
||||
$service = $this->config;
|
||||
if (empty($service)) {
|
||||
@@ -53,7 +65,7 @@ class Client extends Component
|
||||
return $this->client->errCode . ':' . $this->client->errMsg;
|
||||
}
|
||||
}
|
||||
$isSend = $this->client->send(serialize(['route' => $route, 'body' => $param]));
|
||||
$isSend = $this->client->send(serialize(['cmd' => $cmd, 'body' => $param]));
|
||||
if ($isSend === false) {
|
||||
return $this->client->errCode . ':' . $this->client->errMsg;
|
||||
}
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ interface IProducer
|
||||
{
|
||||
|
||||
|
||||
public function getConfig(): array;
|
||||
public function initClient(): array;
|
||||
|
||||
|
||||
}
|
||||
|
||||
+45
-24
@@ -24,45 +24,66 @@ class Producer extends Component
|
||||
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
|
||||
public function get($name): mixed
|
||||
{
|
||||
if (empty($this->producers)) {
|
||||
$this->producers = Config::get('rpc.producers');
|
||||
if (!isset($this->consumers[$name])) {
|
||||
throw new Exception('Unknown rpc client.');
|
||||
}
|
||||
if (empty($this->producers)) {
|
||||
return $this->addError('Empty by rpc service');
|
||||
return $this->consumers[$name];
|
||||
}
|
||||
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));
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
return Snowflake::app()->set($this->getName($name, $rand), [
|
||||
'class' => Client::class,
|
||||
'config' => $rand
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return Client|bool
|
||||
* @throws ReflectionException
|
||||
* @throws ComponentException
|
||||
* @throws ConfigException
|
||||
* @throws NotFindClassException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __get($name): Client|bool
|
||||
{
|
||||
|
||||
+29
-4
@@ -68,11 +68,9 @@ class Service extends Component
|
||||
]);
|
||||
$router->addPortListen($service['port'], function () use ($service, $mode) {
|
||||
try {
|
||||
/** @var Request $request */
|
||||
$request = Context::getContext('request');
|
||||
$request->headers->replace('request_method', Request::HTTP_CMD);
|
||||
|
||||
$router = Snowflake::app()->getRouter();
|
||||
if (($node = $router->find_path($request)) === null) {
|
||||
if (($node = router()->find_path($this->replace($request, $service))) === null) {
|
||||
throw new Exception('Cmd not find.');
|
||||
}
|
||||
return $node->dispatch();
|
||||
@@ -86,4 +84,31 @@ class Service extends Component
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param array $service
|
||||
* @return Request
|
||||
* @throws Exception
|
||||
*/
|
||||
public function replace(Request $request, array $service): Request
|
||||
{
|
||||
$body = $request->params->getBodyAndClear();
|
||||
if (is_null($serialize = unserialize($body))) {
|
||||
throw new Exception('Protocol format error.');
|
||||
}
|
||||
if (!isset($serialize['cmd'])) {
|
||||
throw new Exception('Protocol format error.');
|
||||
}
|
||||
$request->params->setPosts($serialize);
|
||||
|
||||
$serialize['cmd'] = ltrim($serialize['cmd'], '/');
|
||||
|
||||
$header = $request->headers;
|
||||
$header->replace('request_uri', 'rpc/' . $service['port'] . '/' . $serialize['cmd']);
|
||||
$header->replace('request_method', Request::HTTP_CMD);
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -423,6 +423,18 @@ abstract class BaseApplication extends Service
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return \Rpc\Producer
|
||||
* @throws ComponentException
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function getRpc(): \Rpc\Producer
|
||||
{
|
||||
return $this->get('rpc');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
@@ -24,6 +24,8 @@ defined('DB_ERROR_BUSY') or define('DB_ERROR', 'The database is busy. Please try
|
||||
defined('SELECT_IS_NULL') or define('SELECT_IS_NULL', 'Query data does not exist, please check the relevant conditions.');
|
||||
defined('PARAMS_IS_NULL') or define('PARAMS_IS_NULL', 'Required items cannot be empty, please add.');
|
||||
defined('CONTROLLER_PATH') or define('CONTROLLER_PATH', APP_PATH . 'app/Http/Controllers/');
|
||||
defined('RPC_SERVICE_PATH') or define('RPC_SERVICE_PATH', APP_PATH . 'app/Http/Rpc/');
|
||||
defined('RPC_CLIENT_PATH') or define('RPC_CLIENT_PATH', APP_PATH . 'app/Client/Rpc/');
|
||||
defined('MODEL_PATH') or define('MODEL_PATH', APP_PATH . 'app/Models/');
|
||||
defined('SOCKET_PATH') or define('SOCKET_PATH', APP_PATH . 'app/Websocket/');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user