Get the host and port automatically

This commit is contained in:
huangzhhui 2019-06-18 19:30:18 +08:00
parent e37415e8ac
commit 71892a921d

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Hyperf\ServiceGovernance\Listener; namespace Hyperf\ServiceGovernance\Listener;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Event\Annotation\Listener; use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface; use Hyperf\Event\Contract\ListenerInterface;
@ -40,6 +41,11 @@ class RegisterServiceListener implements ListenerInterface
*/ */
private $serviceManager; private $serviceManager;
/**
* @var ConfigInterface
*/
private $config;
/** /**
* @var array * @var array
*/ */
@ -53,6 +59,7 @@ class RegisterServiceListener implements ListenerInterface
$this->consulAgent = $container->get(ConsulAgent::class); $this->consulAgent = $container->get(ConsulAgent::class);
$this->logger = $container->get(StdoutLoggerInterface::class); $this->logger = $container->get(StdoutLoggerInterface::class);
$this->serviceManager = $container->get(ServiceManager::class); $this->serviceManager = $container->get(ServiceManager::class);
$this->config = $container->get(ConfigInterface::class);
} }
public function listen(): array public function listen(): array
@ -68,22 +75,21 @@ class RegisterServiceListener implements ListenerInterface
public function process(object $event) public function process(object $event)
{ {
$services = $this->serviceManager->all(); $services = $this->serviceManager->all();
$servers = $this->getServers();
foreach ($services as $serviceName => $paths) { foreach ($services as $serviceName => $paths) {
foreach ($paths as $path => $service) { foreach ($paths as $path => $service) {
if (! isset($service['publishTo'])) { if (! isset($service['publishTo'], $service['server'])) {
continue; continue;
} }
switch ($service['publishTo']) { switch ($service['publishTo']) {
case 'consul': case 'consul':
// @TODO Retrieve the address and port automatically. [$address, $port] = $servers[$service['server']];
$address = '127.0.0.1';
$port = 9502;
$this->logger->debug(sprintf('Service %s[%s] is registering to the consul.', $serviceName, $path), $this->defaultLoggerContext); $this->logger->debug(sprintf('Service %s[%s] is registering to the consul.', $serviceName, $path), $this->defaultLoggerContext);
if ($this->isRegistered($serviceName, $address, $port, $service['protocol'])) { if ($this->isRegistered($serviceName, $address, $port, $service['protocol'])) {
$this->logger->info(sprintf('Service %s[%s] has been already registered to the consul.', $serviceName, $path), $this->defaultLoggerContext); $this->logger->info(sprintf('Service %s[%s] has been already registered to the consul.', $serviceName, $path), $this->defaultLoggerContext);
return; return;
} }
if ($service['ID']) { if (isset($service['ID']) && $service['ID']) {
$nextId = $service['ID']; $nextId = $service['ID'];
} else { } else {
$nextId = $this->generateId($this->getLastServiceId($serviceName)); $nextId = $this->generateId($this->getLastServiceId($serviceName));
@ -167,4 +173,37 @@ class RegisterServiceListener implements ListenerInterface
} }
return false; return false;
} }
private function getServers(): array
{
$result = [];
$servers = $this->config->get('server.servers', []);
foreach ($servers as $server) {
if (! isset($server['name'], $server['host'], $server['port'])) {
continue;
}
if (! $server['name']) {
throw new \InvalidArgumentException('Invalid server name');
}
$host = $server['host'];
if (in_array($host, ['0.0.0.0', 'localhost'])) {
$host = $this->getInternalIp();
}
if (! filter_var($host, FILTER_VALIDATE_IP)) {
throw new \InvalidArgumentException(sprintf('Invalid host %s', $host));
}
$port = $server['port'];
if (! is_numeric($port) || ($port < 0 || $port > 65535)) {
throw new \InvalidArgumentException(sprintf('Invalid port %s', $port));
}
$port = (int)$port;
$result[$server['name']] = [$host, $port];
}
return $result;
}
private function getInternalIp(): string
{
return gethostbyname(gethostname());
}
} }