All Products
Search
Document Center

Serverless App Engine:Host Dubbo applications to SAE

Last Updated:Jan 25, 2024

Apache Dubbo (Dubbo) is a microservice framework that provides service discovery capabilities between service providers and service consumers. This topic uses the Dubbo microservice application as an example to describe how to develop applications on your computer by configuring XML files and host the applications to SAE.

Benefits

After you host Dubbo applications to SAE, you can focus on building the logic of Dubbo applications without worrying about the establishment and maintenance of the registry and configuration center. The features provided by SAE, such as auto scaling, quick start and stop of applications in a batch, and application monitoring, can help greatly reduce your development and O&M costs.

Preparations

  • Download Maven and set the environment variables.
  • 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.

Versions

SAE supports Dubbo 2.5.x, 2.6.x, and 2.7.x. We recommended that you use 2.7.x for better service governance. This topic takes the version 2.7.3 as an example to describe how to host Dubbo applications to SAE.

Step 1: Create a service provider

  1. Create a Maven project and add dependencies.
    1. Create a Maven project by using an integrated development environment (IDE), such as IntelliJ IDEA or Eclipse.
    2. Add dubbo, dubbo-registry-nacos, and nacos-client to the pom.xml file.
      <dependencies>
      
          <dependency>
              <groupId>org.apache.dubbo</groupId>
              <artifactId>dubbo</artifactId>
              <version>2.7.3</version>
          </dependency>
      
          <dependency>
              <groupId>org.apache.dubbo</groupId>
              <artifactId>dubbo-registry-nacos</artifactId>
              <version>2.7.3</version>
          </dependency>
      
          <dependency>
              <groupId>com.alibaba.nacos</groupId>
              <artifactId>nacos-client</artifactId>
              <version>1.1.1</version>
          </dependency>
      </dependencies>            
  2. Develop a Dubbo service provider.
    All services in Dubbo are provided as interfaces.
    1. Create a package named com.alibaba.edas in src/main/java.
    2. Create an interface named IHelloService that contains a SayHello method in com.alibaba.edas.
        package com.alibaba.edas;
      
        public interface IHelloService {
            String sayHello(String str);
        }                                
    3. Create a class named IHelloServiceImpl in com.alibaba.edas to implement the interface.
        package com.alibaba.edas;
      
        public class IHelloServiceImpl implements IHelloService {
            public String sayHello(String str) {
                return "hello " + str;
            }
        }                          
  3. Configure the Dubbo service.
    1. Create a file named provider.xml in src/main/resources and open the file.
    2. In provider.xml, add Spring-related XML namespace (xmlns) and XML schema instance (xmlns:xsi), as well as the Dubbo-related XML namespace (xmlns:dubbo) and XML schema instance (xsi:schemaLocation).
      <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
      xmlns="http://www.springframework.org/schema/beans"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
                                      
    3. In provider.xml, expose the interface and implementation class as a Dubbo service.
        <dubbo:application name="demo-provider"/>
      
        <dubbo:protocol name="dubbo" port="28082"/>
      
        <dubbo:service interface="com.alibaba.edas.IHelloService" ref="helloService"/>
      
        <bean id="helloService" class="com.alibaba.edas.IHelloServiceImpl"/>                                
    4. In provider.xml, specify Nacos Server that starts locally as the registry.
      <dubbo:registry address="nacos://127.0.0.1:8848" />                                
      • 127.0.0.1 is the IP address of Nacos Server. If your Nacos Server is deployed on another machine, change the IP address to the corresponding one. When an application is deployed in SAE, the registry address will be replaced with the address of the registry in SAE. You do not need to make any changes.
      • 8848 is the port number of Nacos Server, which cannot be changed.
  4. Start the service.
    1. Create the class Provider in com.alibaba.edas and load Spring context to the main function of Provider based on the following code to expose the configured Dubbo service.
      package com.alibaba.edas;
      
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      public class Provider {
          public static void main(String[] args) throws Exception {
              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
              context.start();
              System.in.read();
          }
      }                
    2. Execute the main function of Provider to start the service.
  5. Log on to the Nacos console at http://127.0.0.1:8848. In the left-side navigation pane, click Services to view the list of providers. You can see that com.alibaba.edas.IHelloService is available in the list of providers. In addition, you can query Service Group and Provider IP of the service.

Step 2: Create a service consumer

  1. Create a Maven project and add dependencies.
    1. Create a Maven project by using an integrated development environment (IDE), such as IntelliJ IDEA or Eclipse.
    2. Add dubbo, dubbo-registry-nacos, and nacos-client to the pom.xml file.
      <dependencies>
      
          <dependency>
              <groupId>org.apache.dubbo</groupId>
              <artifactId>dubbo</artifactId>
              <version>2.7.3</version>
          </dependency>
      
          <dependency>
              <groupId>org.apache.dubbo</groupId>
              <artifactId>dubbo-registry-nacos</artifactId>
              <version>2.7.3</version>
          </dependency>
      
          <dependency>
              <groupId>com.alibaba.nacos</groupId>
              <artifactId>nacos-client</artifactId>
              <version>1.1.1</version>
          </dependency>
      </dependencies>            
  2. Develop a Dubbo service provider.
    All services in Dubbo are provided as interfaces.
    1. Create a package named com.alibaba.edas in src/main/java.
    2. Create an interface named IHelloService that contains a SayHello method in com.alibaba.edas.
      Note Generally, an interface is defined in an independent module. The provider and consumer reference the same module through Maven dependencies. In this topic, two identical interfaces are created for the provider and consumer for ease of description. However, we do not recommend this procedure in actual use.
        package com.alibaba.edas;
      
        public interface IHelloService {
            String sayHello(String str);
        }                                
  3. Configure the Dubbo service.
    1. Create a file named consumer.xml in src/main/resources and open the file.
    2. In consumer.xml, add the Spring-related XML namespace (xmlns) and XML schema instance (xmlns:xsi), as well as the Dubbo-related XML namespace (xmlns:dubbo) and XML schema instance (xsi:schemaLocation).
      <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
      xmlns="http://www.springframework.org/schema/beans"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">                              
    3. Add the following configuration to consumer.xml to subscribe to the Dubbo service:
        <dubbo:application name="demo-consumer"/>
      
        <dubbo:registry address="nacos://127.0.0.1:8848"/>
      
        <dubbo:reference id="helloService" interface="com.alibaba.edas.IHelloService"/>
  4. Start and verify the Dubbo service.
    1. Create the class Consumer in com.alibaba.edas and load Spring context to the main function of Consumer based on the following code to subscribe to and consume the Dubbo service:
          package com.alibaba.edas;
      
          import org.springframework.context.support.ClassPathXmlApplicationContext;
      
          import java.util.concurrent.TimeUnit;
      
          public class Consumer {
              public static void main(String[] args) throws Exception {
                  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"consumer.xml"});
                  context.start();
                  while (true) {
                      try {
                          TimeUnit.SECONDS.sleep(5);
                          IHelloService demoService = (IHelloService)context.getBean("helloService");
                          String result = demoService.sayHello("world");
                          System.out.println(result);
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              }
          }               
    2. Execute the main function of Consumer to start the Dubbo service.
  5. Verify the creation result.
    After the Dubbo service is started, the console outputs hello world continuously, indicating successful service consumption.

    Log on to the Nacos console at http://127.0.0.1:8848. In the left-side navigation pane, click Services. On the Services page, select Callers.

    You can see that com.alibaba.edas.IHelloService is available in the list. In addition, you can query Service Group and Caller IP of the service.

Step 3: Deploy applications to SAE

  1. In the pom.xml file of the application, add the following configuration, and then run the mvn clean package command to compile the local program into an executable JAR package.
    • Provider
      <build>
       <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <executions>
                     <execution>
                         <goals>
                             <goal>repackage</goal>
                         </goals>
                         <configuration>
                             <classifier>spring-boot</classifier>
                             <mainClass>com.alibaba.edas.Provider</mainClass>
                         </configuration>
                     </execution>
                 </executions>
             </plugin>
       </plugins>
      </build>                      
    • Consumer
      <build>
       <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <executions>
                     <execution>
                         <goals>
                             <goal>repackage</goal>
                         </goals>
                         <configuration>
                             <classifier>spring-boot</classifier>
                             <mainClass>com.alibaba.edas.Consumer</mainClass>
                         </configuration>
                     </execution>
                 </executions>
             </plugin>
       </plugins>
      </build>
  2. Deploy the compiled provider and consumer application packages to SAE. For more information, see Deploy a microservices application by using a JAR package in the SAE console.

References

The following table describes the operations that you can perform on an application after you deploy the application on SAE.

Operation

References

Lifecycle management operations, such as updating, starting, stopping, and deleting an application, and scaling in or scaling out the instances for an application

Manage the lifecycle of an application

Performance optimization operations, such as configuring auto scaling policies for an application, binding Server Load Balancer (SLB) instances to an application, and starting or stopping applications in batches

Application status-based operations, such as managing logs, configuring monitoring settings, viewing application events, and viewing change records