Master Nacos: Simplify Service Registration, Discovery, and Dynamic Configuration in Spring Cloud

An in‑depth guide walks you through Nacos’s role as a unified service registry and configuration center, compares it with Eureka and Config, shows step‑by‑step Maven setup, YML configuration, service discovery, dynamic refresh, namespace and group isolation, persistence, and cluster deployment.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Master Nacos: Simplify Service Registration, Discovery, and Dynamic Configuration in Spring Cloud

Preface

Nacos is Alibaba's open‑source service registry and configuration center, designed to provide developers with a convenient, easy‑to‑use framework.

From the diagram it is clear that Nacos aims to replace three major Spring Cloud components: Eureka, Config, and Bus.

Why is Nacos so popular?

Nacos helps you more agilely and easily build, deliver, and manage microservice platforms. It is the service infrastructure for building service‑centric modern application architectures such as microservice and cloud‑native paradigms.

Compared with Eureka and Config, the latter feel cumbersome because you must set up services yourself and the UI is English‑only.

Both Eureka and Config require you to build a service yourself.

The English interface is not very friendly.

Developers who use Nacos say it is pleasant: no need to set up services, Alibaba provides them, and the UI supports both Chinese and English, which is ideal for beginners. Moreover, the components may become obsolete (e.g., Eureka has been discontinued), so using Nacos avoids that risk.

How to self‑learn?

https://nacos.io/zh-cn/docs/what-is-nacos.html (Chinese and English)

https://spring-cloud-alibaba-group.github.io/github-pages/hoxton/en-us/index.html (English)

https://github.com/alibaba/nacos (project repository)

Video tutorials are also available via the public account "码猿技术专栏" by replying with the keyword "9527".

Version notes

The Maven‑based microservice project uses the following component versions:

JDK 1.8+

Spring Boot 2.2.2.RELEASE

Spring Cloud Hoxton.SR3

Spring Cloud Alibaba 2.2.1.RELEASE

Version compatibility is strict; refer to the official compatibility matrix.

Start Nacos service

For the selected Spring Cloud Alibaba version, the corresponding Nacos version is 1.2.1. Download the matching tag from GitHub and unzip.

Run startup.cmd (Windows) or startup.sh (Linux). After startup, open the browser at http://localhost:8848/nacos and log in with username nacos and password nacos.

Service registration and discovery

Instead of building a local Eureka server, you can directly start Alibaba's Nacos service, allowing developers to focus on business logic.

How to demonstrate?

Create two modules: nacos-provider (service provider) and nacos-consumer (service consumer).

nacos-provider creation

1. Add Maven dependency

The parent pom defines spring-cloud-alibaba-dependencies, so no version needs to be specified in the child module.

2. Configure YML

server:
  port: 9001
spring:
  application:
    ## Service name in Nacos
    name: nacos-provider
  cloud:
    nacos:
      discovery:
        # Nacos address
        server-addr: 127.0.0.1:8848
management:
  endpoints:
    web:
      exposure:
        ## Must use single quotes for special characters
        include: '*'

3. Enable discovery

4. Write a demo service

5. Start the project

After starting, the provider appears in Nacos’s service list.

nacos-consumer creation

1. Add Maven dependency

2. Configure YML

server:
  port: 9002
spring:
  application:
    name: nacos-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
management:
  endpoints:
    web:
      exposure:
        include: '*'

3. Enable discovery

4. Write a demo service

Use @LoadBalanced RestTemplate and call the provider by its service name.

5. Start the project

Both provider and consumer appear in Nacos’s service list, and the consumer can successfully invoke the provider.

Configuration management

Configuration management solves the pain point of updating configurations without restarting services. Nacos supports dynamic refresh.

How to demonstrate?

Create a module nacos-config and add the dependency spring-cloud-starter-alibaba-nacos-config.

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

YAML configuration

spring:
  application:
    name: nacos-config
  profiles:
    active: dev
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: properties
management:
  endpoints:
    web:
      exposure:
        include: '*'

Data ID

DataId follows the pattern ${prefix}-${spring.profiles.active}.${file-extension} where prefix defaults to spring.application.name.

Add a configuration

Add config.version in Nacos and publish it.

Retrieve configuration

Create an entity class annotated with @Component and @RefreshScope and inject the value with @Value("${config.version}").

@Component
@RefreshScope
@Data
public class DynamicConfigEntity {
    @Value("${config.version}")
    private String version;
}

Expose a controller to return the version.

@RestController
@RequestMapping("/nacos")
public class NacosController {
    @Autowired
    private DynamicConfigEntity entity;
    @GetMapping("/test/{id}")
    public String test(@PathVariable("id") Integer id) {
        return "accept one msg id=" + id + "----- version=" + entity.getVersion();
    }
}

After starting, accessing http://localhost:9003/nacos/test/1 shows the configuration value.

Dynamic refresh

Modify the configuration in Nacos; to enable automatic refresh, annotate the entity with @RefreshScope. After restarting the application, changes are reflected without further code changes.

Namespace isolation

Use namespaces (e.g., dev, test, prod) to separate configurations across environments. Specify the namespace ID in bootstrap.yml via spring.cloud.nacos.config.namespace.

Group isolation

Groups allow separating configurations for different business domains. Set group in the shared‑config definition.

Shared configuration

Define shared configs in bootstrap.yml with shared-configs. Example:

spring:
  cloud:
    nacos:
      config:
        shared-configs:
          - dataId: share-config1.properties
            refresh: true
          - dataId: share-config2.properties
            group: ORDER_GROUP
            refresh: true

Inject shared properties with @Value in a component annotated with @RefreshScope.

@Component
@RefreshScope
@Data
public class DynamicConfigEntity {
    @Value("${database.url}")
    private String databaseUrl;
    @Value("${database.user}")
    private String user;
}

Persistence

Nacos stores data by default in an embedded Derby database. To use MySQL, create a database, run nacos-mysql.sql, and configure the datasource in conf/application.properties.

Cluster deployment

Download the desired Nacos version (e.g., 1.2.1) and modify conf/application.properties to set different ports (8848, 8849, 8850). Create conf/cluster.conf listing each node:

172.16.1.84:8848
172.16.1.84:8849
172.16.1.84:8850

Start each node with bash -f ./startup.sh. After all nodes are up, the cluster management page shows three nodes.

Nginx load balancing

upstream nacos{
    server 172.16.1.84:8848;
    server 172.16.1.84:8849;
    server 172.16.1.84:8850;
}
server{
    listen 80;
    location / {
        proxy_pass http://nacos;
    }
}

Client configuration

Configure the service address in Spring Cloud applications either by listing all Nacos nodes directly or by pointing to the Nginx address.

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 172.16.1.84:8848,172.16.1.84:8849,172.16.1.84:8850

Nacos CP vs AP

Nacos supports both CP and AP modes. By default it operates in AP mode (temporary instance registration). Switching to CP mode enables permanent instance registration, allowing you to choose consistency or availability based on your needs.

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.

service discoveryConfiguration ManagementNacosSpring Cloud
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

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.