Cloud Native 7 min read

Integrating Nacos with Spring Boot: Features, Advantages, and Code Example

This article introduces Alibaba's open‑source Nacos service for dynamic configuration and service discovery, explains its core features and the performance benefits of Nacos 2.x with gRPC, and provides step‑by‑step Spring Boot integration code, configuration examples, and a live demonstration using the Nacos web console.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Integrating Nacos with Spring Boot: Features, Advantages, and Code Example

Spring Boot is a popular microservice framework, and Nacos is Alibaba's open‑source Naming Configuration Service focused on service discovery and configuration management, supporting Java, Go, Python, C#, C++ and other languages.

Core features include a dynamic configuration service that centralizes and externalizes environment settings, a service discovery and management component with DNS‑based and RPC‑based (Dubbo, gRPC) modes plus health checks, and a dynamic DNS service offering weighted routing, flexible load balancing, and simple internal DNS resolution.

Nacos 2.x optimizes the communication layer by adopting gRPC for long‑living connections, which reduces frequent polling, lowers JVM Full GC pressure, and introduces keepalive messages, faster TCP disconnect detection, reliable streaming push, mitigation of TIME_WAIT issues, and finer‑grained synchronization to lessen inter‑node traffic.

To integrate Nacos with Spring Boot, add the following Maven dependencies:

<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-config-spring-boot-starter</artifactId>
    <version>0.2.1</version>
</dependency>
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-config-spring-boot-actuator</artifactId>
    <version>0.2.1</version>
</dependency>

Configure the Nacos server address in application.yaml:

nacos:
  config:
    server-addr: 127.0.0.1:8848

Use @NacosPropertySource on the main class to specify the dataId and enable auto‑refresh, and @NacosValue to inject configuration values dynamically:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})
@NacosPropertySource(dataId = "bulking-nacos-example", autoRefreshed = true)
public class StartApplication {
    public static void main(String[] args) {
        SpringApplication.run(StartApplication.class, args);
    }
}

An example controller demonstrates retrieving a configuration value via @NacosValue and exposing it through a REST endpoint:

@Controller
@RequestMapping("config")
public class ConfigController {
    @NacosValue(value = "${useLocalCache}", autoRefreshed = true)
    private boolean useLocalCache;

    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public boolean get() {
        return useLocalCache;
    }
}

Run the application; Nacos provides a web console at http://localhost:8848/nacos/#/login (default credentials: nacos/nacos) to manage configurations, add dynamic config items, and observe real‑time updates without restarting the service.

The sample project is available at https://github.com/aalansehaiyang/spring-boot-bulking (module spring-boot-bulking-naco).

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.

JavaConfiguration ManagementgRPCNacosSpring Boot
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

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.