ClientFactory.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\ServiceGovernanceNacos;
  12. use Hyperf\Contract\ConfigInterface;
  13. use Hyperf\Nacos\Config;
  14. use Psr\Container\ContainerInterface;
  15. class ClientFactory
  16. {
  17. public function __invoke(ContainerInterface $container): Client
  18. {
  19. $config = $container->get(ConfigInterface::class)->get('services.drivers.nacos', []);
  20. if (! empty($config['uri'])) {
  21. $baseUri = $config['uri'];
  22. } else {
  23. $baseUri = sprintf('http://%s:%d', $config['host'] ?? '127.0.0.1', $config['port'] ?? 8848);
  24. }
  25. return new Client(new Config([
  26. 'base_uri' => $baseUri,
  27. 'username' => $config['username'] ?? null,
  28. 'password' => $config['password'] ?? null,
  29. 'access_key' => $config['access_key'] ?? null,
  30. 'access_secret' => $config['access_secret'] ?? null,
  31. 'guzzle_config' => $config['guzzle']['config'] ?? null,
  32. 'host' => $config['host'] ?? null,
  33. 'port' => $config['port'] ?? null,
  34. 'grpc' => $config['grpc'] ?? [],
  35. ]));
  36. }
  37. }