Consul

Consul can be used to implement service discovery and configuration for distributed systems. It is a practical open source tool from HashiCorp that supports platforms such as Linux. Consul is distributed, highly available, and scaleable. The swoft-consul component, which incorporates the consul function, allows developers to use the consul function directly from the component.

installation

composer require swoft/consul

使用

本章以操作 consul KV 为例,首先必须配置(启动) swoft-consul 组件

配置

配置如下:

app/bean.php 文件中配置:

return [
    // ...
    'consul' => [
        'host' => '192.168.4.11'
    ]

    // ...
];

详细配置

  • host consul 地址IP
  • port consul 端口号
  • timeout 请求超时时间

实例

<?php declare(strict_types=1);

namespace App\Model\Logic;

use ReflectionException;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Consul\Agent;
use Swoft\Consul\Catalog;
use Swoft\Consul\Exception\ClientException;
use Swoft\Consul\Exception\ServerException;
use Swoft\Consul\Health;
use Swoft\Consul\KV;
use Swoft\Consul\Session;

/**
 * Class ConsulLogic
 *
 * @since 2.0
 *
 * @Bean()
 */
class ConsulLogic
{
    /**
     * @Inject()
     *
     * @var Agent
     */
    private $agent;

    /**
     * @Inject()
     *
     * @var Health
     */
    private $health;

    /**
     * @Inject()
     *
     * @var Catalog
     */
    private $catalog;

    /**
     * @Inject()
     *
     * @var KV
     */
    private $kv;

    /**
     * @Inject()
     *
     * @var Session
     */
    private $session;

    /**
     * @throws ReflectionException
     * @throws ContainerException
     * @throws ClientException
     * @throws ServerException
     */
    public function kv(): void
    {
        $value = 'value content';
        $this->kv->put('/test/my/key', $value);

        $response = $this->kv->get('/test/my/key');
        var_dump($response->getBody(), $response->getResult());
    }
}

Swoft\Consul\Response All operations return are both Swoft\Consul\Response objects, which provide the following methods

  • getHeaders() request returned headers
  • getBody() request returns the original body information
  • getStatusCode() request returns http code
  • getResult() requests data to be formatted by json_decode

You can directly inject the agent / health / catalog / kv / session object is simple and flexible, the actual business injects the corresponding object operation as needed, pay attention to the namespace to be correctly referenced

Method list

Agent

  • Checks()
  • Services()
  • Members(array $options = [])
  • Self()
  • Join(string $address, array $options = [])
  • forceLeave(string $node)
  • registerCheck(array $check)
  • deregisterCheck(string $checkId)
  • passCheck(string $checkId, array $options = [])
  • warnCheck(string $checkId, array $options = [])
  • failCheck(string $checkId, array $options = [])
  • registerService(array $service)
  • deregisterService(string $serviceId)

Health

  • Node(string $node, array $options = [])
  • Checks(string $service, array $options = [])
  • Service(string $service, array $options = [])
  • State(string $state, array $options = [])

Catalog

  • Register(array $node)
  • Deregister(array $node)
  • Datacenters()
  • Nodes(array $options = [])
  • Node(string $node, array $options = [])
  • Services(array $options = [])
  • Service(string $service, array $options = [])

Kv

  • Get(string $key, array $options = [])
  • Put(string $key, string $value, array $options = [])
  • Delete(string $key, array $options = [])

Session

  • Create(array $body = null, array $options = [])
  • Destroy(string $sessionId, array $options = [])
  • Info(string $sessionId, array $options = [])
  • Node(string $node, array $options = [])
  • All(array $options = [])
  • Renew(string $sessionId, array $options = [])
/docs/2.x/en/extra/consul.html
progress-bar