Cloud Native 11 min read

Master Spring Cloud Alibaba Nacos: Step‑by‑Step Service Discovery Tutorial

This tutorial walks you through installing Nacos, configuring Spring Cloud Alibaba for service registration and discovery, building provider and consumer Spring Boot applications, and testing load‑balanced calls, offering a complete hands‑on guide to cloud‑native microservice integration.

Programmer DD
Programmer DD
Programmer DD
Master Spring Cloud Alibaba Nacos: Step‑by‑Step Service Discovery Tutorial

What is Nacos

Nacos helps you discover, configure and manage microservices, providing simple features for dynamic service discovery, configuration, metadata and traffic management, enabling agile building and operation of cloud‑native applications.

Install Nacos

Download Nacos from the GitHub releases page (version 0.7.0). After extracting, start the standalone server:

Linux/Unix/Mac: sh startup.sh -m standalone Windows: cmd startup.cmd -m standalone Access the management console at http://127.0.0.1:8848/nacos/.

Build Service Provider

Create a Spring Boot project named alibaba-nacos-discovery-server. Add the following dependencies in pom.xml (parent, dependencyManagement, dependencies) to include Spring Cloud Finchley and Spring Cloud Alibaba Nacos discovery.

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.5.RELEASE</version>
</parent>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Finchley.SR1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-alibaba-dependencies</artifactId>
      <version>0.2.1.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  </dependency>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.2</version>
    <optional>true</optional>
  </dependency>
</dependencies>

Create the main class with @SpringBootApplication and @EnableDiscoveryClient, and a controller exposing a /hello endpoint.

@EnableDiscoveryClient
@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

@Slf4j
@RestController
static class TestController {
    @GetMapping("/hello")
    public String hello(@RequestParam String name) {
        log.info("invoked name = " + name);
        return "hello " + name;
    }
}

Configure Provider

Set application name and Nacos address in application.properties:

spring.application.name=alibaba-nacos-discovery-server
server.port=8001
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

Build Service Consumer

Create a Spring Boot project named alibaba-nacos-discovery-client-common with the same dependencies. Implement a controller that uses LoadBalancerClient to choose a provider instance and call its /hello endpoint.

@EnableDiscoveryClient
@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

@Slf4j
@RestController
static class TestController {
    @Autowired
    LoadBalancerClient loadBalancerClient;

    @GetMapping("/test")
    public String test() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("alibaba-nacos-discovery-server");
        String url = serviceInstance.getUri() + "/hello?name=" + "didi";
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(url, String.class);
        return "Invoke : " + url + ", return : " + result;
    }
}

Configure Consumer

Set application name and Nacos address (port 9000) similarly:

spring.application.name=alibaba-nacos-discovery-client-common
server.port=9000
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

Run and Test

Start both applications (multiple instances can be launched with different -Dserver.port values). Use curl to call the consumer endpoint http://localhost:9000/test, which will invoke a provider instance via load balancing.

$ curl localhost:9000/test
Invoke : http://10.123.18.216:8001/hello?name=didi, return : hello didi

References

Nacos official documentation: https://nacos.io/zh-cn/docs/what-is-nacos.html

Source code repository: https://github.com/dyc87112/SpringCloud-Learning/tree/master/4-Finchley

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.

Microservicesservice discoverySpring BootSpring Cloud
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.