ConsulAgentFactory.php 1.1 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\ServiceGovernanceConsul;
  12. use Hyperf\Consul\Agent;
  13. use Hyperf\Consul\Client;
  14. use Hyperf\Contract\ConfigInterface;
  15. use Hyperf\Guzzle\ClientFactory;
  16. use Psr\Container\ContainerInterface;
  17. class ConsulAgentFactory
  18. {
  19. public function __invoke(ContainerInterface $container)
  20. {
  21. return new Agent(function () use ($container) {
  22. $config = $container->get(ConfigInterface::class);
  23. $token = $config->get('services.drivers.consul.token', '');
  24. $options = [
  25. 'timeout' => 2,
  26. 'base_uri' => $config->get('services.drivers.consul.uri', Client::DEFAULT_URI),
  27. ];
  28. if (! empty($token)) {
  29. $options['headers'] = [
  30. 'X-Consul-Token' => $token,
  31. ];
  32. }
  33. return $container->get(ClientFactory::class)->create($options);
  34. });
  35. }
  36. }