When you configure a Spring Cloud application with an MSE Nacos instance endpoint, the application automatically registers with the MSE Nacos service registry at startup. This registration enables service discovery, configuration management, and inter-service calls. This topic shows you how to connect your application to the registry.
Prerequisites
Download and install Maven and configure its environment variables.
Create a namespace. This tutorial uses the default namespace,
Public.
To access MSE Nacos over the public network, both the service provider and service consumer must have public network access. You must also add the IP addresses of your applications to the whitelist for the MSE Nacos instance. For more information, see Configure a public IP address whitelist.
Create a service provider
Create a service provider application locally, add the required dependencies, enable service registration and discovery, and specify your MSE Nacos instance as the service registry.
Create a Maven project named nacos-service-provider.
In the pom.xml file, add the following dependencies.
This example uses Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1.
<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.1.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>The Spring Cloud Greenwich release corresponds to Spring Cloud Alibaba version 2.1.1.RELEASE.
If you use the Spring Cloud Finchley release, the corresponding Spring Cloud Alibaba version is 2.0.1.RELEASE.
If you use the Spring Cloud Edgware release, the corresponding Spring Cloud Alibaba version is 1.5.1.RELEASE.
ImportantThe lifecycle of Spring Cloud Edgware has been discontinued. We recommend that you do not use this version for new applications.
In the
src\main\javadirectory, create a package namedcom.aliware.edas.In the
com.aliware.edaspackage, create the service provider bootstrap class ProviderApplication, and add the following code.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); } }NoteThe
@EnableDiscoveryClientannotation enables service registration and discovery functionality for the application.In the
com.aliware.edaspackage, create an EchoController. The method takes a parameter from the URL path and echoes it in the response.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; } }In the
src\main\resourcesdirectory, create a file namedapplication.properties. In the file, add the following configuration and specify the endpoint for the Nacos server.spring.application.name=service-provider server.port=18081 spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848 # To use a custom namespace, uncomment and configure the following line: #spring.cloud.nacos.discovery.namespace={namespaceId}On the Instances page, you can view the public endpoint of your MSE Nacos instance. The format is mse.XX.nacos.mse.aliyuncs.com.
ImportantIf you use an MSE ZooKeeper or Eureka instance as your service registry, you must replace the Nacos-specific configuration in this step with the corresponding configuration for ZooKeeper or Eureka. For more information, see Usage notes for MSE clusters.
Verify that the application is registered successfully.
Run the main method of ProviderApplication in the nacos-service-provider project to start the application.
-
Log on to the MSE console, and select a region in the top navigation bar.
-
In the left-side navigation pane, choose Microservices Registry > Instances. Click the name of the instance.
-
Service ManagementServices
On the Services page, verify that the service-provider application appears in the list.
Create a service consumer
This section shows you how to register a service consumer and use Nacos service discovery with two clients: RestTemplate and FeignClient.
Create a Maven project named nacos-service-consumer.
In the pom.xml file, add the following dependencies.
<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.1.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 package namedcom.aliware.edas.In the
com.aliware.edaspackage, configure RestTemplate and FeignClient.In
com.aliware.edas, create an interface class named EchoService, add the@FeignClientannotation, and configure the corresponding HTTP URL and HTTP 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
com.aliware.edas, create the bootstrap class ConsumerApplication and add the relevant configurations.Use the
@EnableDiscoveryClientannotation to enable service registration and discovery.Use the
@EnableFeignClientsannotation to activate FeignClient.Use the
@LoadBalancedannotation to integrate RestTemplate with service discovery.
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
com.aliware.edaspackage, create a class named TestController to 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\resourcesdirectory, create a file named application.properties. In the file, add the following configuration to specify the address of the Nacos service registry.spring.application.name=service-consumer server.port=18082 spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848 # To use a custom namespace, uncomment and configure the following line: #spring.cloud.nacos.discovery.namespace={namespaceId}On the Instances page, you can view the public endpoint of your MSE Nacos instance. The format is
mse.XX.nacos.mse.aliyuncs.com.ImportantIf you use an MSE ZooKeeper or Eureka instance as your service registry, you must replace the Nacos-specific configuration in this step with the corresponding configuration for ZooKeeper or Eureka. For more information, see Usage notes.
Verify that the application is registered successfully.
Run the main method of ConsumerApplication in the nacos-service-consumer project to start the application.
-
Log on to the MSE console, and select a region in the top navigation bar.
-
In the left-side navigation pane, choose Microservices Registry > Instances. Click the name of the instance.
-
Service ManagementServices
On the Services page, verify that the service-consumer application appears in the list.
Local testing
Test the service call from the service consumer to the service provider locally.
On Linux, Unix, or macOS, use the following commands to make the service calls.
curl http://127.0.0.1:18082/echo-rest/rest-rest curl http://127.0.0.1:18082/echo-feign/feign-restOn Windows, enter
http://127.0.0.1:18082/echo-rest/rest-restandhttp://127.0.0.1:18082/echo-feign/feign-restin your browser's address bar.A successful response echoes the string provided in the URL. The first command returns
rest-restand the second returnsfeign-rest, confirming a successful service call.consumer consumes Provider
FAQ
Service not visible in the MSE console
You must configure the whitelist for your application access. For more information, see Configure a public IP address whitelist.
By default, the MSE whitelist is set to 127.0.0.1/32, blocking all external access.
Supported Spring Cloud versions
For Spring Cloud Greenwich, the corresponding Spring Cloud Alibaba version is 2.1.1.RELEASE.
For Spring Cloud Finchley, the corresponding Spring Cloud Alibaba version is 2.0.1.RELEASE.
For Spring Cloud Edgware, the corresponding Spring Cloud Alibaba version is 1.5.1.RELEASE.
The lifecycle of Spring Cloud Edgware has been discontinued. We recommend that you do not use this version for new applications.