Cloud Native 11 min read

Mastering Nacos: Service Discovery and Configuration in Spring Cloud Alibaba

This tutorial walks through installing Nacos, using it as a service registry and configuration center with Spring Cloud Alibaba, configuring Maven dependencies, setting up discovery and config modules, demonstrating load balancing, and showing dynamic configuration refresh in a microservices environment.

macrozheng
macrozheng
macrozheng
Mastering Nacos: Service Discovery and Configuration in Spring Cloud Alibaba
Spring Cloud Alibaba provides a one‑stop solution for microservice development, and Nacos—its core component—can serve as both a service registry and a configuration center.

Nacos Overview

Nacos helps discover, configure, and manage microservices, offering features such as service discovery with health monitoring, dynamic configuration management, dynamic DNS for weighted routing, and metadata management.

Service discovery and health monitoring: DNS/RPC‑based discovery with real‑time health checks.

Dynamic configuration service: Centralized, externalized, and dynamic configuration for all environments.

Dynamic DNS service: Weighted routing, flexible load balancing, and simple internal DNS resolution.

Service metadata management: Manage services and metadata from a microservice platform perspective.

Using Nacos as a Registry Center

Install and Run Nacos

Download nacos-server-1.1.4.zip from the official GitHub releases page.

Set the JAVA_HOME environment variable.

Unzip the package and execute startup.cmd in the bin directory.

After a successful start, access http://localhost:8848/nacos (default username/password: nacos).

Create Application Registration to Nacos

Create nacos-user-service and nacos-ribbon-service modules.

Add Spring Cloud Alibaba dependencies in pom.xml:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-alibaba-dependencies</artifactId>
      <version>2.1.0.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

Replace Consul dependencies with Nacos:

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

Update application.yml to use Nacos discovery:

server:
  port: 8206
spring:
  application:
    name: nacos-user-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 # Nacos address
management:
  endpoints:
    web:
      exposure:
        include: '*'

Run the two nacos-user-service instances and the nacos-ribbon-service; the Nacos console shows the registered services.

Load Balancing Demo

Calling http://localhost:8308/user/1 repeatedly shows alternating logs from the two user‑service instances, demonstrating Nacos‑based load balancing.
2019-11-06 14:28:06.458  INFO 12092 --- [nio-8207-exec-2] c.macro.cloud.controller.UserController  : 根据id获取用户信息,用户名称为:macro

Using Nacos as a Configuration Center

We create a nacos-config-client module and add configuration data in Nacos to demonstrate dynamic configuration management.

Create Config Client Module

Add dependencies in pom.xml:

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

Set application.yml to activate the dev profile.

Configure bootstrap.yml for Nacos discovery and config:

server:
  port: 9101
spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        server-addr: localhost:8848
        file-extension: yaml

Create ConfigClientController to expose configuration value:

@RestController
@RefreshScope
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;
    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }
}

Add Configuration in Nacos

DataId format:

${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

.

For nacos-config-client in dev environment with yaml, the DataId is nacos-config-client-dev.yaml.

Add the following content:

config:
  info: "config info for dev"

Start the client and call http://localhost:9101/configInfo to see the configuration value.

config info for dev

Nacos Dynamic Refresh

Modifying the configuration in Nacos triggers an automatic refresh; the application logs show the refreshed property and related Spring events.

2019-11-06 14:50:49.460  INFO 12372 --- [-localhost_8848] ... Loading nacos data, dataId: 'nacos-config-client-dev.yaml' ... Refresh keys changed: [config.info]

Reference

Spring Cloud Alibaba official documentation: https://github.com/alibaba/spring-cloud-alibaba/wiki

Modules Used

springcloud-learning
├── nacos-config-client   -- demo client for Nacos config center
├── nacos-user-service   -- service providing User CRUD, registered to Nacos
└── nacos-service        -- Ribbon service for testing calls

Source Code

https://github.com/macrozheng/springcloud-learning

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.

javaservice discoveryNacosSpring Cloud AlibabaConfiguration Center
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

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.