All Products
Search
Document Center

Serverless App Engine:Use Spring Cloud to develop microservice applications and deploy them to SAE

Last Updated:Dec 03, 2025

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:

  • Commercial service registry (MSE)

    ZF182Dl2pw

    For information about how to build and deploy an MSE Nacos registry, see Use an MSE Nacos registry.

  • Self-managed service registry

    7D79jXUfBL

    When you build and deploy a self-managed Nacos registry, take note of the following items:

    • Make sure that the network of SAE and the network of the self-managed Nacos registry are interconnected.

    • To prevent the command from being invalid, make sure that you do not use the -D and -XX parameters at the same time. Sample code:

      • Original code:

        java -Dalicloud.deployment.mode=EDAS_MANAGED -XX:+UseContainerSupport -XX:InitialRAMPercentage=70.0 -XX:MaxRAMPercentage=70.0 -XX:+UnlockExperimentalVMOptions -XX:+UseWisp2 -Dio.netty.transport.noNative=true -XX:+UseG1GC -Dspring.profiles.active=yace -Dnacos.use.endpoint.parsing.rule=false -Dnacos.use.cloud.namespace.parsing=false -jar /home/admin/app/xx-server.jar
      • Modified code:

        java -XX:+UseContainerSupport -XX:InitialRAMPercentage=70.0 -XX:MaxRAMPercentage=70.0 -XX:+UnlockExperimentalVMOptions -XX:+UseWisp2 -Dio.netty.transport.noNative=true -XX:+UseG1GC -Dspring.profiles.active=yace -Dnacos.use.endpoint.parsing.rule=false -Dnacos.use.cloud.namespace.parsing=false -jar /home/admin/app/xx-server.jar
    • If you use a self-managed service registry, we recommend that you use an image or a JAR package to deploy an application, and configure the following startup parameters: -Dnacos.use.endpoint.parsing.rule=false and -Dnacos.use.cloud.namespace.parsing=false.

      Important

      To use a non-SAE built-in registry, you must add the required startup parameters before -jar.

      • If you use an image to deploy an application, add -Dnacos.use.endpoint.parsing.rule=false and -Dnacos.use.cloud.namespace.parsing=false to the startup command of the image file. For information about how to create a Docker image, see Example of image creation.

        Sample code is as follows:

        RUN echo 'eval exec java -Dnacos.use.endpoint.parsing.rule=false -Dnacos.use.cloud.namespace.parsing=false -jar $CATALINA_OPTS /home/admin/app/hello-edas-0.0.1-SNAPSHOT.jar'> /home/admin/start.sh && chmod +x /home/admin/start.sh
      • If you use a JAR package to deploy an application, go to the Startup Command Settings section in the SAE console. Then, enter -Dnacos.use.endpoint.parsing.rule=false -Dnacos.use.cloud.namespace.parsing=false in the options Settings field. The following figure shows the startup command that is configured to deploy a Java application in the Open JDK 8 runtime environment. For more information, see Configure a startup command.sc_configure_a_startup_command_for_nacos

  • SAE built-in service registry

    x6JeUmPTQf

    SAE provides the automatic addressing capabilities of Nacos Server for Java microservices applications. You can use the service registration and discovery feature of SAE to configure a service registry. For more information, see Use the SAE built-in Nacos registry.

Preparations

  • Download Maven and set the environment variable.

  • Start Nacos Server.

    1. Download and decompress the Nacos Server package.

    2. Go to the nacos/bin directory and start Nacos Server.

      • Linux, Unix, or macOS: Run the sudo sh startup.sh -m standalone command.

      • Windows: Run the startup.cmd -m standalone command.

      Note

      standalone indicates 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 configure MODE="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.

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

  2. Add dependencies to the pom.xml file.

    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:

    Note

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

      Note

      Spring Cloud Edgware has reached the end of its lifecycle. We recommend that you do not use this version to develop applications.

  3. In src/main/java, create the package com.aliware.edas.

  4. In the package com.aliware.edas, create a startup class named ProviderApplication for the service provider and add the following code.

    The @EnableDiscoveryClient annotation 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);
            }
        }             
  5. In the package com.aliware.edas, create EchoController.

    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;
            }
        }              
  6. Create a file application.properties in the src\main\resources path. In application.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:8848               

    In this example, 127.0.0.1 is 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 the application.properties file. For more information, see Configuration items.

  7. Verify the results.

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

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

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

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

  2. Add dependencies to the pom.xml file.

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

  4. Configure RestTemplate and FeignClient in the package com.aliware.edas.

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

      • To enable service registration and discovery, use the @EnableDiscoveryClient annotation.

      • Use the @EnableFeignClients annotation to activate FeignClient.

      • Integrate RestTemplate with service discovery by adding the @LoadBalanced annotation.

      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 package com.aliware.edas, create the class 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 path, create a file named application.properties. In the application.properties file, 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:8848

    In this example, 127.0.0.1 is 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 the application.properties file. For more information, see Configuration items.

  7. Verify the results.

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

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

    3. 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-rest
  • For 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

  1. Bind a public-facing CLB to the nacos-service-consumer application deployed on SAE. When you configure public access for the CLB, select HTTP for Network Protocol, set HTTP Port to 80, and set Container Port to 18082. For more information, see Bind a CLB to an application.

  2. In the address bar of a browser, enter the public IP address of the application in the http://<Public IP address>/echo-rest/rest-rest or http://<Public IP address>/echo-feign/feign-rest format 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.