Enterprise Distributed Application Service (EDAS) provides the general availability (GA) version of the Nacos registry. Spring Cloud applications built with Nacos can connect to this shared registry without code modification -- deploy to EDAS, and the platform handles registry connectivity automatically.
This tutorial walks through building two Spring Cloud microservices -- a service provider and a service consumer -- that register with Nacos for service discovery. By the end, you will have:
A local Nacos Server running in standalone mode.
A service provider that registers with Nacos and exposes a REST endpoint.
A service consumer that discovers the provider through both RestTemplate and FeignClient.
End-to-end service discovery verified locally.
After local verification, deploy both applications to EDAS. See Deploy applications to ECS instances or Deploy applications to Kubernetes clusters.
EDAS supports multiple registries, including Nacos, Eureka, ZooKeeper, and Consul (self-managed or through Microservice Engine (MSE)). Regardless of which registry you use, EDAS provides application management, microservice governance, and cloud-native PaaS capabilities after deployment.
Prerequisites
Before you begin, make sure that you have:
Maven installed and environment variables configured
The latest Nacos Server downloaded and extracted
Start Nacos Server
Navigate to the
nacos/bindirectory.Start Nacos Server in standalone mode:
Linux, UNIX, or macOS:
sudo sh startup.sh -m standaloneWindows: double-click
startup.cmd.
Open
http://127.0.0.1:8848/nacosin your browser to verify that Nacos Server is running. The default username and password are bothnacos.
Project structure
This tutorial creates two independent Maven projects:
nacos-service-provider/
├── pom.xml
└── src/main/
├── java/com/aliware/edas/
│ ├── ProviderApplication.java
│ └── EchoController.java
└── resources/
└── application.properties
nacos-service-consumer/
├── pom.xml
└── src/main/
├── java/com/aliware/edas/
│ ├── ConsumerApplication.java
│ ├── EchoService.java
│ └── TestController.java
└── resources/
└── application.propertiesDownload the complete demo projects: service-provider | service-consumer.
Create the service provider
Step 1: Set up the Maven project
Create a Maven project named nacos-service-provider and add the following dependencies to pom.xml. This example uses Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1:
The Spring Cloud Alibaba version must match your Spring Cloud release:
| Spring Cloud release | Spring Cloud Alibaba version |
|---|---|
| Greenwich | 2.1.1.RELEASE |
| Finchley | 2.0.1.RELEASE |
| Edgware | 1.5.1.RELEASE |
Spring Cloud Edgware has reached end of life. Do not use Edgware for new projects.
Step 2: Create the application class
In src/main/java, create the package com.aliware.edas, then add ProviderApplication.java:
package com.aliware.edas;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient // Enables service registration and discovery
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}Step 3: Add a REST controller
In the same package, create EchoController.java to expose a /echo/{string} endpoint:
package com.aliware.edas;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EchoController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
return string;
}
}Step 4: Configure the registry address
In src/main/resources, create application.properties:
spring.application.name=service-provider
server.port=18081
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848Replace 127.0.0.1 with the IP address of your Nacos Server if it runs on a different machine. For additional configuration options, see Configuration reference.
Step 5: Verify registration
Run the
mainmethod ofProviderApplicationto start the service.Open the Nacos console at
http://127.0.0.1:8848/nacos(username/password:nacos/nacos).Go to Service Management > Services.
service-providerappears in the service list. Click Details to view registration details such as the instance IP and port.
Create the service consumer
The consumer demonstrates two approaches for calling the provider: RestTemplate with client-side load balancing, and FeignClient as a declarative HTTP client.
Step 1: Set up the Maven project
Create a Maven project named nacos-service-consumer and add the following dependencies to pom.xml:
Step 2: Define the Feign client interface
In src/main/java, create the package com.aliware.edas, then add EchoService.java:
package com.aliware.edas;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name = "service-provider") // Refers to the provider's registered service name
public interface EchoService {
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
String echo(@PathVariable("str") String str);
}Step 3: Create the application class
In the same package, create ConsumerApplication.java:
package com.aliware.edas;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient // Enables service registration and discovery
@EnableFeignClients // Activates FeignClient scanning
public class ConsumerApplication {
@LoadBalanced // Integrates RestTemplate with service discovery for client-side load balancing
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}Annotation summary:
@EnableDiscoveryClient-- registers this application with Nacos.@EnableFeignClients-- scans for@FeignClientinterfaces and creates proxy beans.@LoadBalanced-- enables RestTemplate to resolve service names (such asservice-provider) through the registry instead of DNS.
Step 4: Add a test controller
In the same package, create TestController.java with two endpoints that call the provider using different approaches:
package com.aliware.edas;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private EchoService echoService;
// Uses RestTemplate with service discovery to call the provider
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
public String rest(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str,
String.class);
}
// Uses FeignClient to call the provider
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
public String feign(@PathVariable String str) {
return echoService.echo(str);
}
}Step 5: Configure the registry address
In src/main/resources, create application.properties:
spring.application.name=service-consumer
server.port=18082
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848Replace 127.0.0.1 with the IP address of your Nacos Server if it runs on a different machine.
Step 6: Verify registration
Run the
mainmethod ofConsumerApplicationto start the service.Open the Nacos console at
http://127.0.0.1:8848/nacos.Go to Service Management > Services.
service-consumerappears in the service list alongsideservice-provider.
Test service discovery locally
With both applications running, test end-to-end service discovery. The consumer resolves the service name service-provider through Nacos and forwards each request to the provider.
Linux, UNIX, or macOS:
# Test with RestTemplate
curl http://127.0.0.1:18082/echo-rest/rest-rest
# Test with FeignClient
curl http://127.0.0.1:18082/echo-feign/feign-restWindows: Open the following URLs in your browser:
http://127.0.0.1:18082/echo-rest/rest-resthttp://127.0.0.1:18082/echo-feign/feign-rest
Expected output:
| Endpoint | Expected response |
|---|---|
/echo-rest/rest-rest | rest-rest |
/echo-feign/feign-rest | feign-rest |
If both endpoints return the path parameter value, service registration and discovery are working correctly.
Deploy to EDAS
After local verification, deploy both applications to EDAS. EDAS provides a managed Nacos registry, so your applications connect to it automatically without code modification.
For deployment instructions, see:
Configuration reference
Nacos Discovery supports the following configuration properties in application.properties:
| Configuration item | Key | Default value | Description |
|---|---|---|---|
| Server address | spring.cloud.nacos.discovery.server-addr | None | IP address and port of the Nacos Server |
| Service name | spring.cloud.nacos.discovery.service | ${spring.application.name} | Name used to register the service |
| NIC name | spring.cloud.nacos.discovery.network-interface | None | Network interface whose IP address is registered. Defaults to the first NIC if unspecified |
| Registered IP address | spring.cloud.nacos.discovery.ip | None | Overrides the auto-detected IP. Takes highest priority |
| Registered port | spring.cloud.nacos.discovery.port | -1 | Auto-detected by default |
| Namespace | spring.cloud.nacos.discovery.namespace | None | Isolates services across environments (for example, dev, staging, production) |
| Metadata | spring.cloud.nacos.discovery.metadata | None | Custom key-value pairs attached to the service instance. Configured in Map format |
| Cluster | spring.cloud.nacos.discovery.cluster-name | DEFAULT | Nacos cluster name |
| Endpoint | spring.cloud.nacos.discovery.endpoint | UTF-8 | Domain name for dynamic server address resolution. Not required when deployed to EDAS |
| Ribbon integration | ribbon.nacos.enabled | true | Enables Ribbon-based load balancing with Nacos. Change only when necessary |
For the complete reference, see Nacos Discovery on GitHub.