Centralized Configuration Management and Hot Reloading with Nacos

Nacos functions not only as a service registry but also as a dynamic configuration management server. This capability allows for centralized configuration control, enabling updates to be pushed to multiple services simultaneously without requiring individual modifications or service restarts. When a service starts, it merges local configuration files with the remote configuration hosted in Nacos.

In Nacos, configuration files are identified by a Data ID, typically following the convention ${spring.application.name}-${spring.profiles.active}.${file-extension}. The service initialization sequence prioritizes loading the Nacos configuration before processing the local application.yml and creating the Spring container.

Since the application must connect to Nacos to retrieve configuration data before reading the local configuration, the Nacos server address cannot be defined in application.yml. Instead, a bootstrap.yml file is required. This file holds a higher priority and is loaded during the pre-bootstrap phase.

spring:
  application:
    name: order-service
  profiles:
    active: dev
  cloud:
    nacos:
      server-addr: 192.168.1.100:8848
      config:
        file-extension: yaml

Configuration Sharing Across Environments

When multiple environments (e.g., dev, test, prod) share identical configuration properties, a shared configuration file can be created. By naming this file simply as ${spring.application.name}.${file-extension}, it serves as a common base. The loading priority hierarchy is as follows:

  1. bootstrap.yml
  2. Nacos environment-specific configuration (e.g., order-service-dev.yaml)
  3. Nacos shared configuration (e.g., order-service.yaml)
  4. Local application.yml

Implementing Hot Reloading

Hot reloading allows configuration changes in Nacos to take effect immediately without restarting the service instance. To demonstrate this, consider a scenario where a date formatting pattern needs to be dynamic.

First, define a configuration properties class to map the Nacos configuration. Using @ConfigurationProperties is the recommended approach as it automatically supports refresh scopes in Spring Cloud contexts without requiring the @RefreshScope annotation on the bean itself.

package com.example.order.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "format.settings")
public class DateFormatConfig {
    private String pattern;
}

Next, inject this configuration bean into the controller to utilize the dynamic value.

package com.example.order.controller;

import com.example.order.config.DateFormatConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/order")
public class TimeController {

    @Autowired
    private DateFormatConfig dateFormatConfig;

    @GetMapping("/time")
    public String getCurrentTime() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateFormatConfig.getPattern()));
    }
}

Alternatively, if using the @Value annotation, the class must be annotated with @RefreshScope to enable dynamic updates.

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;

@RestController
@RequestMapping("/order")
@RefreshScope
public class TimeController {

    @Value("${format.settings.pattern}")
    private String pattern;

    // ... handler methods
}

To test the functionality, define the configuration in Nacos, for instance setting format.settings.pattern to yyyy-MM-dd HH:mm:ss. Accessing the endpoint will display the time in this format. Upon updating the value to yyyy/MM/dd HH:mm:ss in the Nacos console and publishing the change, refreshing the browser will immediately reflect the new format without a service restart.

Tags: Nacos Spring Cloud Configuration Management microservices Hot Reloading

Posted on Thu, 07 May 2026 19:23:50 +0000 by vomitbomb