This commit is contained in:
2021-12-02 15:25:41 +08:00
parent 14d395be5e
commit 439e3c566e
4 changed files with 79 additions and 27 deletions
+45 -1
View File
@@ -3,7 +3,7 @@
namespace Kiri\Rpc;
use Kiri\Consul\Agent;
use Kiri\Core\Json;
use Kiri\Consul\Health;
use Kiri\Kiri;
use ReflectionException;
@@ -17,6 +17,50 @@ class RpcManager
private array $_rpc = [];
private array $_services = [];
/**
* @param $serviceName
* @return array
* @throws ReflectionException
*/
public function async($serviceName): array
{
$lists = Kiri::getDi()->get(Health::class)->setQuery('passing=true')->service($serviceName);
if ($lists->getStatusCode() != 200) {
return [];
}
var_dump($lists->getBody());
$body = json_decode($lists->getBody(), true);
if (empty($body) || !is_array($body)) {
return $this->_services = [];
}
return $this->_services[$serviceName] = array_column($body, 'service');
}
/**
* @throws ReflectionException
*/
public function tick(): void
{
foreach ($this->_rpc as $name => $list) {
$this->async($name);
}
}
/**
* @param $serviceName
* @return array
*/
public function getServices($serviceName): array
{
return $this->_services[$serviceName] ?? [];
}
/**
* @param string $name
* @param string $class