In Nacos, the spring.cloud.nacos.config.shared-configs
property is used to load additional shared configurations that are not directly tied to the specific application but can be shared across multiple applications or environments. These shared configurations can be stored in Nacos and pulled into multiple Spring Boot applications.
When using spring.cloud.nacos.config.shared-configs
, the data-id
refers to the identifier of the configuration file in Nacos, which can be a .properties
or .yml
file, or any other configuration format supported by Nacos.
spring.cloud.nacos.config.shared-configs[0].data-id
worksThe spring.cloud.nacos.config.shared-configs
array allows you to specify multiple shared configurations that your application can load from Nacos.
Here’s an example of how to use shared-configs
in the application.properties
or application.yml
of your Spring Boot application.
spring.cloud.nacos.config.shared-configs
in application.properties
In this example, you want to load a shared configuration (shared-config.properties
) from Nacos, which can be used across multiple services.
# application.properties spring.application.name=my-app spring.cloud.nacos.config.server-addr=localhost:8848 # Nacos server address spring.cloud.nacos.config.file-extension=properties # Or 'yaml' if you're using YAML files # Shared configuration file from Nacos spring.cloud.nacos.config.shared-configs[0].data-id=shared-config.properties spring.cloud.nacos.config.shared-configs[0].group=DEFAULT_GROUP # Optional: Default group or specify your own spring.cloud.nacos.config.shared-configs[0].refresh=true # Optionally enable automatic refresh
spring.cloud.nacos.config.shared-configs
in application.yml
Alternatively, you can use application.yml
for a more structured configuration.
spring: application: name: my-app cloud: nacos: config: server-addr: localhost:8848 # Nacos server address file-extension: properties # Or 'yaml' if you're using YAML files shared-configs: - data-id: shared-config.properties group: DEFAULT_GROUP # Optional: Default group or specify your own refresh: true # Optionally enable automatic refresh
shared-configs
and Why Use It?data-id
: This is the ID of the configuration file stored in Nacos. You typically use a name like shared-config.properties
or shared-config.yml
, but this can be any valid Nacos config ID.
group
: This specifies the group of the configuration in Nacos. By default, it's DEFAULT_GROUP
, but you can specify any custom group if needed.
refresh
: If set to true
, Spring Boot will refresh the configuration values dynamically (using the /actuator/refresh
endpoint or automatic refresh functionality). This is useful if your configuration file in Nacos is updated and you want those changes to take effect without restarting the application.
Once you have specified spring.cloud.nacos.config.shared-configs
in your Spring Boot application's configuration, the next step is to create the configuration file (shared-config.properties
in this case) in the Nacos console.
Log into Nacos Console: Open the Nacos console at http://localhost:8848/nacos
.
Add Configuration:
shared-config.properties
(the same data-id
you used in the Spring Boot application).DEFAULT_GROUP
or any custom group you specify).app.name=MySharedApp app.version=1.0.0
Once the shared configuration is successfully loaded from Nacos, you can access the properties in your Spring Boot application just like any other property.
@Value
:@RestController public class ConfigController { @Value("${app.name}") private String appName; @Value("${app.version}") private String appVersion; @GetMapping("/config") public String getConfig() { return "Shared Config - App Name: " + appName + ", Version: " + appVersion; } }
@ConfigurationProperties
:@ConfigurationProperties(prefix = "app") @Component public class SharedConfig { private String name; private String version; // Getters and setters }
If you want to enable automatic updates of the shared configuration without restarting the Spring Boot application, you can use Spring Cloud’s refresh mechanism.
@RefreshScope
annotation to the relevant beans:@RefreshScope @RestController public class ConfigController { @Value("${app.name}") private String appName; @Value("${app.version}") private String appVersion; @GetMapping("/config") public String getConfig() { return "Shared Config - App Name: " + appName + ", Version: " + appVersion; } }
/actuator/refresh
endpoint (if you have the spring-boot-starter-actuator
dependency):curl -X POST http://localhost:8080/actuator/refresh
This will automatically reload the properties from Nacos without needing to restart the application.
To summarize, you can use spring.cloud.nacos.config.shared-configs
to load shared configuration files from Nacos in your Spring Boot application. This is useful for scenarios where you want to manage common configurations (like database settings, app-specific settings, etc.) that can be shared across multiple services or environments.
Steps:
spring.cloud.nacos.config.shared-configs
in your Spring Boot application.shared-config.properties
or shared-config.yml
) in the Nacos console.@Value
or @ConfigurationProperties
to access the properties in your Spring Boot application.