Spring Boot は、マイクロサービスアプリケーションの構成とデプロイを簡略化します。ビジネス要件に基づいてレジストリを選択し、構成を管理できます。このトピックでは、Spring Boot アノテーションを使用して、Nacos ベースのデモ Dubbo マイクロサービスアプリケーションを開発する方法について説明します。Spring Boot を使用して開発された Dubbo アプリケーションが既にある場合は、このトピックをスキップして、アプリケーションを Enterprise Distributed Application Service (EDAS) に直接デプロイできます。
前提条件
Spring Boot を使用して Dubbo マイクロサービスアプリケーションを開発する前に、次の操作が完了していることを確認してください。
Maven をダウンロードし、環境変数を構成します。
最新バージョンの Nacos Server をダウンロードします。
Nacos Server を起動します。
ダウンロードした Nacos Server パッケージを解凍します。
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 パスに格納されています。
サービスプロバイダーを作成する
spring-boot-dubbo-providerという名前の Maven プロジェクトを作成します。必要な依存関係を
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>Dubbo サービスプロバイダーを開発します。
すべての Dubbo サービスは、インターフェースを使用して提供されます。
src/main/javaパスに、com.alibaba.edas.bootという名前のパッケージを作成します。com.alibaba.edas.bootパッケージに、SayHelloという名前のメソッドを含むIHelloServiceという名前のインターフェースを作成します。package com.alibaba.edas.boot; public interface IHelloService { String sayHello(String str); }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 です。
Dubbo サービスを構成します。
src/main/resourcesパスに、application.propertiesファイルまたはapplication.yamlファイルを作成し、ファイルを開きます。次の構成項目を
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.Serviceとcom.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 アドレスに変更します。
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); } }http://127.0.0.1:8848から Nacos コンソールにログインします。 左側のナビゲーションペインで、[サービス] をクリックして、サービスプロバイダーのリストを表示します。リストに
com.alibaba.edas.boot.IHelloServiceが表示されます。 サービスのサービスグループとプロバイダーの IP アドレスを照会できます。
サービスコンシューマーを作成する
spring-boot-dubbo-consumerという名前の Maven プロジェクトを作成します。必要な依存関係を
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 月に終了しました。 新しいバージョンを使用してアプリケーションを開発することをお勧めします。
Dubbo サービスコンシューマーを開発します。
src/main/javaパスに、com.alibaba.edas.bootという名前のパッケージを作成します。com.alibaba.edas.bootパッケージに、SayHelloという名前のメソッドを含むIHelloServiceという名前のインターフェースを作成します。package com.alibaba.edas.boot; public interface IHelloService { String sayHello(String str); }
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 です。
次の構成項目を
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 アドレスに変更します。
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); } }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 コンソールでアプリケーションをデプロイする場合は、アプリケーションをデプロイする前に、オンプレミスのプログラムで次の手順を実行します。
パッケージングプラグインの次の構成を
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>
mvn clean package コマンドを実行して、オンプレミスのプログラムを JAR パッケージにビルドします。
追加情報
edas-dubbo-extension を使用して Dubbo アプリケーションを EDAS にデプロイする場合、Dubbo サービスガバナンスなどの EDAS の特定の機能を使用することはできません。 そのため、edas-dubbo-extension を使用しないことをお勧めします。 アプリケーションを Nacos に移行することをお勧めします。