This topic describes how to use Alibaba Cloud Toolkit to deploy an application to Serverless App Engine (SAE) and monitor the application.

Prerequisites

  • SAE is activated. For more information, see Activate SAE.
  • Maven is downloaded and environment variables are configured.
  • Java Development Kit (JDK) 1.8 or later is downloaded and installed.
  • IntelliJ IDEA 2018.3 or later is downloaded and installed.
    Note The official server of the JetBrains plug-ins is deployed outside China. If you cannot download IntelliJ IDEA due to slow network responses, obtain the offline installation package for IntelliJ IDEA and install it. For more information, see Contact Us.
  • Alibaba Cloud Toolkit is installed and configured in IntelliJ IDEA.

Step 1: Create demo applications in SAE

SAE allows you to deploy applications by using code packages and images. For more information, see Deploy a demo application on SAE.

In this example, JAR packages are used to create a provider application and a consumer application in SAE. For more information, see Deploy a microservices application by using a JAR package in the SAE console.

Step 2: Create a provider

Create a provider application project in an on-premises environment, add dependencies, enable service registration and discovery, and then specify the Nacos server as the service registry.

  1. Create a Maven project named nacos-service-provider.
  2. Add dependencies to the pom.xml file.

    The following sample code provides an example on how to add the Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1 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>
    </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.
    Note Spring Cloud Edgware is discontinued. We recommend that you do not use Spring Cloud Edgware to develop applications.
  3. In src\main\java, create a package named com.aliware.edas.
  4. In the com.aliware.edas package, create a startup class named ProviderApplication for the provider, and add the following sample code:

    The @EnableDiscoveryClient annotation is used to enable the service registration and discovery feature for the provider 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 com.aliware.edas package, create EchoController, and specify /echo/{String} as the URL mapping and GET as the HTTP method. Obtain the method parameter from the URL path, and echo 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. In src\main\resources, create a file named application.properties and add the following configuration to application.properties to specify the IP address of Nacos Server.
    spring.application.name=service-provider
    server.port=18081
    spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848               

    In the preceding configuration, 127.0.0.1 indicates the IP address of your Nacos Server. If your Nacos Server is deployed on another server, you must change the value to the IP address of the server.

    Note If your service registry is a self-managed service registry, replace 127.0.0.1 with the address of the self-managed service registry.
  7. Verify the result.
    1. Execute the main function of ProviderApplication in the nacos-service-provider project to start the provider application.
    2. Log on to the on-premises Nacos Server console at http://127.0.0.1:8848/nacos. The default username and password that can be used to log on to the Nacos Server console are nacos.
    3. In the left-side navigation pane, choose Service Management > Services.
      You can view service-provider in the service list. You can also query the details of the service.

Step 3: Create a consumer

This section describes the service registration feature, and how Nacos Server works with RestTemplate and FeignClient.

  1. Create a Maven project named nacos-service-consumer.
  2. Add 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>        
  3. In src\main\java, create a package named com.aliware.edas.
  4. In the com.aliware.edas package, configure RestTemplate and FeignClient.
    1. In the com.aliware.edas package, create an interface class named EchoService, add the @FeignClient annotation, 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);
      }                   
    2. In the com.aliware.edas package, create a startup class named ConsumerApplication and add the related configurations.
      • Add the @EnableDiscoveryClient annotation to enable service registration and discovery.
      • Add the @EnableFeignClients annotation to enable FeignClient.
      • Add 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 test and validate 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 src\main\resources, create a file named application.properties and add the following configuration to application.properties to specify the IP address of Nacos Server.
    spring.application.name=service-consumer
    server.port=18082
    spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

    In the preceding configuration, 127.0.0.1 indicates the IP address of your Nacos Server. If your Nacos Server is deployed on another server, you must change the value to the IP address of the server.

    Note If your service registry is a self-managed service registry, replace 127.0.0.1:8848 with the address of the self-managed service registry.
  7. Verify the result.
    1. Execute the main function of ConsumerApplication in the nacos-service-consumer project to start the consumer application.
    2. Log on to the on-premises Nacos Server console at http://127.0.0.1:8848/nacos. The default username and password that can be used to log on to the Nacos Server console are nacos.
    3. In the left-side navigation pane, choose Service Management > Services.
      You can view service-consumer in the service list. You can also query the details of the service.

Step 4: Test the services in an on-premises environment

Use the consumer to call the provider in an on-premises environment.

  • For 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-rest
  • On Windows, enter http://127.0.0.1:18082/echo-rest/rest-rest and then http://127.0.0.1:18082/echo-feign/feign-rest in the address bar of a browser and the press Enter key.

In this example, services are tested on Windows.

Successful call

The preceding figure shows that the microservices provider is called by the microservices consumer in an on-premises environment.

Step 5: Deploy the applications to SAE

After you develop an application, you must configure a deployment task in Alibaba Cloud Toolkit to publish your business code to the application that you created in Step 1: Create demo applications in SAE.

  1. Configure an account in Alibaba Cloud Toolkit.
    1. Click the Alibaba Cloud Toolkit icon Alibaba Cloud Toolkit icon and select Preference… from the drop-down list. In the left-side navigation pane of the Settings page, choose Alibaba Cloud Toolkit > Accounts.
    2. On the Accounts page, configure the Access Key ID and Access Key Secret parameters, and click OK.
      Note

      To obtain the Access Key ID and Access Key Secret, perform the following steps:

      On the Accounts page, click Get existing AK/SK to go to the Alibaba Cloud logon page and then log on to Alibaba Cloud. On the Security Management page, obtain the Access Key ID and Access Key Secret.

  2. Configure deployment tasks.
    1. On IntelliJ IDEA, click the Cloud Toolkit icon Alibaba Cloud Toolkit icon and select Deploy to SAE from the drop-down list.
    2. On the Deploy to SAE page, configure the parameters. After you configure the settings, click Apply to save the settings.
      Note If you use a self-managed service registry, you must configure the following startup commands on the Advanced tab: -Dnacos.use.endpoint.parsing.rule=false and -Dnacos.use.cloud.namespace.parsing=false.
      • Configure the settings for the provider application.

        In the Application section, specify the region, namespace, and name of the application that you created in Step 1: Create demo applications in SAE.

        Configure a provider in Alibaba Cloud Toolkit
      • Configure the settings for the consumer application.

        In the Application section, specify the region, namespace, and name of the application that you created in SAE.

        Configure the settings for the consumer application.
  3. Deploy the applications.
    Click Run to run the provider application, and then run the consumer application. Runtime
  4. Verify the result.
    1. Bind a Server Load Balancer (SLB) instance to the consumer application.
      For more information, see Bind an SLB instance to an application. Bind an SLB instance to the consumer
    2. Access the consumer.
      1. Initiate an HTTP request to the consumer.

        curl http://47.111.XX.XX/echo-feign/feign-rest

      2. Initiate an HTTP request to the consumer. The consumer calls the provider.

        curl http://47.111.XX.XX/echo-rest/rest-rest

      Request call
    3. View the call data on the related Application Monitoring dashboards.