Commit 5402f3b8 authored by 王源's avatar 王源
Browse files

对注册中心的服务host进行判断,因为在docker环境中会获取到docker内部ip,故添加local_ip配置

parent b9a8d496
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -18,8 +18,9 @@
    "hyperf/validation": "~1.1.0",
    "hyperf/command": "~1.1.0",
    "hyperf/redis": "~1.1.0",
    "hyperf/guzzle": "^1.1@dev",
    "hyperf/config": "^1.1@dev"
    "hyperf/guzzle": "~1.1.0",
    "hyperf/config": "~1.1.0",
    "hyperf/service-governance": "~1.1.0"
  },
  "minimum-stability": "dev",
  "autoload": {
+3 −0
Original line number Diff line number Diff line
@@ -21,6 +21,9 @@ class ConfigProvider
                    ],
                ],
            ],
            'dependencies' => [
                \Hyperf\ServiceGovernance\Listener\RegisterServiceListener::class => \Meibuyu\Micro\Listener\RegisterServiceListener::class,
            ],
            'commands' => [
                \Meibuyu\Micro\Command\RepositoryCommand::class,
                \Meibuyu\Micro\Command\ValidatorCommand::class,
+48 −0
Original line number Diff line number Diff line
<?php
/**
 * Created by PhpStorm.
 * User: Zero
 * Date: 2020/5/6
 * Time: 8:54
 */

namespace Meibuyu\Micro\Listener;

use Hyperf\ServiceGovernance\Listener\RegisterServiceListener as BaseRegisterServiceListener;

class RegisterServiceListener extends BaseRegisterServiceListener
{

    protected 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');
            }
            /**
             * 若在docker中运行,会获取到docker环境中的ip
             * 这里对配置文件中local_ip判断,如果有,直接使用
             */
            $host = isset($server['local_ip']) ? $server['local_ip'] : $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;
    }

}
 No newline at end of file