How to Implement Dynamic Distributed Configuration with Nacos in Spring Boot

This tutorial walks through creating a Spring Boot service, adding local configuration, moving the configuration to Nacos, integrating Nacos into the service, and enabling dynamic refresh so changes take effect without restarting the application.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How to Implement Dynamic Distributed Configuration with Nacos in Spring Boot

Welcome to part 4 of the Nacos series, focusing on distributed configuration practice.

Steps:

Create a Spring Boot service using local configuration.

Create configuration in Nacos.

Integrate Nacos configuration into the service.

Enable dynamic refresh of configuration.

1. Create Service

pom.xml:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.2.5.RELEASE</version>
</parent>
<groupId>com.example</groupId>
<artifactId>configdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>configdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
  <java.version>11</java.version>
</properties>
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!-- add dependency nacos-config -->
  <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  </dependency>
</dependencies>
<dependencyManagement>
  <dependencies>
    <!-- spring cloud dependency management -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Hoxton.SR3</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    <!-- spring cloud alibaba dependency management -->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-alibaba-dependencies</artifactId>
      <version>2.2.1.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

Add configuration (application.yaml):

test:
  name: local-config

Add a test controller to output the configuration value:

@RestController
public class TestController {
    @Value("${test.name}")
    private String test_name;

    @GetMapping("/test")
    public String testpriority() {
        return test_name;
    }
}

Start the application and visit http://localhost:8080/test. The response shows the local configuration value.

2. Create Configuration in Nacos

Open the Nacos console configuration list page:

Click “Create Configuration”, set Data ID to configdemo.yaml, choose YAML format, and enter:

test:
  name: nacos-config

Publish the configuration. The new entry appears in the list:

3. Integrate Nacos Config into Service

Rename application.yaml to bootstrap.yaml because bootstrap.yaml has the highest priority.

Add Nacos settings to bootstrap.yaml:

spring:
  application:
    name: configdemo
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        file-extension: yaml

Now the local test.name entry is removed. Restart the service; the startup log shows that the configuration is loaded from Nacos.

Access http://localhost:8080/test again; the response is now nacos-config, confirming that the service reads the configuration from Nacos.

Explanation: During startup Spring reads bootstrap.yaml, uses spring.application.name and spring.cloud.nacos.config.file-extension to construct the Data ID, which allows it to fetch the corresponding configuration from Nacos.

4. Dynamic Refresh

To enable automatic refresh after modifying Nacos configuration, add the @RefreshScope annotation to the controller.

@RefreshScope
@RestController
public class TestController {
    @Value("${test.name}")
    private String test_name;

    @GetMapping("/test")
    public String testpriority() {
        return test_name;
    }
}

Restart the service, then modify the configuration in the Nacos console (append -new) and publish.

After confirming the change, the new value appears when the endpoint is refreshed, without restarting the service, demonstrating successful dynamic configuration refresh.

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.

NacosSpring BootDistributed ConfigurationDynamic Refresh
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

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.