このドキュメントでは、Cloud Run、Pub/Sub、Workflows、Datastore モードの Firestore(Datastore)を使用してサンプル アプリケーションをデプロイする方法について説明します。これは、マイクロサービス ベースのアプリケーションでトランザクション ワークフローを実装するアプリケーション デベロッパー向けです。
このドキュメントは、以下で構成されるシリーズの一部です。
- Google Cloud のマイクロサービス アーキテクチャのトランザクション ワークフロー: このチュートリアルでデプロイするサンプルのトランザクション ワークフロー アプリケーションのアーキテクチャの概要。
- Google Cloud のマイクロサービス アーキテクチャにトランザクション ワークフロー アプリケーションのサンプルをデプロイする(このドキュメント)。
このチュートリアルでデプロイするこのサンプル アプリケーションは、次の 2 つのアーキテクチャ パターンでマイクロサービスを実装します。
- コレオグラフィー ベースの Saga
- 同期オーケストレーション
アプリケーションにはウェブ クライアントが含まれています。Google Cloud CLI とウェブ クライアントの両方で、これらのパターンの両方をテストできます。
目標
- コレオグラフィー ベースの Saga アーキテクチャ用にサーバー側コンポーネントをデプロイする
- 同期オーケストレーション アーキテクチャ用にサーバー側コンポーネントをデプロイする
curl
コマンドを使用してサーバー側コンポーネントをテストする- ウェブ アプリケーションをデプロイし、それを通じてワークフローを実行する
費用
このドキュメントでは、Google Cloud の次の課金対象のコンポーネントを使用します。
料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。
このドキュメントに記載されているタスクの完了後、作成したリソースを削除すると、それ以上の請求は発生しません。詳細については、クリーンアップをご覧ください。
始める前に
-
Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。
-
Cloud Services, Cloud Run, Workflows, Cloud Build, and Cloud Scheduler API を有効にします。
ソースコードのクローンを作成する
このセクションではプロジェクト ID を設定し、Cloud Shell でソースコードのクローンを作成します。
Cloud Shell でプロジェクト ID を設定します。
PROJECT_ID=PROJECT_ID
PROJECT_ID
は、前の手順で作成した Google Cloud プロジェクトのプロジェクト ID に置き換えます。プロジェクト ID を設定します。
gcloud config set project $PROJECT_ID
チュートリアル リポジトリのクローンを作成します。
cd $HOME git clone https://github.com/GoogleCloudPlatform/transactional-microservice-examples
コレオグラフィー ベースの SAP アーキテクチャ用にサーバー側コンポーネントをデプロイする
このセクションでは、サンプル アプリケーションのサーバー側コンポーネントをデプロイします。このアプリケーションは、Cloud Run でのマイクロサービス、Cloud Scheduler でのイベント公開のスケジュール、Pub/Sub トピックなどのコレオグラフィー ベースの Saga アーキテクチャを実装します。
コンテナ イメージの構築とデプロイ
Cloud Shell で、
Order
サービスのコンテナ イメージをorder-async
という名前でビルドし、Cloud Run にデプロイします。cd $HOME/transactional-microservice-examples/services/order-async gcloud builds submit --tag gcr.io/$PROJECT_ID/order-service-async gcloud run deploy order-service-async \ --image gcr.io/$PROJECT_ID/order-service-async \ --platform=managed --region=us-central1 \ --no-allow-unauthenticated
customer-async
という名前のCustomer
サービスのコンテナ イメージをビルドし、Cloud Run にデプロイします。cd $HOME/transactional-microservice-examples/services/customer-async gcloud builds submit --tag gcr.io/$PROJECT_ID/customer-service-async gcloud run deploy customer-service-async \ --image gcr.io/$PROJECT_ID/customer-service-async \ --platform=managed --region=us-central1 \ --no-allow-unauthenticated
event-publisher
サービスのコンテナ イメージをビルドし、Cloud Run にデプロイします。cd $HOME/transactional-microservice-examples/services/event-publisher gcloud builds submit --tag gcr.io/$PROJECT_ID/event-publisher gcloud run deploy event-publisher \ --image gcr.io/$PROJECT_ID/event-publisher \ --platform=managed --region=us-central1 \ --no-allow-unauthenticated \ --set-env-vars "PROJECT_ID=$PROJECT_ID"
Datastore のインデックスを作成する
このセクションでは、Datastore のインデックスを作成します。このインデックスは、公開する必要があるイベントを選択するために、event-publisher
サービスによって使用されます。
Google Cloud コンソールの [Datastore] メニューで、[Datastore モード] を選択します。
[データの保存場所を選択] をクリックし、ロケーションとして
us-east1
を選択します。次に、[データベースを作成] をクリックします。Datastore のインデックスを作成する:
cd $HOME/transactional-microservice-examples/services/event-publisher gcloud datastore indexes create index.yaml --quiet
Google Cloud コンソールの [Datastore] メニューで [インデックス] を選択し、[インデックス登録] から [サービス提供] にインデックスのステータスが変化するまで待ちます。この処理には数分かかる場合があります。
Cloud Run でマイクロサービスを呼び出すサービス アカウントを作成する
Cloud Shell で
cloud-run-invoker
サービス アカウントを作成します。SERVICE_ACCOUNT_NAME="cloud-run-invoker" SERVICE_ACCOUNT_EMAIL=${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com gcloud iam service-accounts create $SERVICE_ACCOUNT_NAME \ --display-name "Cloud Run Invoker"
このチュートリアルの後半では、このサービス アカウントを使用して、Cloud Run で実行されるマイクロサービスの REST API を呼び出します。
イベント パブリッシャー サービスを呼び出すスケジュールを定義する
このセクションでは、event-publisher
という名前のイベント パブリッシャーを 1 分間隔で呼び出すスケジュールを定義します。
Cloud Shell で、
run.invoker
ロールをevent publisher
サービスに割り当てます。SERVICE_NAME="event-publisher" gcloud run services add-iam-policy-binding $SERVICE_NAME \ --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \ --role=roles/run.invoker \ --platform=managed --region=us-central1
cloud-run-invoker
サービス アカウントを使用して、event-publisher
というEvent publisher
サービスを 1 分間隔で呼び出すスケジュールを定義します。SERVICE_NAME="event-publisher" SERVICE_URL=$(gcloud run services list --platform managed \ --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}") SERVICE_URL="${SERVICE_URL}/api/v1/event/publish" gcloud scheduler jobs create http event-publisher-scheduler \ --schedule='* * * * *' \ --http-method=GET \ --uri=$SERVICE_URL \ --oidc-service-account-email=$SERVICE_ACCOUNT_EMAIL \ --oidc-token-audience=$SERVICE_URL \ --location=us-central1
Pub/Sub トピックを作成する
Cloud Shell で、次の Pub/Sub トピックを作成します。
gcloud pubsub topics create order-service-event gcloud pubsub topics create customer-service-event
order-service-event
トピックは、Order
サービスと event-publisher
サービスが、Order
サービスからイベントを公開するために使用します。customer-service-event
トピックは、Customer
サービスからイベントを公開するために使用されます。
イベント通知をマイクロサービスに送信するためのトピックを定義する
このセクションでは、Pub/Sub トピックのメッセージをマイクロサービスに配信する push-subscription
トピックを定義します。
Cloud Shell で、プロジェクトの Pub/Sub サービス アカウントに
iam.serviceAccountTokenCreator
ロールを割り当てます。PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format "value(projectNumber)") gcloud projects add-iam-policy-binding $PROJECT_ID \ --member=serviceAccount:service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com \ --role=roles/iam.serviceAccountTokenCreator
このコマンドにより、サービス アカウントは Cloud Run でマイクロサービスを呼び出すためのアクセス トークンを作成できます。
run.invoker
ロールをcustomer-service-async
サービスに割り当てます。SERVICE_NAME="customer-service-async" gcloud run services add-iam-policy-binding $SERVICE_NAME \ --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \ --role=roles/run.invoker \ --platform=managed --region=us-central1
push-subscription
トピックを作成します。SERVICE_URL=$(gcloud run services list --platform managed \ --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}") SERVICE_URL="${SERVICE_URL}/api/v1/customer/pubsub" gcloud pubsub subscriptions create push-order-to-customer \ --topic order-service-event \ --push-endpoint=$SERVICE_URL \ --push-auth-service-account=$SERVICE_ACCOUNT_EMAIL
このトピックは、
cloud-run-invoker
サービス アカウントを使用して、order-service-event
トピックのメッセージをCustomer
サービスに配信します。run.invoker
ロールをorder-service-async
サービスに割り当てます。SERVICE_NAME="order-service-async" gcloud run services add-iam-policy-binding $SERVICE_NAME \ --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \ --role=roles/run.invoker \ --platform=managed --region=us-central1
push-subscription
トピックを作成します。SERVICE_URL=$(gcloud run services list --platform managed \ --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}") SERVICE_URL="${SERVICE_URL}/api/v1/order/pubsub" gcloud pubsub subscriptions create push-customer-to-order \ --topic customer-service-event \ --push-endpoint=$SERVICE_URL \ --push-auth-service-account=$SERVICE_ACCOUNT_EMAIL
push-subscription
トピックは、cloud-run-invoker
サービス アカウントを使用して、customer-service-event
トピックのメッセージをOrder
サービスに配信します。
同期オーケストレーション アーキテクチャ用にサーバー側コンポーネントをデプロイする
このセクションでは、サンプル アプリケーションのサーバー側コンポーネントをデプロイします。これらのコンポーネントは、Cloud Run に、同期オーケストレーション アーキテクチャと、Workflows で実行されるワークフローを実装します。
コンテナ イメージの構築とデプロイ
このセクションでは、マイクロサービスのコンテナ イメージをビルドし、Cloud Run にデプロイします。
Cloud Shell で、
Order
サービスのコンテナ イメージをorder-sync
という名前でビルドし、Cloud Run にデプロイします。cd $HOME/transactional-microservice-examples/services/order-sync gcloud builds submit --tag gcr.io/$PROJECT_ID/order-service-sync gcloud run deploy order-service-sync \ --image gcr.io/$PROJECT_ID/order-service-sync \ --platform=managed --region=us-central1 \ --no-allow-unauthenticated
customer-sync
という名前のCustomer
サービスのコンテナ イメージをビルドし、Cloud Run にデプロイします。cd $HOME/transactional-microservice-examples/services/customer-sync gcloud builds submit --tag gcr.io/$PROJECT_ID/customer-service-sync gcloud run deploy customer-service-sync \ --image gcr.io/$PROJECT_ID/customer-service-sync \ --platform=managed --region=us-central1 \ --no-allow-unauthenticated
Order
プロセッサ サービスのコンテナ イメージをorder-processor
という名前でビルドし、Cloud Run にデプロイします。cd $HOME/transactional-microservice-examples/services/order-processor gcloud builds submit --tag gcr.io/$PROJECT_ID/order-processor-service gcloud run deploy order-processor-service \ --image gcr.io/$PROJECT_ID/order-processor-service \ --platform=managed --region=us-central1 \ --no-allow-unauthenticated \ --set-env-vars "PROJECT_ID=$PROJECT_ID"
Cloud Run でマイクロサービスを呼び出すサービス アカウントを作成する
このセクションでは、Cloud Run でマイクロサービスを呼び出すサービス アカウントを作成するで作成した cloud-run-invoker
サービス アカウントを再利用します。
ワークフローをデプロイして注文を処理する
Cloud Shell で、
order-service-sync
サービスのcloud-run-invoker
サービス アカウントにrun.invoker
とrun.viewer
のロールを割り当てます。SERVICE_NAME="order-service-sync" gcloud run services add-iam-policy-binding $SERVICE_NAME \ --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \ --role=roles/run.invoker \ --platform=managed --region=us-central1 gcloud run services add-iam-policy-binding $SERVICE_NAME \ --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \ --role=roles/run.viewer \ --platform=managed --region=us-central1
customer-service-sync
サービスのcloud-run-invoker
サービス アカウントにrun.invoker
とrun.viewer
のロールを割り当てます。SERVICE_NAME="customer-service-sync" gcloud run services add-iam-policy-binding $SERVICE_NAME \ --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \ --role=roles/run.invoker \ --platform=managed --region=us-central1 gcloud run services add-iam-policy-binding $SERVICE_NAME \ --member=serviceAccount:$SERVICE_ACCOUNT_EMAIL \ --role=roles/run.viewer \ --platform=managed --region=us-central1
cloud-run-invoker
サービス アカウントを使用してワークフローをデプロイします。SERVICE_NAME="order-service-sync" ORDER_SERVICE_URL=$(gcloud run services list --platform managed \ --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}") SERVICE_NAME="customer-service-sync" CUSTOMER_SERVICE_URL=$(gcloud run services list --platform managed \ --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}") cd $HOME/transactional-microservice-examples/services/order-processor cp order_workflow.yaml.template order_workflow.yaml sed -i "s#ORDER-SERVICE-URL#${ORDER_SERVICE_URL}#" order_workflow.yaml sed -i "s#CUSTOMER-SERVICE-URL#${CUSTOMER_SERVICE_URL}#" order_workflow.yaml gcloud beta workflows deploy order_workflow \ --source=order_workflow.yaml \ --service-account=$SERVICE_ACCOUNT_EMAIL
コレオグラフィー ベースの Saga アーキテクチャ コンポーネントをテストする
このセクションでは、コレオグラフィー ベースの Saga アーキテクチャにデプロイされたコンポーネントを curl
コマンドを使用してテストします。
Cloud Shell で、
customer-service-async
マイクロサービスとorder-service-async
マイクロサービスの API エンドポイントの URL を指す環境変数を設定します。SERVICE_NAME="customer-service-async" CUSTOMER_SERVICE_URL=$(gcloud run services list --platform managed \ --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}") SERVICE_NAME="order-service-async" ORDER_SERVICE_URL=$(gcloud run services list --platform managed \ --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}")
顧客エントリを作成する
Cloud Shell で、
customer-service-async
という名前のCustomer
サービスに API リクエストを送信して、customer01
という名前のお客様 ID を作成します。curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \ -H "Content-Type: application/json" \ -d '{"customer_id":"customer01", "limit":10000}' \ -s ${CUSTOMER_SERVICE_URL}/api/v1/customer/limit | jq .
出力は次のようになります。
{ "credit": 0, "customer_id": "customer01", "limit": 10000 }
注文を送信する
このセクションでは、注文を送信して、注文 ID の割り当てをトリガーします。
Cloud Shell で、
order-service-async
という名前のOrder
サービスに API リクエストを送信して注文を送信します。curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \ -H "Content-Type: application/json" \ -d '{"customer_id":"customer01", "number":10}' \ -s ${ORDER_SERVICE_URL}/api/v1/order/create | jq .
この例では、
"number": 10
を指定して 10 個の商品を注文します。出力は次のようになります。
{ "customer_id": "customer01", "number": 10, "order_id": "720d1305-b6fd-4f57-aaf4-fd2ca5bdfe1e", "status": "pending" }
出力の
"order_id"
フラグには、注文に割り当てられている一意の注文 ID が表示されます。次のステップで使用するため、この ID をコピーします。注文 ID を環境変数に設定します。
ORDER_ID=ORDER_ID
ORDER_ID
は、前の手順でコピーした"order_id"
フラグに置き換えます。
注文のステータスを確認する
このセクションでは、注文ステータスの変化を確認します。
Cloud Shell で、
order-service-async
という名前のOrder
サービスに API リクエストを送信して注文のステータスを取得します。curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \ -H "Content-Type: application/json" \ -d "{\"customer_id\":\"customer01\", \"order_id\":\"$ORDER_ID\"}" \ -s ${ORDER_SERVICE_URL}/api/v1/order/get | jq .
出力は次のようになります。
{ "customer_id": "customer01", "number": 10, "order_id": "720d1305-b6fd-4f57-aaf4-fd2ca5bdfe1e", "status": "pending" }
出力の
"status"
フラグに注文のステータスが表示されます。トランザクション プロセスがまだ実行中の場合、ステータスは"pending"
です。この場合は、数分待ってから同じコマンドを実行して再度ステータスを確認します。トランザクション プロセスが完了すると、ステータスは"accepted"
になります。お客様 ID
customer01
のお客様情報を取得します。curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \ -H "Content-Type: application/json" \ -d '{"customer_id":"customer01"}' \ -s ${CUSTOMER_SERVICE_URL}/api/v1/customer/get | jq .
出力は次のようになります。
{ "credit": 1000, "customer_id": "customer01", "limit": 10000 }
"credit"
フラグは、お客様の現在のクレジット使用状況を示します。このトランザクションのビジネス ロジックでは、1 つの商品に対してクレジットが 100 増加するため、この場合は 1,000 増加します。注文プロセスを繰り返すには、注文を送信するの手順を繰り返します。
"number": 100
を指定して 100 個の商品を注文した場合、クレジット使用量が上限を超えているため、注文の最終ステータスは"rejected"
になります。
同期オーケストレーション アーキテクチャ コンポーネントをテストする
このセクションでは、curl
コマンドを使用して、同期オーケストレーション アーキテクチャのデプロイされたコンポーネントをテストします。
Cloud Shell で、
customer-service-sync
、order-service-sync
、order-processor-service
マイクロサービスの API エンドポイントの URL を指す環境変数を設定します。SERVICE_NAME="customer-service-sync" CUSTOMER_SERVICE_URL=$(gcloud run services list --platform managed \ --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}") SERVICE_NAME="order-service-sync" ORDER_SERVICE_URL=$(gcloud run services list --platform managed \ --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}") SERVICE_NAME="order-processor-service" ORDER_PROCESSOR_URL=$(gcloud run services list --platform managed \ --format="table[no-heading](URL)" --filter="SERVICE:${SERVICE_NAME}")
顧客エントリを作成する
Cloud Shell で、
customer-service-sync
という名前のCustomer
サービスに API リクエストを送信して、customer02
という名前のお客様 ID を作成します。curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \ -H "Content-Type: application/json" \ -d '{"customer_id":"customer02", "limit":10000}' \ -s ${CUSTOMER_SERVICE_URL}/api/v1/customer/limit | jq .
出力は次のようになります。
{ "credit": 0, "customer_id": "customer02", "limit": 10000 }
注文を送信する
このセクションでは、注文を送信して結果を確認します。
Cloud Shell で、
Order
サービスorder-processor-service
に API リクエストを送信して注文を送信します。curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \ -H "Content-Type: application/json" \ -d '{"customer_id":"customer02", "number":10}' \ -s ${ORDER_PROCESSOR_URL}/api/v1/order/process | jq .
この場合、
"number": 10
を指定して 10 個の商品を注文します。出力は次のようになります。
{ "customer_id": "customer02", "number": 10, "order_id": "fb6d5087-dd99-4d5a-84c2-0e381016b9d3", "status": "accepted" }
トランザクションのワークフローは同期的に実行されるため、クライアントはすぐに結果を確認できます。この場合、最終ステータスは
"accepted"
となります。お客様 ID が
customer02
のお客様情報を取得します。curl -X POST -H "Authorization: Bearer $(gcloud auth print-identity-token)" \ -H "Content-Type: application/json" \ -d '{"customer_id":"customer02"}' \ -s ${CUSTOMER_SERVICE_URL}/api/v1/customer/get | jq .
出力は次のようになります。
{ "credit": 1000, "customer_id": "customer02", "limit": 10000 }
お客様のクレジット使用量は 1,000 増加します。注文プロセスを繰り返すには、上述の手順を繰り返します。値
"number": 100
を指定して 100 個の商品を注文すると、クレジット使用量が上限を超えているため、クライアントはすぐに結果"rejected"
を取得します。
省略可: ウェブ アプリケーションをデプロイする
このセクションでは、チュートリアルのオプションの拡張として、前のセクションでデプロイしたマイクロサービス上にトランザクション ワークフローを実行するサンプル ウェブ アプリケーションをデプロイします。Firebase Hosting を使用して、Cloud Run で実行されているバックエンド マイクロサービスとやり取りするウェブ アプリケーションをデプロイします。
Firebase Hosting には組み込みの Cloud Run のインテグレーションがあります。これにより、ウェブ アプリケーションとバックエンド マイクロサービスのサービスが同じドメインでホストできます。他のタイプのデプロイでは、クロスオリジン リソース シェアリング(CORS)の問題が発生する可能性があります。詳細については、エンドユーザーの認証をご覧ください。
Firebase Hosting のインテグレーションでは、次の処理が行われます。
- ウェブ アプリケーションの静的アセットをホストします。
- API へのアクセスを Cloud Run の次のバックエンド サービスにリダイレクトします。
/customer-service-sync/*
からcustomer-service-sync
/customer-service-async/*
からcustomer-service-async
/order-service-sync/*
からorder-service-sync
/order-service-async/*
からorder-service-async
/order-processor-service/*
からorder-processor-service
- ウェブ アプリケーションの静的アセットを返します。
Firebase を設定する
このセクションでは、Firebase Hosting でウェブ アプリケーションをホストするように Firebase を設定します。
Firebase CLI を使用するには、Cloud Shell で Node.js を最新の LTS バージョンに更新します。
nvm install lts/gallium
Firebase リソースを既存の Google Cloud プロジェクトに追加する
firebase projects:addfirebase $PROJECT_ID
Firebase CLI のデフォルトのプロジェクト エイリアスを追加します。
jq -n --arg project_id $PROJECT_ID \ '{"projects": {"default":$project_id}}' > \ $HOME/transactional-microservice-examples/frontend/.firebaserc
ウェブ アプリケーションをデプロイする
このセクションでは、Firebase Hosting にウェブ アプリケーションをデプロイします。
Cloud Shell で、ウェブ フロントエンド アプリケーションをビルドします。
cd $HOME/transactional-microservice-examples/frontend yarn install && yarn build
Firebase Hosting にウェブ アプリケーションをデプロイします。
firebase deploy
ウェブ アプリケーションを使用する
- 前のセクションでコピーしたホスティング URL をブラウザに入力します。ウェブ アプリケーションのホームページが表示されます。
ID トークンでウェブ アプリケーションを初期化する
このセクションでは、ID トークンでウェブ アプリケーションを初期化し、ショッピング ページに進みます。
Cloud Shell で、ID トークンを出力します。
gcloud auth print-identity-token
前のコマンドの出力をコピーし、テキスト ボックスに貼り付けます。
[続行] をクリックします。ID トークンが有効であれば、[ショッピング] ページにリダイレクトされます。
カートに商品を追加する
- ウェブ アプリケーションで、[カートに追加] をクリックして商品をカートに追加します。
カート内の商品を確認する
- ウェブ アプリケーションで、ナビゲーション バーのカートアイコンをクリックして [ご購入手続き] ページに移動します。カートに商品が表示されています。
非同期サービスを使用して注文を送信する
- ウェブ アプリケーションで、ナビゲーション バーのショッピング カート アイコンをクリックして [ご購入手続き] ページに移動します。
- [注文を送信(非同期)] をクリックします。注文が正常に送信されると、[注文履歴] ページにリダイレクトされます。
同期サービスを使用して注文を送信する
- ウェブ アプリケーションで、ナビゲーション バーのショッピング カート アイコンをクリックして [ご購入手続き] ページに移動します。
- [注文を送信(同期)] をクリックします。注文が正常に送信されると、[注文履歴] ページにリダイレクトされます。
注文履歴を確認する
- ウェブ アプリケーションで、ナビゲーション バーの [注文] をクリックして [注文履歴] ページに移動します。
未完了の注文のステータスを更新する
- ウェブ アプリケーションで、ナビゲーション バーの [注文] をクリックして [注文履歴] ページに移動します。
- 個々の注文の更新アイコンをクリックします。このアイコンは、注文が非同期で処理され、ステータスが
pending
の場合にのみ表示されます。
お客様の現在の予算を確認する
- ウェブ アプリケーションで、ナビゲーション バーのお客様の名前(customer-number)をクリックして、お客様のプロフィール ページに移動します。
ウェブ アプリケーションの状態をリセットする
- ブラウザで、ウェブ アプリケーションを再読み込みします。カート内の商品、注文履歴、顧客情報などの状態がリセットされ、ホームページにリダイレクトされます。
クリーンアップ
このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、リソースを含むプロジェクトを削除するか、プロジェクトを維持して個々のリソースを削除します。
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
リソースを個別に削除する
このセクションでは、このチュートリアルで使用する個別のリソースを削除します。
Firebase Hosting を無効にする
Cloud Shell で次のコマンドを実行して、Firebase Hosting トラフィックの配信を停止します。
firebase hosting:disable -f
Cloud Run でサービスを削除する
Cloud Shell で、Cloud Run でのサービスを削除します。
gcloud run services delete order-service-async \ --platform=managed --region=us-central1 --quiet gcloud run services delete customer-service-async \ --platform=managed --region=us-central1 --quiet gcloud run services delete order-service-sync \ --platform=managed --region=us-central1 --quiet gcloud run services delete customer-service-sync \ --platform=managed --region=us-central1 --quiet gcloud run services delete order-processor-service \ --platform=managed --region=us-central1 --quiet gcloud run services delete event-publisher \ --platform=managed --region=us-central1 --quiet
Cloud Run で使用されているコンテナ イメージを削除する
Cloud Shell で、このチュートリアル用に作成したコンテナ イメージを削除します。
gcloud container images delete \ gcr.io/$PROJECT_ID/order-service-async --force-delete-tags --quiet gcloud container images delete \ gcr.io/$PROJECT_ID/customer-service-async --force-delete-tags --quiet gcloud container images delete \ gcr.io/$PROJECT_ID/event-publisher --force-delete-tags --quiet gcloud container images delete \ gcr.io/$PROJECT_ID/order-service-sync --force-delete-tags --quiet gcloud container images delete \ gcr.io/$PROJECT_ID/customer-service-sync --force-delete-tags --quiet gcloud container images delete \ gcr.io/$PROJECT_ID/order-processor-service --force-delete-tags --quiet
ワークフローを削除する
Cloud Shell でワークフローを削除します。
gcloud beta workflows delete order_workflow --quiet
Pub/Sub サブスクリプションとトピックを削除する
Cloud Shell で、Pub/Sub サブスクリプションを削除します。
gcloud pubsub subscriptions delete push-customer-to-order --quiet gcloud pubsub subscriptions delete push-order-to-customer --quiet
Pub/Sub トピックを削除する
gcloud pubsub topics delete order-service-event --quiet gcloud pubsub topics delete customer-service-event --quiet
Datastore からレコードを削除する
- Google Cloud コンソールの Datastore メニューで、[エンティティ] を開きます。
- [
Customer
] 内のすべてのエンティティを確認します。 - [削除]、[確認] の順にクリックし、エンティティを完全に削除します。
- 次のフィールドに対して、手順 2 と 3 を繰り返します。
Event
Order
ProcessedEvent
Cloud Scheduler でジョブを削除する
Cloud Shell で、Cloud Scheduler で実行されているジョブを削除します。
gcloud scheduler jobs delete event-publisher-scheduler --quiet \ --location=us-central1
サービス アカウントを削除する
Cloud Shell でサービス アカウントを作成します。
gcloud iam service-accounts delete $SERVICE_ACCOUNT_EMAIL --quiet
チュートリアル アセットを削除する
Cloud Shell で、このチュートリアルで使用するアセットを削除します。
cd $HOME rm -rf transactional-microservice-examples
次のステップ
- マイクロサービスの設計、ビルド、デプロイの詳細については、次のシリーズをご覧ください。
- モノリスとマイクロサービス両方のメリットと課題について確認する
- サービス境界を定義し、モノリスをリファクタリングする方法を確認する。
- マイクロサービス間でトランザクションを分散する方法を理解する
- 多言語環境での分散トレースを設定する方法を確認する
- マイクロサービス アーキテクチャのサービス メッシュを実装する方法を確認する
- リファレンス アーキテクチャ、図、ベスト プラクティスについては、Cloud アーキテクチャ センターをご確認ください。