Managing Configurations in Spring Cloud with Nacos

Nacos offers a centralized configuration service, allowing external management of application properties across all environments while supporting real-time updates without service restarts.

Adding Configuration Files in Nacos

Begin by creating a configuration entry in the Nacos console. For example, define a file named inventory-service-prod.yaml with the following content:

app:
  time-format: yyyy-MM-dd HH:mm:ss

Only configurations that require dynamic adjustment should be placed in Nacos; sttatic properties are better kept within the service’s local application files.

Pulling Configuration into a Microservice

Before integrating Nacos configuration, a Spring Boot application starts by reading application.yml. When Nacos is790 configured, the service must first retrieve the Nacos address; therefore bootstrap.yml is introduced and read prior to application.yml.

1. Add Dependency

Include the Nacos config starter in your service (e.g., inventory-service):

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

2. Create bootstrap.yml

Define the service name, active profile, Nacos server address, and file extension:

spring:
  application:
    name: inventory-service
  profiles:
    active: prod
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
      config:
        file-extension: yaml

The client constructs the data ID as ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}, which becomes inventory-service-prod.yaml.

3. Access Nacos Property

Inject the app.time-format value into a controller:

@RestController
public class FormatController {
    @Value("${app.time-format}")
    private String timeFormat;

    @GetMapping("/time-format")
    public String getTimeFormat() {
        return timeFormat;
    }
}

Summary: Place the configuration in Nacos, add the config dependency, create bootstrap.yml with environment details, and02 theabo Nacos address.

Enabling Hot Refresh of Configuration

After modifying a configuration in Nacos,13 the application can refresh values without restarting. Two approaches exist.

Option A: @RefreshScope

Annotate the class that uses @Value with @RefreshScope:

@RefreshScope
@RestController
public class FormatController {
    @Value("${app.time-format}")
    private String timeFormat;
    // ...
}

Option B: @ConfigurationProperties

Define a properties class and inject it where needed. This automatically refreshes.

@Data
@Component
@ConfigurationProperties(prefix = "app")
public class AppTimeProperties {
    private String timeFormat;
}

alen

@RestController
public class FormatController {
    private final AppTimeProperties properties;

    public FormatController(AppTimeProperties properties) {
        this.properties = properties;
    }

    @GetMapping("/time-format")
    public String getTimeFormat() {
        return properties.getTimeFormat();
    }
}

Shared Configuration Across Environments

Nacos exposes multiple data IDs for a service: one environment-specific (inventory-service-prod.yaml) and one environment-agnostic (inventory-service.yaml). The file without a profile suffix is shared across all environments.

###1 Setup a Shared File

Create inventory-service.yaml in Nacos:

app:
  env-shared: common-value

2. Bind Shared Properties

Extend the properties class to include the shared field:

@Data
@Component
@ConfigurationProperties(prefix = "app")
public class AppTimeProperties {
    private String timeFormat;
    private String envShared;
}

add an endpoint:

@GetMapping("/shared")
public String getShared() {
    return properties.getEnvShared();
}

3. Run Multiple Profiles

Start two instances of the service with different Spring profiles (e.g., prod and staging). Bothwill return common-value from the shared configuration.

4. Priority of Configurations

When the same property appears in multiple sources, the precedence is:

  1. [service]-[profile].yaml
  2. [service].yaml
  3. Local application.yml (or application.properties)

alter a key in inventory-service.yaml and inventory-service-prod.yaml to verify the behaviour: the environment-specific file takes priority over the shared one, and both take priority over local04 settings.

##Conclusion

Nacos configuration management provides centralized, dynamic, and shared properties.01 The integration steps rely on bootstrap.yml and12 appropriate dependencies, while hot refresh is achievable either through @RefreshScope with @Value or via @ConfigurationProperties. Only environment-sensitive properties should be moved to Nacos; the rest can remain local.

Tags: Nacos Spring Cloud Configuration Management Hot Reload microservices

Posted on Sat, 04 Jul 2026 17:29:15 +0000 by maxudaskin