Configuration center

The configuration center is mainly to centralize the configuration management to facilitate uniformity and maintenance. This chapter uses Apollo as an example to pull configuration and secure restart services from the remote configuration center. If you are unfamiliar with Apollo, you can first look at Swoft's extended Apollo components and read the official Apollo documentation.

Configuration center usage process

  • Write local agent listener configuration changes, modify local configuration files if there are changes
  • Restart the service corresponding to the service

The local agent must be started before the service, otherwise the service starts and cannot get the latest configuration information.


This chapter uses apollo in Swoft as an example. When the apollo configuration changes, restart the service (http-server / rpc-server/ ws-server). The following is an example of an agent:

Declare agent

 <?php declare(strict_types=1);

namespace App\Console\Command;

use ReflectionException;
use Swoft\Apollo\Config;
use Swoft\Apollo\Exception\ApolloException;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Co;
use Swoft\Console\Annotation\Mapping\Command;
use Swoft\Console\Annotation\Mapping\CommandMapping;
use Swoft\Http\Server\HttpServer;
use Swoft\Log\Helper\CLog;
use Swoft\Rpc\Server\ServiceServer;
use Swoft\WebSocket\Server\WebSocketServer;
use Throwable;

 * Class AgentCommand
 * @since 2.0
 * @Command("agent")
class AgentCommand
     * @Inject()
     * @var Config
    private $config;

     * @CommandMapping(name="index")
    public function index(): void
        $namespaces = [

        while (true) {
            try {
                $this->config->listen($namespaces, [$this, 'updateConfigFile']);
            } catch (Throwable $e) {
                CLog::error('Config agent fail(%s %s %d)!', $e->getMessage(), $e->getFile(), $e->getLine());

     * @param array $data
     * @throws ContainerException
     * @throws ReflectionException
    public function updateConfigFile(array $data): void
        foreach ($data as $namespace => $namespaceData) {
            $configFile = sprintf('@config/%s.php', $namespace);

            $configKVs = $namespaceData['configurations'] ?? '';
            $content   = '<?php return ' . var_export($configKVs, true) . ';';
            Co::writeFile(alias($configFile), $content, FILE_NO_DEFAULT_CONTEXT);

            CLog::info('Apollo update success!');

            /** @var HttpServer $server */
            $server = bean('httpServer');

//            /** @var ServiceServer $server */
//            $server = bean('rpcServer');
//            $server->restart();

//            /* @var WebSocketServer $server */
//            $server = bean('wsServer');
//            $server->restart();

This declares a local agent that listens for apollo remote configuration changes. If there is a change, the callback function returns the latest configuration information, re-modifies the configuration file according to the actual situation of the service, and then restarts the corresponding service.

Start agent

 php bin/swoft agent:index 

Agent starts, can be started by the background daemon, prevent hanging

Start service

Take the Http server as an example

 /usr/local/php/bin/php /data/www/swoft/bin/swoft http:start -d 

The path to the startup command must be an absolute path (/data/www/swoft/bin/swoft) and the background running mode (-d). The service must be started after the agent, otherwise the latest configuration information cannot be obtained. This chapter is just a simple use case. Developers can change their actual business situation, monitor apollo configuration changes, generate configuration files, and restart services.