Nacos access example
This example demonstrates how to integrate Nacos in VextJS to implement service registration and discovery, dynamic configuration during runtime, and remote configuration patch during startup.
VextJS provides the official Nacos plug-in vextjs-nacos, which encapsulates the registration/discovery and runtime configuration subscription processes. For content that must take effect before the framework configuration is frozen (such as database configuration), the bootstrap config provider of src/config/bootstrap.ts should be used.
Recommended to use in layers:
- Service registration/discovery, dynamic switch during runtime: directly use the official plug-in
vextjs-nacos - Boot database/key/infrastructure configuration: Use
src/config/bootstrap.tsto pull Nacos configuration and return patch :::
Preconditions
- Nacos Server 2.x (tested nacos@2.6.1)
- Node.js >= 20.19.0
- VextJS >= 0.3.2
1. Recommendation: Use the vextjs-nacos official plug-in
1. Installation
2. Configuration (src/config/default.ts)
Description:
configis suitable for single configuration scenariosconfigsis suitable for basic configuration + environment coverage configuration split- When both exist at the same time, they will be merged in the order of
config -> configs[0] -> configs[1] ..., with the latter taking precedence.
3. Register plug-in (src/plugins/nacos.ts)
Explicit parameter passing is also supported (overriding app.config.nacos):
Dynamic port (recommended: consistent with app.config.port)
service.port in config/default.ts is a static value and the final merged port number cannot be read.
If the ports of each environment are different (such as sit: 10019 / prod: 20019), it is recommended to dynamically inject it in the plug-in:
In this way, service.port in config/default.ts is only a type placeholder, and the actual registered port is determined by app.config.port.
Each environment only needs to set port: 10019 in the corresponding config file, and nacos will automatically follow.
3.1 It is recommended to use src/config/bootstrap.ts for remote configuration during startup
If you want to pull the database configuration from Nacos before MonSQLize is initialized, do not put this step in a normal plug-in; it is recommended to use createNacosBootstrapProvider() provided by vextjs-nacos directly:
Configuring the priority in this way will enter the official link: default < config profile < local < provider < CLI.
:::info current boundary
createNacosBootstrapProvider() is only responsible for batch pulling and deep merging of JSON object patches during the startup period, which is suitable for content such as databases, keys, and infrastructure configurations that "must take effect before the configuration is frozen."
This return value will enter the provider patch merge link of **app.config and will not automatically become app.remoteConfig.
It is not responsible for:
- Service registration
- Service discovery
app.nacosmountapp.remoteConfiginjection and runtime subscription update
These runtime capabilities are still handled by nacosPlugin().
If you need:
- Service registration/service discovery
- Consistent
app.remoteConfigbehavior with plugins - Continuous subscription updates after configuration changes
All should continue to be processed using nacosPlugin() in src/plugins/nacos.ts instead of completing it in the bootstrap phase.
3.1.1 Can service discovery be done in bootstrap.ts?
By default ** cannot be used directly ** app.nacos!.discover().
The reason is that src/config/bootstrap.ts runs before Vext App is created:
- There is no
appat this time nacosPlugin()has not been executed yet- Therefore
app.nacos/app.remoteConfigdoes not exist
So the recommended bounds are:
- Only configuration patch pulling is done during startup →
createNacosBootstrapProvider() - Runtime service registration/service discovery/configuration subscription →
nacosPlugin()
3.2 Continue to use app.remoteConfig for dynamic configuration during runtime
If the configuration only affects the runtime function switch, grayscale strategy, external API address, etc., and does not need to participate in database / plugins / middlewares initialization, you can directly use the configuration subscription capability of vextjs-nacos:
- After initial startup, the plug-in will pull the Nacos configuration and mount it to
app.remoteConfig - Subsequent configuration changes will automatically update
app.remoteConfig - No need to restart the service
Done. Plugin autocomplete:
- ✅ Register the current service instance to Nacos at startup
- ✅ Pull and subscribe to the configuration center to automatically update
app.remoteConfig - ✅ Follow LIFO order when closing: first
deregisterInstance(stop traffic) → thenconfigClient.close() - ✅ TypeScript type auto-enhancement
app.nacos/app.remoteConfig/config.nacos
4. Use service discovery
For advanced load balancing strategies (weight/consistent hashing), you can directly use app.nacos!.naming.selectInstances(...) to call the nacos SDK native API.
5. Use remote configuration
Nacos configuration content must be legal JSON. When the configuration changes,
app.remoteConfigwill be automatically updated (no need to restart the service).In the routing handler, if you want to read such fields that may be replaced by
app.extend()during runtime, it is recommended to usereq.app.remoteConfiginstead of the closureapp.remoteConfig. The reason is that the closureapppassed indefineRoutes()comes from the collector copy; when the plugin subsequently usesapp.extend("remoteConfig", nextValue)to replace it with a new object, the old reference captured in the closure will not be automatically refreshed.
Multiple configuration runtime example
This approach is suitable for:
- Basic switches + environment coverage
- Common service configuration + tenant/region incremental configuration
- Hierarchical maintenance of grayscale parameters during runtime
2. Best Practices
Service discovery cache (high-frequency calling scenario)
discover() queries Nacos every time. In high QPS scenarios, it is recommended to add local cache:
Nacos SDK's
subscribeinternally maintains a local cache of the instance list, so it is more efficient even without adding a layer cache. The main significance of local caching is to avoid the overhead of calling selectInstances every time discover is called.
Health check endpoint
Nacos configuration data format
Use JSON when creating configurations in the console:
After modification, the vext application will automatically receive the changes through subscription without restarting.
3. Next step
- 📦
vextjs-nacosnpm package — Complete API documentation and change log - 🔭 OpenTelemetry access example — Full observability
- 🔌 Plugin System —
definePlugin()Custom plugin - 🌐 app.fetch — built-in HTTP client (timeouts/retries/requestId propagation)