JAZUG札幌支部(きたあず) 第23回勉強会のセッションスライドです。
Azure アーキテクチャ センターを読んで LINE BOT を作ってみた話2019.10.26JAZUG札幌支部(きたあず) 第23回勉強会
View Slide
自己紹介名前原 敏之個人Twitter : @TonyTonyKunROMANCE DAWN for the new world• http://gooner.hateblo.jp/Microsoft MVP for Microsoft Azure 2017 〜仕事株式会社アークウェイきたあず東京から来ました2年ぶり2回目の登壇です(前回は AKS ネタでした)Copyright© 2019, @thara All Rights Reserved.2
このセッションで伝えたいことLINE BOT 開発を題材に、Azure が提供するサービスを組み合わせた構造(アーキテクチャ)を決める Tips を紹介しますAzure アーキテクチャ センターには、実践向けの役立つ情報が多いので、活用のポイントを紹介しますAzure が提供するサービスについて、一つひとつの機能の詳しい説明はしませんので、ご了承くださいCopyright© 2019, @thara All Rights Reserved.3
目次LINE BOT アプリを作ってみたAzure アーキテクチャ センターLINE BOT アプリのアーキテクチャアーキテクチャ要素の解説① LINE Messaging API② Geo Search③ CQRS + Event Sourcing④ Backends for Frontends⑤ Serverless⑥ External Configuration Store⑦ MonitoringまとめCopyright© 2019, @thara All Rights Reserved.4
LINE BOT を作ろうと思ったきっかけは・・・LINE BOT を作ってみたCopyright© 2019, @thara All Rights Reserved.5
LINE BOT を作ってみた近くにある孤独のグルメに登場したお店を LINE で教えてほしいCopyright© 2019, @thara All Rights Reserved.6
Azure アーキテクチャ センターAzure が提供するサービスの機能ではなくアーキテクチャの観点で、Azure 上に構築するアプリケーションの設計に関する知見を得ることができるCopyright© 2019, @thara All Rights Reserved.7https://docs.microsoft.com/ja-jp/azure/architecture/?WT.mc_id=AZ-MVP-5002272
Azure アーキテクチャ センターおすすめコンテンツAzure アプリケーション アーキテクチャ ガイド• https://docs.microsoft.com/ja-jp/azure/architecture/guide/?WT.mc_id=AZ-MVP-5002272クラウド設計パターン• https://docs.microsoft.com/ja-jp/azure/architecture/patterns/?WT.mc_id=AZ-MVP-5002272Azure の参照アーキテクチャ• https://docs.microsoft.com/ja-jp/azure/architecture/reference-architectures/?WT.mc_id=AZ-MVP-5002272Copyright© 2019, @thara All Rights Reserved.8
LINE BOT アプリのアーキテクチャアーキテクチャ要素ごとにポイントを解説していきますCopyright© 2019, @thara All Rights Reserved.9FunctionAppsWeb AppsAzureCosmos DBAzure SearchFunctionAppsStatic Website withAzure StorageKey Vault App Configuration App InsightsAzure SearchIndexerLINE Messaging APILINE BOTWebBrowser
① LINE Messaging APICopyright© 2019, @thara All Rights Reserved.10FunctionAppsWeb AppsAzureCosmos DBAzure SearchFunctionAppsStatic Website withAzure StorageKey Vault App Configuration App InsightsAzure SearchIndexerLINE Messaging APILINE BOTWebBrowser
① LINE Messaging APICopyright© 2019, @thara All Rights Reserved.11https://developers.line.biz/ja/services/messaging-api/LINE アプリから、現在位置の緯度経度を取得したいLINE Messaging API を採用するテキストメッセージ・・・送りたいテキストを送信できる位置情報メッセージ・・・住所や緯度経度の座標を受信できるクイックリプライ ・・・メッセージのほかに、トーク画面の下部にボタンを表示できる
② Geo SearchCopyright© 2019, @thara All Rights Reserved.12FunctionAppsWeb AppsAzureCosmos DBAzure SearchFunctionAppsStatic Website withAzure StorageKey Vault App Configuration App InsightsAzure SearchIndexerLINE Messaging APILINE BOTWebBrowser
② Geo SearchCopyright© 2019, @thara All Rights Reserved.13今いる場所から、近くにあるお店を検索したいデータストアには、地理空間の検索ができる Azure Search を採用するAzure Search といえば全文検索エンジンのイメージがつよいが、よくある周辺の店舗一覧を検索する機能も提供しているAzure Search SDK を利用して、指定した緯度経度から、距離の近い順にソートしたデータを返すことができる
② Geo SearchCopyright© 2019, @thara All Rights Reserved.14JSON のスキーマ ソースコード
③ CQRS + Event SourcingCopyright© 2019, @thara All Rights Reserved.15FunctionAppsWeb AppsAzureCosmos DBAzure SearchFunctionAppsStatic Website withAzure StorageKey Vault App Configuration App InsightsAzure SearchIndexerLINE Messaging APILINE BOTWebBrowser
③ CQRS + Event SourcingCopyright© 2019, @thara All Rights Reserved.16https://docs.microsoft.com/ja-jp/azure/architecture/patterns/cqrs?WT.mc_id=AZ-MVP-5002272地理空間検索で Azure Search を利用したいが、店舗データはスキーマレスなマスターとして管理したいCQRS ( Command Query Responsibility Segregation ) を採用する書き込みと読み取りのエンティティを別々の最適化した構造にできる物理的にデータストアを分けることで、パフォーマンス、スケーラビリティ、セキュリティを向上できる
③ CQRS + Event SourcingCopyright© 2019, @thara All Rights Reserved.17https://docs.microsoft.com/ja-jp/azure/architecture/patterns/event-sourcing?WT.mc_id=AZ-MVP-5002272検索用と更新用のデータストアを分離するので、店舗データを同期したいAzure Cosmos DB の Azure Search Indexer を採用するCQRS の実装として、Event Sourcing が使われるケースが多いEvent Sourcing のメッセージングは、エラーや重複を考慮する必要があり、設計が複雑になりがちAzure Cosmos DB の Change Feed を使う選択もあるが、シンプルなデータ同期処理なので、 AzureSearch Indexer で十分要件を満たせる
④ Backends for FrontendsCopyright© 2019, @thara All Rights Reserved.18FunctionAppsWeb AppsAzureCosmos DBAzure SearchFunctionAppsStatic Website withAzure StorageKey Vault App Configuration App InsightsAzure SearchIndexerLINE Messaging APILINE BOTWebBrowser
④ Backends for FrontendsCopyright© 2019, @thara All Rights Reserved.19https://docs.microsoft.com/ja-jp/azure/architecture/patterns/backends-for-frontends?WT.mc_id=AZ-MVP-5002272LINE BOT 向けの API の改修による影響を Web アプリに与えたくないBackends for Frontends パターンを採用し、LINE BOT 向けの API のみを公開するバックエンドサービスを構築するフロントエンド固有の要件に対応しやすく、保守性が向上するフロントエンドから利用する API の複雑さが軽減され、パフォーマンスが向上する
⑤ ServerlessCopyright© 2019, @thara All Rights Reserved.20FunctionAppsWeb AppsAzureCosmos DBAzure SearchFunctionAppsStatic Website withAzure StorageKey Vault App Configuration App InsightsAzure SearchIndexerLINE Messaging APILINE BOTWebBrowser
⑤ ServerlessCopyright© 2019, @thara All Rights Reserved.21https://docs.microsoft.com/ja-jp/azure/architecture/serverless/?WT.mc_id=AZ-MVP-5002272https://docs.microsoft.com/ja-jp/azure/azure-functions/functions-best-practices?WT.mc_id=AZ-MVP-5002272LINE から送信されるメッセージのイベントドリブンで、サーバーを意識せずアプリケーション開発に集中したいサーバーレスのアーキテクチャを採用するAzure Functions v2 を採用し、.NET Core 2.1 ベースで開発するホスティングプランは、Consumption Plan を採用するコールドスタート回避や高速スケーリングが必要なら、Premium Plan (Preview) の採用を検討するAPI は、Azure Functions の API Key 認証で保護するHTTP 処理の同時並行処理数を調整して、スケーリングをチューニングする(host.json)
余談:HttpClient をする際の注意事項HttpClient オブジェクトは1度だけ生成して再利用するC# で HTTP 通信するサンプルの多くが using で囲っているが、この使い方は間違いAzure Functions(App Services)でソケットが再利用されず、SNATポートが枯渇し、高負荷時に突然ダウンしてしまうAzure Functions v2 では、HttpClientFactory を使うのがオススメCopyright© 2019, @thara All Rights Reserved.22https://qiita.com/superriver/items/91781bca04a76aec7dc0
⑥ External Configuration StoreCopyright© 2019, @thara All Rights Reserved.23FunctionAppsWeb AppsAzureCosmos DBAzure SearchFunctionAppsStatic Website withAzure StorageKey Vault App Configuration App InsightsAzure SearchIndexerLINE Messaging APILINE BOTWebBrowser
⑥ External Configuration StoreCopyright© 2019, @thara All Rights Reserved.24https://docs.microsoft.com/ja-jp/azure/architecture/patterns/external-configuration-store?WT.mc_id=AZ-MVP-5002272アプリケーションの構成情報を一元管理したいAzure Key Vault と Azure App Configuration を採用する構成情報はソースコードに埋め込みたくない環境変数による切り替えが定石だが、アプリケーションの数が多くなると管理が面倒になるAzure Key Vault と Azure App Configuration (Preview) への接続情報は、 Azure ManagementIdentity を利用することで、アプリケーションで管理する必要がなくなる
⑦ MonitoringCopyright© 2019, @thara All Rights Reserved.25FunctionAppsWeb AppsAzureCosmos DBAzure SearchFunctionAppsStatic Website withAzure StorageKey Vault App Configuration App InsightsAzure SearchIndexerLINE Messaging APILINE BOTWebBrowser
⑦ MonitoringCopyright© 2019, @thara All Rights Reserved.26https://docs.microsoft.com/ja-jp/azure/architecture/reference-architectures/app-service-web-app/app-monitoring?WT.mc_id=AZ-MVP-5002272アプリケーションで発生した障害を検知して解決したいAzure Monitor の Application Insights を採用するアプリケーションのパフォーマンスをモニタリングし、エラー情報を収集する緊急度の高いエラーの場合は、アラートを通知するBackends for Frontends パターンで分割したバックエンド API 群を分散トレーシングしたいので、1つの Application Insights を共有する
まとめAzure アーキテクチャ センターは、実践向けの役立つ情報が多いので、積極的に活用しましょう!ただし、参照アーキテクチャやパターンを無理に適用する必要はありません設計はシンプルさが大事なので、バランスとトレードオフを判断しましょうCopyright© 2019, @thara All Rights Reserved.27
AppendixRepositoryFrontend for LINE BOT API• https://github.com/thara0402/goro-botBackend for Gourmet API• https://github.com/thara0402/goro-apiCopyright© 2019, @thara All Rights Reserved.28