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 not familiar with Apollo, you can look at it first. Swoft extends the Apollo component and reads the official Apollo documentation.

Configuration center usage process

  • Write changes to the local agent listener configuration, modify the local configuration file 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:

Declaration 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 command must be an absolute path (/data/www/swoft/bin/swoft) and the background running mode (-d). The service must start after the agent, otherwise it will not get the latest. Configuration information. This chapter is just a simple use case. Developers can more easily monitor their apollo configuration changes, generate configuration files, and restart services