Cloud Native 6 min read

Master Nacos Config Loading Rules in Spring Cloud Alibaba

This article explains how Spring Cloud Alibaba's Nacos module loads configuration by default, maps DataID, Group, and file extensions to client properties, and demonstrates three practical examples for customizing DataID, using YAML, and grouping configurations for different environments.

Programmer DD
Programmer DD
Programmer DD
Master Nacos Config Loading Rules in Spring Cloud Alibaba

Loading Rules

In the default example of the Spring Cloud Alibaba Nacos tutorial, the configuration is loaded using the default values of the Nacos client module. The key elements are:

DataID: alibaba-nacos-config-client.properties Group: DEFAULT_GROUP The correspondence between Nacos entries and Spring Cloud client properties is:

DataID's alibaba-nacos-config-client maps to spring.cloud.nacos.config.prefix, whose default value is ${spring.application.name} (the service name).

DataID's properties maps to spring.cloud.nacos.config.file-extension, defaulting to properties.

Group's value DEFAULT_GROUP maps to spring.cloud.nacos.config.group, also defaulting to DEFAULT_GROUP.

Therefore, an application that uses the defaults loads configuration with the rule: DataID=${spring.application.name}.properties, Group=DEFAULT_GROUP.

Example Scenarios

Example 1 : Load a custom properties file instead of using the service name. spring.cloud.nacos.config.prefix=example This results in DataID=example.properties and Group=DEFAULT_GROUP.

Example 2 : Load a YAML file instead of a properties file.

spring.cloud.nacos.config.prefix=example</code>
<code>spring.cloud.nacos.config.file-extension=yaml

This results in DataID=example.yaml and Group=DEFAULT_GROUP.

Example 3 : Use a custom group to separate configurations.

spring.cloud.nacos.config.prefix=example</code>
<code>spring.cloud.nacos.config.file-extension=yaml</code>
<code>spring.cloud.nacos.config.group=DEV_GROUP

This results in DataID=example.yaml and Group=DEV_GROUP.

Deep Dive

For spring.cloud.nacos.config.prefix and spring.cloud.nacos.config.file-extension, the options are limited; most users can rely on the defaults or change the file extension via spring.cloud.nacos.config.file-extension. The spring.cloud.nacos.config.group property, however, can be used to differentiate configurations across product lines, purposes, or environments, helping avoid naming conflicts.

References

Nacos official documentation

Code Repository

The code examples for this tutorial series are available at:

GitHub: https://github.com/dyc87112/SpringCloud-Learning/

Gitee: https://gitee.com/didispace/SpringCloud-Learning/

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

MicroservicesSpring CloudConfiguration Center
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.