Skip to Content
イベント駆動マイクロサービスを構築する
book

イベント駆動マイクロサービスを構築する

by Adam Bellemare
March 2025
Beginner to intermediate
324 pages
4h 49m
Japanese
O'Reilly Media, Inc.
Content preview from イベント駆動マイクロサービスを構築する

第5章. イベント駆動処理の基本

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

ほとんどのイベント駆動マイクロサービスは、最低限、同じ3つのステップを踏む:

  1. 入力イベントストリームからイベントを消費する。

  2. そのイベントを処理する。

  3. 必要な出力イベントをプロデューサする。

また、同期リクエスト/レスポンス対話から入力イベントを生成するイベント駆動のマイクロサービスもある。この章では、イベントストリームからイベントを生成するマイクロサービスのみを取り上げる。

ストリームソースイベントドリブンマイクロサービスでは、マイクロサービスインスタンスはプロデューサークライアントとコンシューマークライアントを作成し、必要であればコンシューマグループに登録する。マイクロサービスはコンシューマークライアントに新しいイベントをポーリングするループを開始し、入ってきたイベントを処理し、必要な出力イベントを発行する。このワークフローを以下の擬似コードに示す。(もちろん、言語、ストリーム処理フレームワーク、イベントブローカーの選択、その他の技術的要因によって実装は異なるだろう)

Consumer consumerClient = new consumerClient(consumerGroupName, ...);
Producer producerClient = new producerClient(...);

while(true) {
    InputEvent event = consumerClient.pollOneEvent(inputEventStream);
    OutputEvent output = processEvent(event);
    producerClient.produceEventToStream(outputEventStream, output);

    //At-least-once processing.
    consumerClient.commitOffsets();
}

特に注目すべきは、processEvent 関数である。主にビジネスロジックの適用と、イベントがある場合はどのイベントを発行するかである。この処理関数は、マイクロサービスの処理トポロジーへのエントリポイントとして考えるのがベストである。ここから、データ駆動パターンが境界づけられたコンテキストのビジネスニーズに合わせてデータを変換・処理する。

ステートレス・トポロジーを構成する

マイクロサービスのトポロジーを構築するには、コンシューマ・インプットに到着したイベントにレスポンスしてコードが実行されるように、イベント・ドリブンで考える必要がある。マイクロサービスのトポロジーは、基本的にイベントに対して実行する演算子のシーケンスである。マイクロサービスに必要なビジネスロジックを実行するために必要なフィルタリング、ルーター、変換、マテリアライゼーション、集約、その他の関数を選択する必要がある。関数型プログラミングやビッグデータのマップ・リダクション・スタイルのフレームワークに慣れ親しんでいる人たちは、ここでかなりくつろげるかもしれない。他の人にとっては、これは少し新しい概念かもしれない。

図5-1のトポロジーを考えてみよう。イベントは一度に1つずつコンシューマされ、ステージ1と2の変換に従って処理される。

図5-1. 単純なイベント処理トポロジー

キーがAC のイベントは両方ともトポロジー全体を横断する。それらは両方とも ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

マイクロサービスの成功を可能にする

マイクロサービスの成功を可能にする

Sarah Wells
マイクロサービスアーキテクチャ 第2版

マイクロサービスアーキテクチャ 第2版

Sam Newman, 佐藤 直生, 木下 哲也

Publisher Resources

ISBN: 9798341625228