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.
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:8848Build 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:8848Run 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 didiReferences
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
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
