このトピックでは、XML 構成を使用してサンプル Dubbo マイクロサービスアプリケーションをローカルで開発し、そのアプリケーションを Serverless App Engine (SAE) にデプロイする方法について説明します。このサンプルアプリケーションには、サービスプロバイダー (Provider) とサービスコンシューマー (Consumer) が含まれています。
準備
Maven をダウンロードし、環境変数を設定します。
Nacos Server を起動します。
Nacos Server パッケージをダウンロードして解凍します。
nacos/bin ディレクトリに移動し、Nacos Server を起動します。
Linux、Unix、または macOS:
sudo sh startup.sh -m standaloneコマンドを実行します。Windows:
startup.cmd -m standaloneコマンドを実行します。
説明standaloneは、startup.cmd ファイルがクラスターモードではなくスタンドアロンモードで実行されることを示します。デフォルトでは、startup.cmd ファイルはクラスターモードで起動されます。Windows システムで startup.cmd ファイルをダブルクリックして実行すると、起動に失敗します。この場合、startup.cmd ファイルでMODE="standalone"を構成する必要があります。詳細については、「Nacos のクイックスタート」をご参照ください。
バージョンガイド
Dubbo 2.7.x と Dubbo 3.0.x のライフサイクルは 2023 年 3 月に終了しました。アプリケーションの開発には、より新しいバージョンを使用することをお勧めします。このトピックでは、Dubbo 3.2.15 を例として、SAE で Dubbo アプリケーションをホストする方法について説明します。
ステップ 1: プロバイダーの作成
ローカル環境でプロバイダーアプリケーションプロジェクトを作成し、依存関係を追加し、サービス登録と検出を構成し、Nacos をサービスレジストリとして指定します。
Maven プロジェクトを作成し、依存関係を追加します。
IntelliJ IDEA や Eclipse などの IDE を使用して Maven プロジェクトを作成します。
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>
Dubbo サービスプロバイダーを開発します。
すべての Dubbo サービスはインターフェイスを介して提供されます。
src/main/java パスに、パッケージ
com.alibaba.edasを作成します。com.alibaba.edasに、sayHelloメソッドを含むインターフェイスIHelloServiceを作成します。package com.alibaba.edas; public interface IHelloService { String sayHello(String str); }com.alibaba.edasに、このインターフェイスを実装するクラスIHelloServiceImplを作成します。package com.alibaba.edas; public class IHelloServiceImpl implements IHelloService { public String sayHello(String str) { return "hello " + str; } }
Dubbo サービスを構成します。
src/main/resources パスで、
provider.xmlファイルを作成して開きます。provider.xmlファイルに、Spring の XML 名前空間 (xmlns) と XML スキーマインスタンス (xmlns:xsi)、および Dubbo の名前空間 (xmlns:dubbo) とスキーマインスタンス (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">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"/>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 のポートであり、変更できません。
サービスを開始します。
com.alibaba.edasパッケージに、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(); } }Provider の main 関数を実行してサービスを開始します。
http://127.0.0.1:8848の Nacos コンソールにログオンします。左側のナビゲーションウィンドウで、[サービスリスト] をクリックしてプロバイダーリストを表示します。サービスプロバイダーリストには
com.alibaba.edas.IHelloServiceが含まれています。サービスの [サービスグループ] と [プロバイダー IP] をクエリすることもできます。
ステップ 2: コンシューマーの作成
ローカル環境でコンシューマーアプリケーションプロジェクトを作成し、依存関係を追加し、サービスサブスクリプションを構成します。
Maven プロジェクトを作成し、依存関係を追加します。
IntelliJ IDEA や Eclipse などの IDE を使用して Maven プロジェクトを作成します。
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>
Dubbo サービスコンシューマーを開発します。
すべての Dubbo サービスはインターフェイスを介して提供されます。
src/main/java パスに、
com.alibaba.edasという名前のパッケージを作成します。com.alibaba.edasパッケージに、sayHelloメソッドを含むインターフェイスIHelloServiceを作成します。説明ほとんどの場合、インターフェイスは独立したモジュールで定義されます。プロバイダーとコンシューマーは、Maven の依存関係を使用して同じモジュールを参照します。このトピックでは、プロバイダーとコンシューマー用に 2 つの同一のインターフェイスが作成されます。ただし、この方法は本番シナリオでは推奨されません。
package com.alibaba.edas; public interface IHelloService { String sayHello(String str); }
Dubbo サービスを構成します。
src/main/resources パスで、
consumer.xmlファイルを作成して開きます。consumer.xmlファイルに、Spring の XML 名前空間 (xmlns) と XML スキーマインスタンス (xmlns:xsi)、および Dubbo の名前空間 (xmlns:dubbo) とスキーマインスタンス (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">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"/>
サービスを開始して検証します。
com.alibaba.edasパッケージに、`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(); } } } }Consumer の main 関数を実行してサービスを開始します。
結果を検証します。
起動後、コンソールは継続的に
hello worldを出力します。これは、サービスが正常にコンシュームされたことを示します。http://127.0.0.1:8848の Nacos コンソールにログオンし、左側のナビゲーションウィンドウで [サービス] をクリックします。[サービス] ページで、[呼び出し元] を選択します。サービス
com.alibaba.edas.IHelloServiceが表示されます。その [サービスグループ] と [サブスクライバー IP] を表示することもできます。
ステップ 3: SAE へのデプロイ
プロバイダーとコンシューマーの両方の pom.xml ファイルに次の構成を追加します。次に、mvn clean package コマンドを実行して、ローカルプロジェクトを実行可能な JAR パッケージにコンパイルします。
プロバイダー
<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>コンシューマー
<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>
コンパイルされたプロバイダーとコンシューマーのアプリケーションパッケージを SAE にデプロイします。詳細については、「Java アプリケーションをデプロイする」をご参照ください。