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 so that it can be used quickly in Swoft.

installation

 composer require swoft/apollo 

Configuring the bean

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
    ]

    // ...
]; 

Detailed parameters

  • 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.

Once the Apollo configuration is complete, inject it into 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, in addition to this method, swowt-apollo provides more ways to use.

Method list

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 pull of the returned 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 batch encapsulation of the pull method, which is used to pull 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
  • The $callback configuration changes the listener function to make both closure and object methods available.
  • $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