spring.cloud.nacos.config.shared-configs property

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.

How spring.cloud.nacos.config.shared-configs[0].data-id works

The 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.

Example 1: Using 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

Example 2: Using 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

3. What is 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.

4. Shared Configuration File in Nacos

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.

  1. Log into Nacos Console: Open the Nacos console at http://localhost:8848/nacos.

  2. Add Configuration:

    • Go to Config Management > Configuration.
    • Click + to create a new configuration.
    • In the Data ID field, enter shared-config.properties (the same data-id you used in the Spring Boot application).
    • Choose the appropriate Group (use DEFAULT_GROUP or any custom group you specify).
    • Enter the configuration content, for example:
      • Save the configuration.

    app.name=MySharedApp app.version=1.0.0

5. Accessing the Shared Configuration in Your Application

Once the shared configuration is successfully loaded from Nacos, you can access the properties in your Spring Boot application just like any other property.

Using @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; } }

Using @ConfigurationProperties:

@ConfigurationProperties(prefix = "app") @Component public class SharedConfig { private String name; private String version; // Getters and setters }

6. Dynamic Refresh (Optional)

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.

  • Add the @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; } }

  • Trigger the refresh using the /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.


Conclusion

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:

  1. Configure spring.cloud.nacos.config.shared-configs in your Spring Boot application.
  2. Create and upload the shared configuration file (shared-config.properties or shared-config.yml) in the Nacos console.
  3. Use @Value or @ConfigurationProperties to access the properties in your Spring Boot application.

你可能感兴趣的:(spring,java,前端)