How to Integrate Nacos Service Discovery with Spring Boot: Step‑by‑Step Guide

This tutorial walks you through creating two Spring Boot microservices, adding Nacos dependencies, configuring discovery, implementing a simple /hello endpoint, and invoking it from a consumer service using RestTemplate and LoadBalancerClient, all with complete code examples.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How to Integrate Nacos Service Discovery with Spring Boot: Step‑by‑Step Guide

1. Create Services and Integrate Nacos

1.1 service‑provider

pom.xml (key parts)

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
  </parent>
  <groupId>com.example</groupId>
  <artifactId>service-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Nacos discovery dependency -->
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
  </dependencies>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR3</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.2.1.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

application.yml configuration

server:
  port: 8081
spring:
  application:
    name: service-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

Main class

@SpringBootApplication
@EnableDiscoveryClient // enable service discovery
public class ServiceProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceProviderApplication.class, args);
    }
}

1.2 service‑consumer

Configuration is similar; only the port and application name differ.

server:
  port: 8082
spring:
  application:
    name: service-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

Main class also uses @EnableDiscoveryClient.

1.3 Test Registration

Start both services; they appear in the Nacos console under the service list, confirming successful registration.

2. Service Invocation

2.1 Provider – Add /hello Endpoint

@RestController
public class TestController {
    @GetMapping("hello")
    public String hello(@RequestParam String name) {
        return "hello " + name;
    }
}

2.2 Consumer – Configure RestTemplate and Call Provider

RestTemplate bean

@Configuration
public class ConsumerConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

Controller that uses LoadBalancerClient to locate the provider and invoke the /hello API

@RestController
public class TestController {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    LoadBalancerClient loadBalancerClient;

    @GetMapping("hello")
    public String hello(@RequestParam String name) {
        ServiceInstance instance = loadBalancerClient.choose("service-provider");
        return restTemplate.getForObject(instance.getUri() + "/hello?name=" + name, String.class);
    }
}

2.3 Run and Verify

After restarting both services, request http://localhost:8082/hello?name=nacos. The response "hello nacos" confirms that the consumer successfully called the provider through Nacos service discovery.

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 Boot
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

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.