すべてのプロダクト
Search
ドキュメントセンター

Enterprise Distributed Application Service:Spring Boot を使用して Dubbo マイクロサービスアプリケーションを開発する

最終更新日:Jan 07, 2025

Spring Boot は、マイクロサービスアプリケーションの構成とデプロイを簡略化します。ビジネス要件に基づいてレジストリを選択し、構成を管理できます。このトピックでは、Spring Boot アノテーションを使用して、Nacos ベースのデモ Dubbo マイクロサービスアプリケーションを開発する方法について説明します。Spring Boot を使用して開発された Dubbo アプリケーションが既にある場合は、このトピックをスキップして、アプリケーションを Enterprise Distributed Application Service (EDAS) に直接デプロイできます。

前提条件

Spring Boot を使用して Dubbo マイクロサービスアプリケーションを開発する前に、次の操作が完了していることを確認してください。

  • Maven をダウンロードし、環境変数を構成します。

  • 最新バージョンの Nacos Server をダウンロードします。

  • Nacos Server を起動します。

    1. ダウンロードした Nacos Server パッケージを解凍します。

    2. nacos/bin パスに移動し、次のいずれかの方法を使用して Nacos Server を起動します。 使用する方法は、OS によって異なります。

      • Linux、UNIX、または macOS: sh startup.sh -m standalone コマンドを実行します。

      • Windows: startup.cmd ファイルで、set MODE="standalone" コマンドを使用して MODE パラメーターを構成します。 その後、ファイルを実行します。

  • (オプション): オンプレミスアプリケーションとクラウドアプリケーション間の相互接続を実装します。

    Cloud Native App Initializer を使用して Spring Boot プロジェクトを作成できます。詳細については、「Cloud Native App Initializer」をご参照ください。

デモプロジェクト

このトピックで説明されている手順を実行して、プロジェクトをビルドできます。 また、このトピックで使用されている デモプロジェクト をダウンロードするか、Git で git clone https://github.com/aliyun/alibabacloud-microservice-demo.git コマンドを実行してプロジェクトを複製することもできます。

複製されたプロジェクトには、複数のデモプロジェクトが含まれています。 このトピックで使用されるデモプロジェクトは、alibabacloud-microservice-demo/microservice-doc-demo/dubbo-samples-spring-boot パスに格納されています。

サービスプロバイダーを作成する

  1. spring-boot-dubbo-provider という名前の Maven プロジェクトを作成します。

  2. 必要な依存関係を pom.xml ファイルに追加します。

    次のサンプルコードは、依存関係を追加する方法の例を示しています。 この例では、Spring Boot 2.0.6.RELEASE が使用されています。

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>                   
  3. Dubbo サービスプロバイダーを開発します。

    すべての Dubbo サービスは、インターフェースを使用して提供されます。

    1. src/main/java パスに、com.alibaba.edas.boot という名前のパッケージを作成します。

    2. com.alibaba.edas.boot パッケージに、SayHello という名前のメソッドを含む IHelloService という名前のインターフェースを作成します。

      package com.alibaba.edas.boot;
      public interface IHelloService {
      String sayHello(String str);
      }                                
    3. com.alibaba.edas.boot パッケージに、IHelloServiceImpl という名前のクラスを作成して、インターフェースを実装します。

      package com.alibaba.edas.boot;
      import com.alibaba.dubbo.config.annotation.Service;
      @Service
      public class IHelloServiceImpl implements IHelloService {
      public String sayHello(String name) {
        return "Hello, " + name + " (from Dubbo with Spring Boot)";
       }
      }                                
      説明

      サンプルコードでは、Service アノテーションは、Dubbo によって提供されるアノテーションクラスです。 アノテーションクラスの完全な名前は com.alibaba.dubbo.config.annotation.Service です。

  4. Dubbo サービスを構成します。

    1. src/main/resources パスに、application.properties ファイルまたは application.yaml ファイルを作成し、ファイルを開きます。

    2. 次の構成項目を application.properties ファイルまたは application.yaml ファイルに追加します。

      # Dubbo コンポーネントをスキャンする基本パッケージ (例: @Service, @Reference) // Base packages to scan Dubbo Components (e.g @Service , @Reference)
      dubbo.scan.basePackages=com.alibaba.edas.boot
      dubbo.application.name=dubbo-provider-demo
      dubbo.registry.address=nacos://127.0.0.1:8848                                
      説明
      • 上記のコードの 3 つの構成項目にはデフォルト値がないため、値を指定する必要があります。

      • dubbo.scan.basePackages の値は、コードに com.alibaba.dubbo.config.annotation.Servicecom.alibaba.dubbo.config.annotation.Reference が含まれるパッケージです。 複数のパッケージはコンマ (,) で区切ります。

      • dubbo.registry.address の値は nacos:// で始まり、その後に Nacos Server の IP アドレスとポート番号が続きます。 サンプルコードの IP アドレスは、オンプレミスのアドレスです。Enterprise Distributed Application Service (EDAS) 管理レジストリを使用する場合、EDAS はサンプルの IP アドレスをレジストリの IP アドレスに自動的に変更します。 自己管理 Nacos レジストリを使用する場合は、サンプルコードの dubbo.registry.address の値をレジストリの IP アドレスに変更します。

  5. Spring Boot の DubboProvider エントリクラスを開発して起動します。

        package com.alibaba.edas.boot;
    
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
    
        @SpringBootApplication
        public class DubboProvider {
    
            public static void main(String[] args) {
                SpringApplication.run(DubboProvider.class, args);
            }
    
        }                        
  6. http://127.0.0.1:8848 から Nacos コンソールにログインします。 左側のナビゲーションペインで、[サービス] をクリックして、サービスプロバイダーのリストを表示します。

    リストに com.alibaba.edas.boot.IHelloService が表示されます。 サービスのサービスグループとプロバイダーの IP アドレスを照会できます。

サービスコンシューマーを作成する

  1. spring-boot-dubbo-consumer という名前の Maven プロジェクトを作成します。

  2. 必要な依存関係を pom.xml ファイルに追加します。

    次のサンプルコードは、依存関係を追加する方法の例を示しています。 この例では、Spring Boot 2.0.6.RELEASE が使用されています。

        <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.1.1</version>
        </dependency>
    
    </dependencies>                        

    Spring Boot 1.x を使用する場合は、Spring Boot 1.5.x と com.alibaba.boot:dubbo-spring-boot-starter 0.1.0 を使用してください。

    説明

    Spring Boot 1.x のライフサイクルは 2019 年 8 月に終了しました。 新しいバージョンを使用してアプリケーションを開発することをお勧めします。

  3. Dubbo サービスコンシューマーを開発します。

    1. src/main/java パスに、com.alibaba.edas.boot という名前のパッケージを作成します。

    2. com.alibaba.edas.boot パッケージに、SayHello という名前のメソッドを含む IHelloService という名前のインターフェースを作成します。

      package com.alibaba.edas.boot;
      
      public interface IHelloService {
       String sayHello(String str);
      }                                
  4. Dubbo サービスコールのコードを開発します。

    次のサンプルコードは、コントローラーでリモート Dubbo サービスを呼び出す方法の例を示しています。

    package com.alibaba.edas.boot;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
        public class DemoConsumerController {
    
            @Reference
            private IHelloService demoService;
    
            @RequestMapping("/sayHello/{name}")
            public String sayHello(@PathVariable String name) {
                return demoService.sayHello(name);
            }
        }                        
    説明

    Reference アノテーションは com.alibaba.dubbo.config.annotation.Reference です。

  5. 次の構成項目を application.properties or application.yaml ファイルに追加します。

    dubbo.application.name=dubbo-consumer-demo
    dubbo.registry.address=nacos://127.0.0.1:8848                        
    説明
    • 上記のコードの 2 つの構成項目にはデフォルト値がないため、値を指定する必要があります。

    • dubbo.registry.address の値は nacos:// で始まり、その後に Nacos Server の IP アドレスとポート番号が続きます。 サンプルコードの IP アドレスは、オンプレミスのアドレスです。Nacos Server が別のマシンにデプロイされている場合は、サンプルの IP アドレスをそのマシンの IP アドレスに変更します。

  6. Spring Boot の DubboConsumer エントリクラスを開発して起動します。

    package com.alibaba.edas.boot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class DubboConsumer {
    
        public static void main(String[] args) {
            SpringApplication.run(DubboConsumer.class, args);
        }
    
    }                        
  7. http://127.0.0.1:8848 から Nacos コンソールにログインします。 左側のナビゲーションペインで、[サービス] をクリックします。 [サービス] ページで、呼び出し元サービスのリストを表示します。

    リストに com.alibaba.edas.boot.IHelloService が表示されます。 サービスのサービスグループと呼び出し元の IP アドレスを表示できます。

結果を確認する

`curl http://localhost:8080/sayHello/EDAS`

`Hello, EDAS (from Dubbo with Spring Boot)`            

アプリケーションを EDAS にデプロイする

説明

EDAS に統合されている Application Configuration Management (ACM) は、Nacos の公式商用バージョンを提供します。 アプリケーションを EDAS にデプロイすると、EDAS はオンプレミス Nacos Server の IP アドレスとポート番号 (127.0.0.1:8848) を自動的に変更します。

アプリケーションを EDAS にデプロイするには、ビジネス要件に基づいてクラスタタイプとデプロイ方法を選択できます。 Elastic Compute Service (ECS) クラスタと Container Service for Kubernetes (ACK) クラスタの 2 種類のクラスタがサポートされています。 ツールまたは EDAS コンソールを使用したデプロイ方法がサポートされています。 詳細については、次のトピックを参照してください。

EDAS コンソールでアプリケーションをデプロイする場合は、アプリケーションをデプロイする前に、オンプレミスのプログラムで次の手順を実行します。

  1. パッケージングプラグインの次の構成を pom.xml ファイルに追加します。

    • プロバイダー

      <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.boot.DubboProvider</mainClass>
                         </configuration>
                     </execution>
                 </executions>
             </plugin>
       </plugins>
      </build>                                
    • コンシューマー

      <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.boot.DubboConsumer</mainClass>
                         </configuration>
                     </execution>
                 </executions>
             </plugin>
       </plugins>
      </build>                                
  2. mvn clean package コマンドを実行して、オンプレミスのプログラムを JAR パッケージにビルドします。

追加情報

edas-dubbo-extension を使用して Dubbo アプリケーションを EDAS にデプロイする場合、Dubbo サービスガバナンスなどの EDAS の特定の機能を使用することはできません。 そのため、edas-dubbo-extension を使用しないことをお勧めします。 アプリケーションを Nacos に移行することをお勧めします。