Master Nacos: Service Discovery, Dynamic Config, and DNS Integration with Webman
This guide explains what Nacos is, its core features such as service registration, dynamic configuration, and DNS services, and provides step‑by‑step instructions for installing the webman‑nacos plugin, configuring Nacos, writing PHP code to read configurations, and using dynamic listeners and version rollback.
What Is Nacos?
Nacos is an open‑source project from Alibaba that offers service registration and discovery, dynamic configuration, and dynamic DNS services, helping users manage service metadata and traffic.
Key Features
Dynamic configuration service for centralized, externalized, and real‑time management of application and service settings.
Service registration and discovery supporting both DNS‑based and RPC‑based mechanisms.
Dynamic DNS service with weighted routing, flexible load balancing, and simple internal DNS resolution.
Why Use a Configuration Center?
Traditional static configuration suffers from latency, risk of production accidents, inconsistent formats, and lack of audit, version control, and permission management. A configuration center standardizes formats, enables real‑time updates without restarts, provides audit trails, and simplifies stateless service deployment.
Benefits of a Configuration Center
Standardized and unified configuration formats.
Instantaneous effect of configuration changes without restarting services, allowing rapid feature toggles per region or promotion period.
Audit capabilities for tracing configuration changes.
Installing the webman‑nacos Plugin
composer require workbunny/webman-nacosConfiguring Nacos
Plugin configuration file path:
plugin/workbunny/webman-nacos/app.php1. Server Settings
/** nacos server address */
'host' => '192.168.1.2',
/** nacos server port */
'port' => 8848,
/** nacos authentication username */
'username' => 'nacos',
/** nacos authentication password */
'password' => 'nacos',2. Configuration Center Settings
'config_listeners' => [
[
/** DataID */
'payment.php',
/** groupName */
'DEFAULT_GROUP',
/** namespaceId */
'',
/** filePath @desc */
config_path() . '/nacos/payment.php',
],
[
/** DataID */
'application-dev.yml',
/** groupName */
'DEFAULT_GROUP',
/** namespaceId */
'b34ea59f-e240-413b-ba3d-bb040981d773',
/** filePath @desc */
config_path() . '/nacos/application-dev.yml',
],
],The config_path() helper returns the project's config directory.
3. Reading Configuration Files in Code
<?php
declare(strict_types=1);
namespace app\controller;
class Test
{
/**
* @desc nacos configuration center
*/
public function nacos()
{
$client = \Workbunny\WebmanNacos\Client::channel();
// Read payment.php from the public namespace
$payment = $client->config->get('payment.php', 'DEFAULT_GROUP');
var_dump($payment);
echo 'Read namespace configuration' . PHP_EOL;
// Read application-dev.yml from the java namespace
$application = $client->config->get('application-dev.yml', 'DEFAULT_GROUP', 'b34ea59f-e240-413b-ba3d-bb040981d773');
var_dump($application);
}
}4. Adding Configurations via the Nacos Console
Create namespaces (e.g., public and java) and upload configuration files such as payment.php and application-dev.yml. Screenshots of the console are omitted for brevity.
5. Running the Webman Project
php start.php startAfter starting, access http://127.0.0.1:8888/test/nacos to trigger the controller and view the configuration values printed in the console.
6. Dynamic Configuration Listening
The plugin starts a config-listener process that watches the config_listeners array in the plugin config file. You can manually invoke listeners:
$client = \Workbunny\WebmanNacos\Client::channel();
// Asynchronous non‑blocking listener
$response = $client->config->listenerAsyncUseEventLoop();
// Asynchronous blocking listener (returns a Promise)
$response = $client->config->listenerAsync();
// Synchronous blocking listener
$response = $client->config->listener();When a configuration is changed in Nacos, the listener updates the local file without restarting the application.
7. One‑Click Version Rollback
Nacos provides configuration version management and a one‑click rollback feature, allowing quick recovery from erroneous changes and improving system availability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
