This topic uses a Spring Cloud application that contains a service provider and a service consumer as an example to demonstrate how to develop and debug the application locally, deploy it to SAE, and enable service registration, discovery, and calls from the consumer to the provider.
Background information
If you are not familiar with Spring Cloud but have a basic understanding of Spring and Maven, this topic will help you learn how to use Spring Cloud Alibaba Nacos Discovery to implement service registration and discovery for Spring Cloud applications. You will also learn how to enable calls from consumers to providers.
If you are familiar with service registration components in Spring Cloud, such as Eureka, Consul, and ZooKeeper, but have not used the Nacos Discovery component of Spring Cloud Alibaba, you only need to replace the existing service registration dependencies and configurations with those for Spring Cloud Alibaba Nacos Discovery. No code modification is required.
Spring Cloud Alibaba Nacos Discovery implements the standard interfaces and specifications of the Spring Cloud Registry. Therefore, the process of connecting to a service registry using Spring Cloud remains largely unchanged.
If you are familiar with using the open source version of Spring Cloud Alibaba Nacos Discovery for service registration and discovery in Spring Cloud applications, you can deploy your applications directly to SAE to use the commercial service registration and discovery features that SAE provides. For more information, see the application hosting overview.
Why use the SAE service registry
The SAE service registry provides a commercial version of the open source Nacos Server. Applications developed with the open source Spring Cloud Alibaba Nacos Discovery can directly use the commercial service registry provided by SAE.
The SAE service registry offers several advantages over self-managed registries, such as Nacos, Eureka, and Consul:
Shared components. You do not need to deploy or maintain Nacos, Eureka, or Consul. This reduces costs.
Encrypted links. Service registration and discovery calls are encrypted to protect your services from being discovered by unauthorized applications.
The SAE service registry is seamlessly integrated with other SAE components, offering a comprehensive microservice solution that includes environment isolation and phased release.
When you deploy an application on SAE, the SAE service registry automatically sets the Nacos Server address, service port, namespace, AccessKey, and Context-path. These settings take precedence, and no additional configuration is required.
If you have a large number of microservices applications, you can use one of the following types of service registries that are listed in descending order by recommendation level:
Preparations
Download Maven and set the environment variable.
Start Nacos Server.
Download and decompress the Nacos Server package.
Go to the nacos/bin directory and start Nacos Server.
Linux, Unix, or macOS: Run the
sudo sh startup.sh -m standalonecommand.Windows: Run the
startup.cmd -m standalonecommand.
Notestandaloneindicates that the startup.cmd file is run in standalone mode, not cluster mode. By default, the startup.cmd file is started in cluster mode. If you double-click the startup.cmd file to run the file in a Windows system, the startup fails. In this case, you must configureMODE="standalone"in the startup.cmd file. For more information, see Quick Start for Nacos.
Step 1: Create a service provider
Create a service provider application project in your local environment. Add dependencies, enable service registration and discovery, and specify Nacos Server as the service registry.
Create a Maven project named
nacos-service-provider.Add dependencies to the
pom.xmlfile.For a specific example, see nacos-service-provider. This topic uses Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1, with the following dependencies:
NoteSpring Boot 2.4 and later are not supported. Spring Cloud Alibaba 2.2.6.RELEASE (client version 1.4.2) is supported.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</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>This example uses Spring Cloud Greenwich. The corresponding version of Spring Cloud Alibaba is 2.1.1.RELEASE.
If you use Spring Cloud Finchley, the corresponding version of Spring Cloud Alibaba is 2.0.1.RELEASE.
If you use Spring Cloud Edgware, the corresponding version of Spring Cloud Alibaba is 1.5.1.RELEASE.
NoteSpring Cloud Edgware has reached the end of its lifecycle. We recommend that you do not use this version to develop applications.
In
src/main/java, create the packagecom.aliware.edas.In the package
com.aliware.edas, create a startup class namedProviderApplicationfor the service provider and add the following code.The
@EnableDiscoveryClientannotation enables service registration and discovery for this application.package com.aliware.edas; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }In the package
com.aliware.edas, createEchoController.In
EchoController, specify the URL mapping as/echo/{string}, set the HTTP method to GET, retrieve the method parameter from the URL path, and return the received parameter.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; } }Create a file
application.propertiesin thesrc\main\resourcespath. Inapplication.properties, add the following configuration to specify the Nacos Server address.spring.application.name=service-provider server.port=18081 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848In this example,
127.0.0.1is the address of the Nacos Server. If your Nacos Server is deployed on a different machine, you must change this to the corresponding IP address. If you have other requirements, you can add configurations to theapplication.propertiesfile. For more information, see Configuration items.Verify the results.
Run the
mainfunction ofProviderApplicationinnacos-service-providerto start the application.Log on to the local Nacos Server console at
http://127.0.0.1:8848/nacos.The default username and password for the local Nacos console are both nacos.
In the navigation pane on the left, choose Service Management > Service List.
The service list now includes
service-provider, and you can view the details of this service by clicking Details.
Step 2: Create a service consumer
This section describes how to create a service consumer that uses Nacos for service discovery and calls the provider using RestTemplate and FeignClient.
Create a Maven project named
nacos-service-consumer.Add dependencies to the
pom.xmlfile.For a specific example, see nacos-service-consumer.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.1.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</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>In the
src/main/javadirectory, create a packagecom.aliware.edas.Configure RestTemplate and FeignClient in the package
com.aliware.edas.In the package
com.aliware.edas, create an interface namedEchoService, add the@FeignClientannotation, and configure the corresponding HTTP URL and method.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") public interface EchoService { @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET) String echo(@PathVariable("str") String str); }In the package
com.aliware.edas, create a startup class namedConsumerApplicationand add the necessary configuration.To enable service registration and discovery, use the
@EnableDiscoveryClientannotation.Use the
@EnableFeignClientsannotation to activate FeignClient.Integrate RestTemplate with service discovery by adding the
@LoadBalancedannotation.
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 @EnableFeignClients public class ConsumerApplication { @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
In the package
com.aliware.edas, create the classTestControllerto demonstrate and verify the service discovery feature.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; @RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET) public String rest(@PathVariable String str) { return restTemplate.getForObject("http://service-provider/echo/" + str, String.class); } @RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET) public String feign(@PathVariable String str) { return echoService.echo(str); } }In the
src\main\resourcespath, create a file namedapplication.properties. In theapplication.propertiesfile, add the following configuration to specify the address of the Nacos Server.spring.application.name=service-consumer server.port=18082 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848In this example,
127.0.0.1is the address of the Nacos Server. If your Nacos Server is deployed on a different machine, you must change the value to the IP address of that machine. If you have other requirements, you can add configurations to theapplication.propertiesfile. For more information, see Configuration items.Verify the results.
Run the
mainfunction of theConsumerApplicationclass in thenacos-service-consumerproject to start the application.Log on to the local Nacos Server console at
http://127.0.0.1:8848/nacos.The default username and password for the local Nacos console are both nacos.
In the navigation pane on the left, choose Service Management > Service List. The service list now includes
service-consumer, and you can view the details of this service by clicking Details.
Step 3: Local testing
Test the service calls from the consumer to the provider in the local environment.
For Linux, Unix, and macOS: run the following commands.
curl http://127.0.0.1:18082/echo-rest/rest-rest curl http://127.0.0.1:18082/echo-feign/feign-restFor Windows: enter http://127.0.0.1:18082/echo-rest/rest-rest and http://127.0.0.1:18082/echo-feign/feign-rest in your browser.
Step 4: Deploy the application to SAE
After you have completed local development and testing of your application, you can package and deploy it to SAE.
nacos-service-provider and nacos-service-consumer are deployed as two separate applications and must be deployed in the same region and namespace. For more information, see Deploy a Java application.
If you deploy a JAR package, set Application Runtime Environment to Standard Java Application Runtime Environment in the application deployment configurations.
If you deploy a WAR package, set Application Runtime Environment to apache-tomcat-XXX in the application deployment configurations.
When you deploy an application to SAE, the SAE service registry automatically sets the Nacos Server address, service port, namespace, AccessKey, and Context-path information with high priority. You do not need to make any additional configurations. You can retain or delete the original configurations.
Step 5: Verify the results
Bind a public-facing CLB to the
nacos-service-consumerapplication deployed on SAE. When you configure public access for the CLB, select HTTP for Network Protocol, set HTTP Port to80, and set Container Port to18082. For more information, see Bind a CLB to an application.In the address bar of a browser, enter the public IP address of the application in the
http://<Public IP address>/echo-rest/rest-restorhttp://<Public IP address>/echo-feign/feign-restformat and press Enter. If a response is returned on the page, the application is deployed successfully.
Configuration items
Configuration item | Key | Default value | Description |
Server address | spring.cloud.nacos.discovery.server-addr | None | The IP address and port on which Nacos Server listens. |
Service name | spring.cloud.nacos.discovery.service | ${spring.application.name} | The name of the current service. |
Network interface name | spring.cloud.nacos.discovery.network-interface | None | If no IP address is configured, the IP address that corresponds to this network interface is registered. If the network interface name is not configured, the IP address of the first network interface is used by default. |
Registered IP address | spring.cloud.nacos.discovery.ip | None | High priority. |
Registered port | spring.cloud.nacos.discovery.port | -1 | You do not need to configure this parameter. The system automatically detects the port. |
Namespace | spring.cloud.nacos.discovery.namespace | None | Logically isolates the registration of different environments. For example, resources such as configurations and services are isolated between the development and production environments. |
Metadata | spring.cloud.nacos.discovery.metadata | None | Configure this parameter in the map format. You can customize service-related metadata based on your requirements. |
Cluster | spring.cloud.nacos.discovery.cluster-name | DEFAULT | The name of the Nacos cluster. |
Endpoint | spring.cloud.nacos.discovery.endpoint | None | The domain name of a service in a region. You can dynamically obtain the server address through this domain name. This configuration is not required when deploying to SAE. |
Integrate with Ribbon | ribbon.nacos.enabled | true | Do not change the value unless necessary. |
For more information about Spring Cloud Alibaba Nacos Discovery, see Nacos Discovery.



