Apollo

Apollo is a distributed configuration center developed by Ctrip's framework department. It can centrally manage the configuration of different environments and different clusters of applications. It can be pushed to the application end in real time after configuration modification, and has the features of standardized authority and process management. Applicable to micro service configuration management scenarios. Based on the API provided by Apollo, Swoft encapsulates it on top of it, making it fast to use in Swoft.

Installation

Composer require swoft/apollo

Configuring beans

Let's take the Apollo command space as the application configuration as an example:

First enable Apollo in app/bean.php configuration

Return [
    // ...
    'apollo' => [
        'host' => '192.168.2.102',
        'timeout' => 6
    ]
    
    // ...
];

Parameter Details

  • host address
  • port port number
  • appId Apollo app ID
  • clusterName Apollo cluster name
  • timeout timeout unit seconds

If you use apollo to listen for update callbacks, the timeout must be greater than 60 seconds

After the Apollo configuration is complete, inject Apollo Swoft\Apollo\Config just like a normal bean.

Use

<?php declare(strict_types=1);

Namespace App\Model\Logic;

Use Swoft\Apollo\Config;
Use Swoft\Apollo\Exception\ApolloException;
Use Swoft\Bean\Annotation\Mapping\Bean;
Use Swoft\Bean\Annotation\Mapping\Inject;

/**
 * Class ApolloLogic
 *
 * @since 2.0
 *
 * @Bean()
 */
Class ApolloLogic
{
    /**
     * @Inject()
     *
     * @var Config
     */
    Private $config;

    /**
     * @throws ApolloException
     */
    Public function pull(): void
    {
        $data = $this->config->pull('application');
        
        // Print data
        Var_dump($data);
    }
}

The above is a simple Apollo configuration pull, and Swoft-apollo provides more methods in addition to this method.

方法列表

pullWithCache

Public function pullWithCache(string $namespace, string $clientIp = ''): array

Pulling the configuration from the Apollo cache, with a delay of 1 second

  • $namespace namespace name
  • $clientIp client IP, automatically obtain the current machine IP for the underlying layer, for grayscale publishing

pull

Public function pull(string $namespace, string $releaseKey = '', string $clientIp = ''): array

Real-time pull configuration without delay

  • $namespace namespace name
  • $releaseKey Last pulled back the version number, more description, refer to Apollo official documentation
  • $clientIp client IP, automatically obtain the current machine IP for the underlying layer, for grayscale publishing

batchPull

Public function batchPull(array $namespaces, string $clientIp = ''): array

This method is a bulk wrapper for the pull method for pulling multiple namespace configurations at the same time, but does not support the $releaseKey parameter.

  • $namespaces namespace name collection array
  • $clientIp client IP, automatically obtain the current machine IP for the underlying layer, for grayscale publishing

listen

Public function listen(array $namespaces, $callback, array $notifications = [], string $clientIp = ''): void

If you poll the pull configuration all the time, a large part of the resources will be wasted. This method is to solve the problem. When the configuration is updated, the function that listens for the change will be called back.

  • $namespaces namespace name collection array
  • $callback configuration changes listener function, you can make closures, object methods are fine.
  • $notifications listener information, detailed description reference Apollo official release document
  • $clientIp client IP, automatically obtain the current machine IP for the underlying layer, for grayscale publishing

If the configuration changes, the listener function callback will pass a parameter, this parameter information records the latest configuration information after the change, such as:

$callback = function(array $data){
    // ...
}
/docs/2.x/en/extra/apollo.html
progress-bar