All Products
Search
Document Center

Microservices Engine:Connect Spring Cloud to MSE Nacos

Last Updated:Jun 21, 2026

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

Important

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.

  1. Create a Maven project named nacos-service-provider.

  2. 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.

    Important

    The lifecycle of Spring Cloud Edgware has been discontinued. We recommend that you do not use this version for new applications.

  3. In the src\main\java directory, create a package named com.aliware.edas.

  4. In the com.aliware.edas package, 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);
            }
        }             
    Note

    The @EnableDiscoveryClient annotation enables service registration and discovery functionality for the application.

  5. In the com.aliware.edas package, 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;
          }
    }              
  6. In the src\main\resources directory, create a file named application.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.

    Important

    If 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.

  7. Verify that the application is registered successfully.

    1. Run the main method of ProviderApplication in the nacos-service-provider project to start the application.

    2. Log on to the MSE console, and select a region in the top navigation bar.

    3. In the left-side navigation pane, choose Microservices Registry > Instances. Click the name of the instance.

    4. Service ManagementServices

    5. 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.

  1. Create a Maven project named nacos-service-consumer.

  2. 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>        
  3. In the src\main\java directory, create a package named com.aliware.edas.

  4. In the com.aliware.edas package, configure RestTemplate and FeignClient.

    1. In com.aliware.edas, create an interface class named EchoService, add the @FeignClient annotation, 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);
      }                   
    2. In com.aliware.edas, create the bootstrap class ConsumerApplication and add the relevant configurations.

      • Use the @EnableDiscoveryClient annotation to enable service registration and discovery.

      • Use the @EnableFeignClients annotation to activate FeignClient.

      • Use the @LoadBalanced annotation 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);
          }
      }
  5. In the com.aliware.edas package, 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);
            }
        }           
  6. In the src\main\resources directory, 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.

    Important

    If 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.

  7. Verify that the application is registered successfully.

    1. Run the main method of ConsumerApplication in the nacos-service-consumer project to start the application.

    2. Log on to the MSE console, and select a region in the top navigation bar.

    3. In the left-side navigation pane, choose Microservices Registry > Instances. Click the name of the instance.

    4. Service ManagementServices

    5. 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-rest
  • On 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's address bar.

    A successful response echoes the string provided in the URL. The first command returns rest-rest and the second returns feign-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.

Note

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.

Important

The lifecycle of Spring Cloud Edgware has been discontinued. We recommend that you do not use this version for new applications.