全部產品
Search
文件中心

Serverless App Engine:將Dubbo應用託管到SAE

更新時間:Feb 26, 2025

本文以包含服務提供者(本文簡稱Provider)和服務消費者(本文簡稱Consumer)的Dubbo微服務應用為例,介紹如何在本地通過XML配置的方式,開發Dubbo微服務樣本應用,並部署到Serverless 應用引擎 SAE(Serverless App Engine)

準備工作

  • 下載Maven並設定環境變數。

  • 啟動Nacos Server。

    1. 下載並解壓Nacos Server

    2. 進入nacos/bin目錄,啟動Nacos Server。

      • Linux、Unix、macOS系統:執行命令sudo sh startup.sh -m standalone

      • Windows系統:執行命令startup.cmd -m standalone

      說明

      standalone表示單機模式運行,非叢集模式。startup.cmd檔案預設以叢集模式啟動,因此您在使用Windows系統時,如果直接雙擊執行startup.cmd檔案會導致啟動失敗,此時需要在startup.cmd檔案內設定MODE="standalone"。 更多資訊,請參見Nacos快速開始

版本說明

Dubbo 2.7.x、Dubbo 3.0.x 版本的生命週期已於2023年03月結束,請使用新版本開發您的應用。本文以Dubbo 3.2.15版本為例,介紹如何將Dubbo應用託管到SAE。

步驟一:建立服務提供者

在本地建立一個提供者應用工程,添加依賴、佈建服務註冊與發現,並將註冊中心指定為Nacos。

  1. 建立Maven專案並引入依賴。

    1. 使用IDE(如IntelliJ IDEA或Eclipse)建立一個Maven專案。

    2. pom.xml檔案中添加dubbo、dubbo-registry-nacos和nacos-client依賴。

      <dependencies>
      
          <dependency>
              <groupId>org.apache.dubbo</groupId>
              <artifactId>dubbo</artifactId>
              <version>3.2.15</version>
          </dependency>
      
          <dependency>
              <groupId>org.apache.dubbo</groupId>
              <artifactId>dubbo-registry-nacos</artifactId>
              <version>3.2.15</version>
          </dependency>
      
          <dependency>
              <groupId>com.alibaba.nacos</groupId>
              <artifactId>nacos-client</artifactId>
              <version>1.1.1</version>
          </dependency>
      </dependencies>            
  2. 開發Dubbo服務提供者。

    Dubbo中服務都是以介面形式提供。

    1. src/main/java路徑下建立一個packagecom.alibaba.edas

    2. com.alibaba.edas下建立一個介面(interface)IHelloService,裡麵包含一個sayHello方法。

        package com.alibaba.edas;
      
        public interface IHelloService {
            String sayHello(String str);
        }                                
    3. com.alibaba.edas下建立一個類IHelloServiceImpl,實現此介面。

        package com.alibaba.edas;
      
        public class IHelloServiceImpl implements IHelloService {
            public String sayHello(String str) {
                return "hello " + str;
            }
        }                          
  3. 配置Dubbo服務。

    1. src/main/resources路徑下建立provider.xml檔案並開啟。

    2. provider.xml中,添加Spring相關的XML Namespace(xmlns)和XML Schema Instance(xmlns:xsi),以及Dubbo相關的Namespace(xmlns:dubbo)和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. provider.xml中將介面和實作類別暴露成Dubbo服務。

        <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. provider.xml中將註冊中心指定為本地啟動的Nacos Server。

      <dubbo:registry address="nacos://127.0.0.1:8848" />                                
      • 127.0.0.1為Nacos Server的地址。如果您的Nacos Server部署在另外一台機器,則需要修改成對應的IP地址。當將應用部署到SAE後,無需做任何修改,註冊中心會替換成SAE上的註冊中心的地址。

      • 8848為Nacos Server的連接埠號碼,不可修改。

  4. 啟動服務。

    1. com.alibaba.edas中建立類Provider,並按下面的代碼在Provider的main函數中載入Spring Context,將配置好的Dubbo服務暴露。

      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. 執行Provider的main函數,啟動服務。

  5. 登入Nacos控制台http://127.0.0.1:8848,在左側導覽列中單擊服務列表,查看提供者列表。

    可以看到服務提供者裡已經包含了com.alibaba.edas.IHelloService,且可以查詢該服務的服務分組提供者IP

步驟二:建立服務消費者

在本地建立一個消費者應用工程,添加依賴、訂閱服務的配置。

  1. 建立Maven專案並引入依賴。

    1. 使用IDE(如IntelliJ IDEA或Eclipse)建立一個Maven專案。

    2. pom.xml檔案中添加dubbo、dubbo-registry-nacos和nacos-client依賴。

      <dependencies>
      
          <dependency>
              <groupId>org.apache.dubbo</groupId>
              <artifactId>dubbo</artifactId>
              <version>3.2.15</version>
          </dependency>
      
          <dependency>
              <groupId>org.apache.dubbo</groupId>
              <artifactId>dubbo-registry-nacos</artifactId>
              <version>3.2.15</version>
          </dependency>
      
          <dependency>
              <groupId>com.alibaba.nacos</groupId>
              <artifactId>nacos-client</artifactId>
              <version>1.1.1</version>
          </dependency>
      </dependencies>            
  2. 開發Dubbo服務消費者。

    Dubbo中服務都是以介面形式提供。

    1. src/main/java路徑下建立Package,命名為com.alibaba.edas

    2. com.alibaba.edas下建立一個介面(interface)IHelloService,裡麵包含一個SayHello方法。

      說明

      通常是在一個單獨的模組中定義介面,服務提供者和服務消費者都通過Maven依賴來引用此模組。本文為了簡便,服務提供者和服務消費者分別建立兩個完全一模一樣的介面,實際使用中不推薦這樣使用。

        package com.alibaba.edas;
      
        public interface IHelloService {
            String sayHello(String str);
        }                                
  3. 配置Dubbo服務。

    1. src/main/resources路徑下建立consumer.xml檔案並開啟。

    2. consumer.xml中,添加Spring相關的XML Namespace(xmlns)和XML Schema Instance(xmlns:xsi),以及Dubbo相關的Namespace(xmlns:dubbo)和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. consumer.xml中添加如下配置,訂閱Dubbo服務。

        <dubbo:application name="demo-consumer"/>
      
        <dubbo:registry address="nacos://127.0.0.1:8848"/>
      
        <dubbo:reference id="helloService" interface="com.alibaba.edas.IHelloService"/>
  4. 啟動並驗證服務。

    1. com.alibaba.edas下建立類Consumer,並按下面的代碼在Consumer的main函數中載入Spring Context,訂閱並消費Dubbo服務。

          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. 執行Consumer的main函數,啟動服務。

  5. 驗證結果。

    啟動後,可以看到控制台不斷地輸出hello world,表明服務消費成功。

    登入Nacos控制台http://127.0.0.1:8848,在左側導覽列中單擊服務列表,再在服務列表頁面選擇調用者列表

    可以看到包含了com.alibaba.edas.IHelloService,且可以查看該服務的服務分組調用者IP

步驟三:部署到SAE

  1. 分別在Provider和Consumer的pom.xml檔案中添加如下配置,配置完成後執行mvn clean package將本地程式編譯為可執行檔JAR包。

    • 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.sae.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.sae.Consumer</mainClass>
                         </configuration>
                     </execution>
                 </executions>
             </plugin>
       </plugins>
      </build>                        
  2. 將編譯好的Provider和Consumer應用程式套件部署至SAE。具體操作,請參見使用JAR包部署Java應用