Cloud Native 5 min read

Integrating Nacos Service Discovery into a Spring Cloud Microservice Project – Step‑by‑Step Guide

This guide walks through setting up Nacos as a service discovery platform for a Spring Cloud microservice architecture, covering dependency inclusion, server installation, configuration of each service’s address, enabling discovery annotations, naming services, and accessing the Nacos dashboard, with full code snippets and troubleshooting tips.

Wukong Talks Architecture
Wukong Talks Architecture
Wukong Talks Architecture
Integrating Nacos Service Discovery into a Spring Cloud Microservice Project – Step‑by‑Step Guide

Nacos, an open‑source project from Alibaba, provides dynamic service discovery, configuration management, and service governance for building cloud‑native applications.

1. Add Nacos discovery dependency – Include the following Maven dependency in the pom.xml of the passjava-common module:

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

2. Download and start Nacos Server – Obtain the Nacos server zip from GitHub releases . After extracting, run the appropriate startup script:

Linux/Unix/macOS: sh startup.sh -m standalone Windows: cmd startup.cmd If Windows reports Please set the JAVA_HOME variable…, edit startup.cmd and replace %JAVA_HOME% with the actual JDK path, e.g. C:\Program Files\Java\jdk1.8.0_131. After fixing, the server starts successfully.

3. Configure each microservice – In the application.yml of every service module (passjava‑question, passjava‑channel, etc.), add the Nacos discovery address:

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

4. Enable discovery annotations – Add @EnableDiscoveryClient (and other needed annotations such as @MapperScan) to the main application class of each service:

@EnableDiscoveryClient
@MapperScan("com.jackson0714.passjava.question.dao")
@SpringBootApplication
public class PassjavaQuestionApplication {
    public static void main(String[] args) {
        SpringApplication.run(PassjavaQuestionApplication.class, args);
    }
}

5. Set service name – Define a unique name for each microservice in application.yml:

spring:
  application:
    name: passjava-question

6. Access the Nacos dashboard – Open http://localhost:8848/nacos/index.html#/login, log in with username nacos and password nacos. The dashboard lists all registered services, e.g. passjava-channel, passjava-member, passjava-study, passjava-question, passjava-content.

The complete source code for this tutorial is available at https://github.com/Jackson0714/PassJava-Platform .

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.

javaDockerMicroservicesservice discoveryNacosSpring Cloud
Wukong Talks Architecture
Written by

Wukong Talks Architecture

Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.

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.