When you create a Spring Cloud application, you can configure the endpoint of a Microservices Engine (MSE) Nacos registry for the application. After the application is started, it can automatically register with the MSE Nacos registry. After the registration, the MSE Nacos registry can implement features such as service discovery and configuration management for the Spring Cloud application. This topic describes how to register Spring Cloud applications with MSE Nacos registries to implement service calls.
Prerequisites
Maven is downloaded and the environment variables are configured.
An instance is created. For more information, see Create an instance.
A namespace is created. For more information, see Create a namespace. In this topic, the default namespace Public is used.
If you access the MSE Nacos registry over the Internet, you must make sure that both the provider and consumer can access the Internet. You must also configure a public IP address whitelist for the MSE Nacos registry that the application registers with. For more information, see Configure a public IP address whitelist.
Create a provider
Create a provider application project on your on-premises machine, add dependencies, enable the service registration and discovery features, and configure the created MSE Nacos engine as a service registry.
Create a Maven project named nacos-service-provider.
Add the following dependencies to the pom.xml file.
The following code provides the required dependencies when Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1 are used.
<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>In this example, Spring Cloud Greenwich is used. The version of Spring Cloud Alibaba for Spring Cloud Greenwich is 2.1.1.RELEASE.
The version of Spring Cloud Alibaba for Spring Cloud Finchley is 2.0.1.RELEASE.
The version of Spring Cloud Alibaba for Spring Cloud Edgware is 1.5.1.RELEASE.
ImportantSpring Cloud Edgware is discontinued. We recommend that you do not use Spring Cloud Edgware to develop applications.
Create a package named com.aliware.edas in the src\main\java project directory.
In the com.aliware.edas package, add the following code to create a boot class named ProviderApplication for the provider.
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 specifies that the service registration and discovery features need to be enabled for the provider.In the com.aliware.edas package, create a class named EchoController. For URL mapping, specify /echo/{string} as the path and GET as the HTTP method. This way, the method parameters are obtained from the URL path and displayed in the output.
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 named application.properties in the src\main\resources project directory. Add the following configuration to the application.properties file to specify the endpoint of the Nacos server.
spring.application.name=service-provider server.port=18081 spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848 #Configure a custom namespace #spring.cloud.nacos.discovery.namespace={namespaceId}On the Instances page, you can view the public endpoint of the MSE Nacos registry. The public endpoint is in the mse.XX.nacos.mse.aliyuncs.com format.
ImportantIf an MSE Zookeeper or Eureka engine is used as the service registry, you must replace the code of the service registry with the code of the ZooKeeper or Eureka registry. For more information, see Usage notes.
Check whether the application is successfully registered.
Execute the main function of ProviderApplication in the nacos-service-provider project to start the provider.
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.
In the left-side navigation pane, choose Service Management > Services.
On the Services page, check whether the application is successfully registered.
Create a consumer
This section describes the service registration feature and describes how to configure RestTemplate and FeignClient to work with the Nacos server.
Create a Maven project named nacos-service-consumer.
Add the following dependencies to the pom.xml file.
<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>Create a package named com.aliware.edas in the src\main\java project directory.
In the com.aliware.edas package, configure RestTemplate and FeignClient.
In the com.aliware.mse package, create an interface class named EchoService, add the
@FeignClientannotation, and then specify the 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 the com.aliware.edas package, create a boot class named ConsumerApplication and add the related configurations.
Add the
@EnableDiscoveryClientannotation to enable the service registration and discovery features.Add the
@EnableFeignClientsannotation to enable FeignClient.Add the
@LoadBalancedannotation to integrate RestTemplate with the service discovery feature.
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.edas package, create a class named TestController to demonstrate and test 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); } }Create a file named application.properties in the src\main\resources project directory. Add the following configuration to the application.properties file to specify the endpoint of the Nacos server.
spring.application.name=service-consumer server.port=18082 spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848 #Configure a custom namespace #spring.cloud.nacos.discovery.namespace={namespaceId}On the Instances page, you can view the public endpoint of the MSE Nacos registry. The public endpoint is in the
mse.XX.nacos.mse.aliyuncs.comformat.ImportantIf an MSE Zookeeper or Eureka engine is used as the service registry, you must replace the code of the service registry with the code of the ZooKeeper or Eureka registry. For more information, see Usage notes.
Check whether the application is successfully registered.
Execute the main function of ConsumerApplication in the nacos-service-consumer project to start the consumer.
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.
In the left-side navigation pane, choose Service Management > Services.
On the Services page, check whether the application is successfully registered.
Test the services in an on-premises environment
Use the consumer to call the provider in an on-premises environment.
On Linux, UNIX, or 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-restOn Windows, enter http://127.0.0.1:18082/echo-rest/rest-rest and http://127.0.0.1:18082/echo-feign/feign-rest in the address bar of a browser and press Enter.
The following figure shows that the service call is successful.

FAQ
What do I do if I cannot view service information in the MSE console after I run my Spring Cloud microservice application?
Your Spring Cloud microservice application is developed on an on-premises environment and registered with an MSE Nacos registry. To resolve this issue, configure a public IP address whitelist to allow your application to access the MSE Nacos registry. For more information, see Configure a public IP address whitelist.
By default, the IP address whitelist is set to 127.0.0.1/32, which indicates that no IP addresses are allowed to access the service registry.
Which Spring Cloud versions does MSE support?
The version of Spring Cloud Alibaba for Spring Cloud Greenwich is 2.1.1.RELEASE.
The version of Spring Cloud Alibaba for Spring Cloud Finchley is 2.0.1.RELEASE.
The version of Spring Cloud Alibaba for Spring Cloud Edgware is 1.5.1.RELEASE.
Spring Cloud Edgware is discontinued. We recommend that you do not use Spring Cloud Edgware to develop applications.