This commit is contained in:
2021-10-29 18:06:04 +08:00
parent d83228bb97
commit b8efb343b0
3 changed files with 65 additions and 14 deletions
+42 -4
View File
@@ -3,6 +3,10 @@
namespace Kiri\Rpc\Annotation; namespace Kiri\Rpc\Annotation;
use Annotation\Attribute; use Annotation\Attribute;
use Kiri\Abstracts\Config;
use Kiri\Consul\Agent;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Kiri\Rpc\RpcManager; use Kiri\Rpc\RpcManager;
use ReflectionException; use ReflectionException;
@@ -12,10 +16,15 @@ use ReflectionException;
/** /**
* @param string $method * @param string $method
* @param string $version * @param string $driver
* @param string $protocol * @param array $checkOptions
*/ */
public function __construct(public string $method, public string $version = '2.0', public string $protocol = 'json') public function __construct(public string $method, public string $driver, public array $checkOptions = [
"DeregisterCriticalServiceAfter" => "1m",
"Http" => "http://127.0.0.1:9527",
"Interval" => "1s",
"Timeout" => "1s"
])
{ {
} }
@@ -26,10 +35,39 @@ use ReflectionException;
* @param mixed|string $method * @param mixed|string $method
* @return mixed * @return mixed
* @throws ReflectionException * @throws ReflectionException
* @throws ConfigException
*/ */
public function execute(mixed $class, mixed $method = ''): bool public function execute(mixed $class, mixed $method = ''): bool
{ {
return RpcManager::add($this->method, $class); $default = $this->create();
$agent = Kiri::getDi()->get(Agent::class);
$data = $agent->service->register($default);
if ($data->getStatusCode() != 200) {
exit($data->getBody()->getContents());
}
return RpcManager::add($this->method, $class, $default['id']);
}
/**
* @throws ConfigException
*/
protected function create(): array
{
$content = swoole_get_local_ip()['eth0'];
return [
"id" => uniqid("rpc.json.{$this->method}."),
"name" => $this->method,
"address" => swoole_get_local_ip()['eth0'],
"port" => 9526,
"enableTagOverride" => true,
"check" => [
"DeregisterCriticalServiceAfter" => "1m",
"TCP" => $content . ":" . Config::get('rpc.port'),
"Interval" => "1s",
"Timeout" => "1s"
]
];
} }
+5 -7
View File
@@ -59,16 +59,14 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
/** /**
* @param OnBeforeShutdown $beforeShutdown * @param OnBeforeShutdown $beforeShutdown
* @throws ConfigException
*/ */
public function onBeforeShutdown(OnBeforeShutdown $beforeShutdown) public function onBeforeShutdown(OnBeforeShutdown $beforeShutdown)
{ {
$config = Config::get('rpc.registry.config'); $doneList = RpcManager::doneList();
$config = array_change_key_case($config, CASE_LOWER);
$agent = $this->container->get(Agent::class); $agent = $this->container->get(Agent::class);
$agent->service->deregister($config['id']); foreach ($doneList as $value) {
$agent->service->deregister($value);
}
} }
@@ -173,7 +171,7 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
private function dispatch($data): array private function dispatch($data): array
{ {
try { try {
[$handler, $params] = RpcManager::get($data['service'], $data['method']); [$handler, $params, $_] = RpcManager::get($data['service'], $data['method']);
if (is_null($handler)) { if (is_null($handler)) {
throw new \Exception('Method not found', -32601); throw new \Exception('Method not found', -32601);
} else { } else {
+18 -3
View File
@@ -14,10 +14,11 @@ class RpcManager
/** /**
* @param string $name * @param string $name
* @param string $class * @param string $class
* @param string $serviceId
* @return bool * @return bool
* @throws ReflectionException * @throws ReflectionException
*/ */
public static function add(string $name, string $class): bool public static function add(string $name, string $class, string $serviceId): bool
{ {
$methods = Kiri::getDi()->getReflect($class); $methods = Kiri::getDi()->getReflect($class);
$lists = $methods->getMethods(\ReflectionMethod::IS_PUBLIC); $lists = $methods->getMethods(\ReflectionMethod::IS_PUBLIC);
@@ -27,12 +28,26 @@ class RpcManager
foreach ($lists as $reflection) { foreach ($lists as $reflection) {
$methodName = $reflection->getName(); $methodName = $reflection->getName();
static::$_rpc[$name][$methodName] = [[$class, $methodName], null]; static::$_rpc[$name][$methodName] = [[$class, $methodName], null, $serviceId];
} }
return true; return true;
} }
public static function doneList(): array
{
$array = [];
foreach (static::$_rpc as $list) {
foreach ($list as $value) {
$array[] = $value[2];
}
}
return $array;
}
/** /**
* @param string $name * @param string $name
* @param string $method * @param string $method
@@ -40,7 +55,7 @@ class RpcManager
*/ */
public static function get(string $name, string $method): array public static function get(string $name, string $method): array
{ {
return static::$_rpc[$name][$method] ?? [null, null]; return static::$_rpc[$name][$method] ?? [null, null, null];
} }
} }