Spring で Azure Event Grid を使用する
この記事では、Azure Event Grid を使用してトピックにイベントを送信し、Spring Boot アプリケーションで受信する イベント ハンドラー として Service Bus キューを使用する方法について説明します。
Azure Event Grid サービスは、MQTT プロトコルと HTTP プロトコルを使用して柔軟なメッセージ消費パターンを提供する、高度にスケーラブルでフル マネージドの Pub Sub メッセージ配布サービスです。
前提 条件
Azure サブスクリプション - 無料アカウントを作成します。
Java Development Kit (JDK) バージョン 8 以降。
Event Grid トピック インスタンス。 お持ちでない場合は、「Azure Event Gridでカスタム トピックまたはドメインを作成する」を参照してください。
Service Bus Queue インスタンス。 お持ちでない場合は、「Azure portalでキュー
作成する」を参照してください。 Spring Boot アプリケーション。 まだない場合は、Spring Initializrを使用して Maven プロジェクトを作成します。 Maven Project
選択し、Java バージョン 8 以降を選択してください。
カスタム トピックをサブスクライブする
Service Bus キューにイベントを送信するように Event Grid に指示するイベント サブスクリプションを作成するには、次の手順に従います。
- Azure portal で、Event Grid トピック インスタンスに移動します。
- ツール バー イベント サブスクリプション を選択します。
- [イベント サブスクリプションの作成 ] ページで、イベント サブスクリプションの 名 値を入力します。
- [エンドポイントの種類] で、[Service Bus Queue] を選択します。
- エンドポイント を選択し、前に作成した Service Bus キュー インスタンスを選択します。
Azure Event Grid でイベントを送信し、Azure Service Bus キューで受信する
Azure Event Grid リソースでは、Spring Cloud Azure Event Grid を使用してイベントを送信できます。 イベント ハンドラーとして Azure Service Bus キュー リソースを使用すると、Spring Cloud Azure Stream Binder for Service Bus を使用してイベントを受信できます。
Spring Cloud Azure Event Grid Starter モジュールと Spring Cloud Azure Stream Binder Service Bus モジュールをインストールするには、pom.xml ファイルに次の依存関係を追加します。
Spring Cloud Azure 部品表 (BOM):
<dependencyManagement> <dependencies> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-dependencies</artifactId> <version>5.19.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
手記
Spring Boot 2.x を使用している場合は、必ず
spring-cloud-azure-dependencies
バージョンを4.19.0
に設定してください。 この部品表 (BOM) は、pom.xml ファイルの<dependencyManagement>
セクションで構成する必要があります。 これにより、すべての Spring Cloud Azure 依存関係で同じバージョンが使用されるようになります。 この BOM に使用されるバージョンの詳細については、「どのバージョンの Spring Cloud Azure を使用する必要」を参照してください。Spring Cloud Azure Event Grid Starter アーティファクト:
<dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-eventgrid</artifactId> </dependency>
Spring Cloud Azure Stream Binder Service Bus アーティファクト:
<dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-stream-binder-servicebus</artifactId> </dependency>
アプリケーションをコーディングする
Event Grid を使用してイベントを送信し、Service Bus キューを使用して受信するようにアプリケーションを構成するには、次の手順を使用します。
次の例に示すように、application.yaml 構成ファイルで Azure Event Grid と Service Bus の資格情報を構成します。
spring: cloud: azure: eventgrid: endpoint: ${AZURE_EVENTGRID_ENDPOINT} key: ${AZURE_EVENTGRID_KEY} servicebus: connection-string: ${AZURE_SERVICEBUS_CONNECTION_STRING} function: definition: consume stream: bindings: consume-in-0: destination: ${AZURE_SERVICEBUS_QUEUE_NAME} servicebus: bindings: consume-in-0: consumer: auto-complete: false
スタートアップ クラス ファイルを編集して、次の内容を表示します。 このコードは Completion を生成します。
import com.azure.core.util.BinaryData; import com.azure.messaging.eventgrid.EventGridEvent; import com.azure.messaging.eventgrid.EventGridPublisherClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.messaging.Message; import java.util.List; import java.util.function.Consumer; @SpringBootApplication public class EventGridSampleApplication implements CommandLineRunner { private static final Logger LOGGER = LoggerFactory.getLogger(EventGridSampleApplication.class); @Autowired EventGridPublisherClient<EventGridEvent> client; public static void main(String[] args) { SpringApplication.run(EventGridSampleApplication.class, args); } @Bean public Consumer<Message<String>> consume() { return message -> { List<EventGridEvent> eventData = EventGridEvent.fromString(message.getPayload()); eventData.forEach(event -> { LOGGER.info("New event received: '{}'", event.getData()); }); }; } @Override public void run(String... args) throws Exception { String str = "FirstName: John, LastName: James"; EventGridEvent event = new EventGridEvent("A user is created", "User.Created.Text", BinaryData.fromObject(str), "0.1"); client.sendEvent(event); LOGGER.info("New event published: '{}'", event.getData()); } }
アプリケーションを起動します。 起動後、アプリケーションは次の例のようなログを生成します。
New event published: '"FirstName: John, LastName: James"' ... New event received: '"FirstName: John, LastName: James"'
Azure Spring Apps へのデプロイ
Spring Boot アプリケーションがローカルで実行されたので、運用環境に移行します。 Azure Spring Apps を
次の手順
Spring と Azure の詳細については、Spring on Azure ドキュメント センターに進んでください。
Spring 開発者向けの Azure Spring Cloud Azure Event Grid のサンプル