This commit is contained in:
2022-05-31 11:11:12 +08:00
parent 95772e794b
commit 0ad17ae4c5
3 changed files with 18 additions and 66 deletions
+2 -54
View File
@@ -20,15 +20,9 @@ use ReflectionException;
/**
* @param string $service
* @param string $driver
* @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 string $checkUrl = '')
public function __construct(public string $service)
{
$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())));
}
@@ -36,58 +30,12 @@ use ReflectionException;
* @param mixed $class
* @param mixed|string $method
* @return mixed
* @throws ConfigException
*/
public function execute(mixed $class, mixed $method = ''): bool
{
$manager = Kiri::getDi()->get(RpcManager::class);
return $manager->add($this->service, $class, $this->create());
return $manager->add($this->service, $class);
}
/**
* @throws ConfigException
*/
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,
"EnableTagOverride" => false,
"TaggedAddresses" => [
"lan_ipv4" => [
"address" => "127.0.0.1",
"port" => $rpcPort
],
"wan_ipv4" => [
"address" => Network::local(),
"port" => $rpcPort
]
],
"Check" => [
"CheckId" => "service:rpc.json.{$this->service}." . $this->uniqueId,
"Name" => "service " . $this->service . ' health check',
"Notes" => "Script based health check",
"ServiceID" => $this->service,
"TCP" => $this->checkUrl,
"Interval" => "5s",
"Timeout" => "1s",
"DeregisterCriticalServiceAfter" => "30s"
],
];
if (!empty($this->meta)) {
$defaultConfig["Meta"] = $this->meta;
}
if (!empty($this->tags)) {
$defaultConfig["tags"] = $this->tags;
}
return $defaultConfig;
}
}
+11 -3
View File
@@ -130,11 +130,19 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
}
/**
* @param OnServerBeforeStart $server
*/
/**
* @param OnServerBeforeStart $server
* @throws ConfigException
*/
public function register(OnServerBeforeStart $server)
{
$consumers = Config::get("rpc.consumers", []);
if (!empty($consumers)) {
$manager = Kiri::getDi()->get(RpcManager::class);
foreach ($consumers as $service => $consumer) {
$manager->add($service, $consumer);
}
}
$this->manager->register();
}
+1 -5
View File
@@ -77,14 +77,10 @@ class RpcManager extends Component
/**
* @param string $name
* @param string $class
* @param array $serviceConfig
* @return bool
*/
public function add(string $name, string $class, array $serviceConfig): bool
public function add(string $name, string $class): bool
{
if (!isset($this->_rpc[$name])) {
// $this->_rpc[$name] = ['id' => $serviceConfig['ID'], 'config' => $serviceConfig];
}
Router::addServer('rpc', static function () use ($name, $class) {
Router::get($name, $class);
});