Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b0ed7650c5 | |||
| 3d89cbd4c6 | |||
| 0f2d19f3c7 | |||
| 9001dd041e | |||
| 1bb26b4680 | |||
| a6d825ef9b | |||
| 6cc0334b9b | |||
| d6cb6e40a8 | |||
| fb10622960 | |||
| 2a9c36babb | |||
| bc4e506ea8 | |||
| 59089462e7 | |||
| 845b8b36d1 | |||
| 07436f7035 | |||
| 4d93fcaff2 | |||
| 2e01b60e1e | |||
| 8d2ce1d45c | |||
| 8a3ed5aea5 | |||
| d50b6e6ec7 |
@@ -1,13 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Rpc\Note;
|
||||
namespace Kiri\Rpc\Annotation;
|
||||
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Core\Network;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Kiri\Rpc\RpcManager;
|
||||
use Note\Attribute;
|
||||
use Kiri\Annotation\Attribute;
|
||||
use ReflectionException;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_CLASS)] class JsonRpc extends Attribute
|
||||
@@ -23,10 +23,11 @@ use ReflectionException;
|
||||
* @param array $tags
|
||||
* @param array $meta
|
||||
* @param array $checkOptions
|
||||
* @param string $checkUrl
|
||||
*/
|
||||
public function __construct(public string $service, public string $driver, public array $tags = [], public array $meta = [], public array $checkOptions = [])
|
||||
public function __construct(public string $service, public string $driver, public array $tags = [], public array $meta = [], public array $checkOptions = [], public string $checkUrl = '')
|
||||
{
|
||||
$this->uniqueId = preg_replace('/(\w{11})(\w{4})(\w{3})(\w{8})(\w{6})/', '$1-$2-$3-$4-$5', md5(__DIR__ . '.' . md5(Network::local())));
|
||||
$this->uniqueId = preg_replace('/(\w{11})(\w{4})(\w{3})(\w{8})(\w{6})/', '$1-$2-$3-$4-$5', md5(__DIR__ . 'Annotation' . md5(Network::local())));
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +50,9 @@ use ReflectionException;
|
||||
protected function create(): array
|
||||
{
|
||||
$rpcPort = Config::get('rpc.port');
|
||||
if (empty($this->checkUrl)) {
|
||||
$this->checkUrl = Network::local() . ":" . Config::get('rpc.port');
|
||||
}
|
||||
$defaultConfig = [
|
||||
"ID" => "rpc.json.{$this->service}." . $this->uniqueId,
|
||||
"Name" => $this->service,
|
||||
@@ -66,9 +70,9 @@ use ReflectionException;
|
||||
"Check" => [
|
||||
"CheckId" => "service:rpc.json.{$this->service}." . $this->uniqueId,
|
||||
"Name" => "service " . $this->service . ' health check',
|
||||
"Notes" => "Script based health check",
|
||||
"Annotations" => "Script based health check",
|
||||
"ServiceID" => $this->service,
|
||||
"TCP" => Network::local() . ":" . Config::get('rpc.port'),
|
||||
"TCP" => $this->checkUrl,
|
||||
"Interval" => "5s",
|
||||
"Timeout" => "1s",
|
||||
"DeregisterCriticalServiceAfter" => "30s"
|
||||
@@ -110,6 +110,7 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface
|
||||
* @param $service
|
||||
* @return array
|
||||
* @throws RpcServiceException|\ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function get_consul($service): array
|
||||
{
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Kiri\Rpc;
|
||||
|
||||
use Note\Inject;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Exception;
|
||||
use Http\Message\Response;
|
||||
use Http\Message\Stream;
|
||||
@@ -7,14 +7,15 @@ use Http\Handler\Handler;
|
||||
use Http\Handler\Router;
|
||||
use Http\Message\ServerRequest;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Consul\Agent;
|
||||
use Kiri\Context;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Note\Inject;
|
||||
use Note\Note;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Annotation\Annotation;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use ReflectionException;
|
||||
@@ -42,17 +43,10 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
||||
public Router $router;
|
||||
|
||||
|
||||
#[Inject(Note::class)]
|
||||
public Note $annotation;
|
||||
#[Inject(Annotation::class)]
|
||||
public Annotation $annotation;
|
||||
|
||||
|
||||
#[Inject(EventProvider::class)]
|
||||
public EventProvider $eventProvider;
|
||||
|
||||
|
||||
#[Inject(ContainerInterface::class)]
|
||||
public ContainerInterface $container;
|
||||
|
||||
|
||||
private RpcManager $manager;
|
||||
|
||||
@@ -67,7 +61,6 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
||||
scan_directory(APP_PATH . 'rpc', 'Rpc');
|
||||
|
||||
$this->eventProvider->on(OnWorkerStart::class, [$this, 'consulWatches']);
|
||||
$this->eventProvider->on(OnTaskerStart::class, [$this, 'consulWatches']);
|
||||
$this->eventProvider->on(OnServerBeforeStart::class, [$this, 'register']);
|
||||
|
||||
$this->manager = Kiri::getDi()->get(RpcManager::class);
|
||||
@@ -76,6 +69,7 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
||||
|
||||
/**
|
||||
* @param OnBeforeShutdown $beforeShutdown
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
@@ -92,13 +86,15 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
||||
|
||||
/**
|
||||
* @param OnWorkerStart|OnTaskerStart $server
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function consulWatches(OnWorkerStart|OnTaskerStart $server)
|
||||
{
|
||||
if ($server->workerId != 0) {
|
||||
return;
|
||||
}
|
||||
Timer::tick(1000, static function ($timeId) {
|
||||
$async_time = (int)Config::get('consul.async_time', 1000);
|
||||
Timer::tick($async_time, static function ($timeId) {
|
||||
if (env('state', 'start') == 'exit') {
|
||||
Timer::clear($timeId);
|
||||
return;
|
||||
@@ -110,7 +106,6 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
||||
|
||||
/**
|
||||
* @param OnServerBeforeStart $server
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function register(OnServerBeforeStart $server)
|
||||
{
|
||||
@@ -3,12 +3,13 @@
|
||||
namespace Kiri\Rpc;
|
||||
|
||||
use Http\Handler\Handler;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Consul\Agent;
|
||||
use Kiri\Consul\Health;
|
||||
use Kiri\Kiri;
|
||||
use ReflectionException;
|
||||
|
||||
class RpcManager
|
||||
class RpcManager extends Component
|
||||
{
|
||||
|
||||
|
||||
@@ -87,8 +88,10 @@ class RpcManager
|
||||
}
|
||||
|
||||
foreach ($lists as $reflection) {
|
||||
if ($reflection->getDeclaringClass() != $class) {
|
||||
continue;
|
||||
}
|
||||
$methodName = $reflection->getName();
|
||||
|
||||
$this->_rpc[$name]['methods'][$methodName] = [new Handler('/', [$class, $methodName]), null];
|
||||
}
|
||||
return true;
|
||||
@@ -109,7 +112,6 @@ class RpcManager
|
||||
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
@@ -117,7 +119,7 @@ class RpcManager
|
||||
foreach ($this->_rpc as $list) {
|
||||
$data = $agent->service->register($list['config']);
|
||||
if ($data->getStatusCode() != 200) {
|
||||
exit($data->getBody());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -16,7 +16,7 @@
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Kiri\\Rpc\\": "src/"
|
||||
"Kiri\\Rpc\\": "./"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
|
||||
@@ -61,7 +61,7 @@ return [
|
||||
"node" => "t2.320",
|
||||
"checkId" => "service:redis1",
|
||||
"name" => "Redis health check",
|
||||
"notes" => "Script based health check",
|
||||
"Annotations" => "Script based health check",
|
||||
"status" => "passing",
|
||||
"serviceID" => "redis1",
|
||||
"definition" => [
|
||||
Reference in New Issue
Block a user