$30 off During Our Annual Pro Sale. View Details »

Azure アーキテクチャ センターを読んで LINE BOT を作ってみた話/kitaazu23

TonyTonyKun
October 25, 2019

Azure アーキテクチャ センターを読んで LINE BOT を作ってみた話/kitaazu23

JAZUG札幌支部(きたあず) 第23回勉強会のセッションスライドです。

TonyTonyKun

October 25, 2019
Tweet

More Decks by TonyTonyKun

Other Decks in Technology

Transcript

  1. Azure アーキテクチャ センターを
    読んで LINE BOT を作ってみた話
    2019.10.26
    JAZUG札幌支部(きたあず) 第23回勉強会

    View Slide

  2. 自己紹介
    名前
    原 敏之
    個人
    Twitter : @TonyTonyKun
    ROMANCE 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

    View Slide

  3. このセッションで伝えたいこと
    LINE BOT 開発を題材に、Azure が提供するサービスを組み合わ
    せた構造(アーキテクチャ)を決める Tips を紹介します
    Azure アーキテクチャ センターには、実践向けの役立つ情報が
    多いので、活用のポイントを紹介します
    Azure が提供するサービスについて、一つひとつの機能の詳し
    い説明はしませんので、ご了承ください
    Copyright© 2019, @thara All Rights Reserved.
    3

    View Slide

  4. 目次
    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

    View Slide

  5. LINE BOT を作ろうと思ったきっかけは・・・
    LINE BOT を作ってみた
    Copyright© 2019, @thara All Rights Reserved.
    5

    View Slide

  6. LINE BOT を作ってみた
    近くにある孤独のグルメに登場したお店を LINE で教えてほしい
    Copyright© 2019, @thara All Rights Reserved.
    6

    View Slide

  7. Azure アーキテクチャ センター
    Azure が提供するサービスの機能ではなくアーキテクチャの観
    点で、Azure 上に構築するアプリケーションの設計に関する知
    見を得ることができる
    Copyright© 2019, @thara All Rights Reserved.
    7
    https://docs.microsoft.com/ja-jp/azure/architecture/?WT.mc_id=AZ-MVP-5002272

    View Slide

  8. 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-5002272
    Azure の参照アーキテクチャ
    • https://docs.microsoft.com/ja-jp/azure/architecture/reference-architectures/?WT.mc_id=AZ-MVP-5002272
    Copyright© 2019, @thara All Rights Reserved.
    8

    View Slide

  9. LINE BOT アプリのアーキテクチャ
    アーキテクチャ要素ごとにポイントを解説していきます
    Copyright© 2019, @thara All Rights Reserved.
    9
    Function
    Apps
    Web Apps
    Azure
    Cosmos DB
    Azure Search
    Function
    Apps
    Static Website with
    Azure Storage
    Key Vault App Configuration App Insights
    Azure Search
    Indexer
    LINE Messaging API
    LINE BOT
    Web
    Browser

    View Slide

  10. ① LINE Messaging API
    Copyright© 2019, @thara All Rights Reserved.
    10
    Function
    Apps
    Web Apps
    Azure
    Cosmos DB
    Azure Search
    Function
    Apps
    Static Website with
    Azure Storage
    Key Vault App Configuration App Insights
    Azure Search
    Indexer
    LINE Messaging API
    LINE BOT
    Web
    Browser

    View Slide

  11. ① LINE Messaging API
    Copyright© 2019, @thara All Rights Reserved.
    11
    https://developers.line.biz/ja/services/messaging-api/
    LINE アプリから、現在位置の緯度経度を取得したい
    LINE Messaging API を採用する
    テキストメッセージ・・・送りたいテキストを送信できる
    位置情報メッセージ・・・住所や緯度経度の座標を受信できる
    クイックリプライ ・・・メッセージのほかに、トーク画面の下部にボタンを表示できる

    View Slide

  12. ② Geo Search
    Copyright© 2019, @thara All Rights Reserved.
    12
    Function
    Apps
    Web Apps
    Azure
    Cosmos DB
    Azure Search
    Function
    Apps
    Static Website with
    Azure Storage
    Key Vault App Configuration App Insights
    Azure Search
    Indexer
    LINE Messaging API
    LINE BOT
    Web
    Browser

    View Slide

  13. ② Geo Search
    Copyright© 2019, @thara All Rights Reserved.
    13
    今いる場所から、近くにあるお店を検索したい
    データストアには、地理空間の検索ができる Azure Search を採用する
    Azure Search といえば全文検索エンジンのイメージがつよいが、よくある周辺の店舗一覧を検索
    する機能も提供している
    Azure Search SDK を利用して、指定した緯度経度から、距離の近い順にソートしたデータを返す
    ことができる

    View Slide

  14. ② Geo Search
    Copyright© 2019, @thara All Rights Reserved.
    14
    JSON のスキーマ ソースコード

    View Slide

  15. ③ CQRS + Event Sourcing
    Copyright© 2019, @thara All Rights Reserved.
    15
    Function
    Apps
    Web Apps
    Azure
    Cosmos DB
    Azure Search
    Function
    Apps
    Static Website with
    Azure Storage
    Key Vault App Configuration App Insights
    Azure Search
    Indexer
    LINE Messaging API
    LINE BOT
    Web
    Browser

    View Slide

  16. ③ CQRS + Event Sourcing
    Copyright© 2019, @thara All Rights Reserved.
    16
    https://docs.microsoft.com/ja-jp/azure/architecture/patterns/cqrs?WT.mc_id=AZ-MVP-5002272
    地理空間検索で Azure Search を利用したいが、
    店舗データはスキーマレスなマスターとして管理したい
    CQRS ( Command Query Responsibility Segregation ) を採用する
    書き込みと読み取りのエンティティを別々
    の最適化した構造にできる
    物理的にデータストアを分けることで、パ
    フォーマンス、スケーラビリティ、セキュ
    リティを向上できる

    View Slide

  17. ③ CQRS + Event Sourcing
    Copyright© 2019, @thara All Rights Reserved.
    17
    https://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 を使う選択もあるが、シンプルなデータ同期処理なので、 Azure
    Search Indexer で十分要件を満たせる

    View Slide

  18. ④ Backends for Frontends
    Copyright© 2019, @thara All Rights Reserved.
    18
    Function
    Apps
    Web Apps
    Azure
    Cosmos DB
    Azure Search
    Function
    Apps
    Static Website with
    Azure Storage
    Key Vault App Configuration App Insights
    Azure Search
    Indexer
    LINE Messaging API
    LINE BOT
    Web
    Browser

    View Slide

  19. ④ Backends for Frontends
    Copyright© 2019, @thara All Rights Reserved.
    19
    https://docs.microsoft.com/ja-jp/azure/architecture/patterns/backends-for-frontends?WT.mc_id=AZ-MVP-5002272
    LINE BOT 向けの API の改修による影響を Web アプリに与えたくない
    Backends for Frontends パターンを採用し、LINE BOT 向けの API のみ
    を公開するバックエンドサービスを構築する
    フロントエンド固有の要件に対応しやすく、
    保守性が向上する
    フロントエンドから利用する API の複雑さ
    が軽減され、パフォーマンスが向上する

    View Slide

  20. ⑤ Serverless
    Copyright© 2019, @thara All Rights Reserved.
    20
    Function
    Apps
    Web Apps
    Azure
    Cosmos DB
    Azure Search
    Function
    Apps
    Static Website with
    Azure Storage
    Key Vault App Configuration App Insights
    Azure Search
    Indexer
    LINE Messaging API
    LINE BOT
    Web
    Browser

    View Slide

  21. ⑤ Serverless
    Copyright© 2019, @thara All Rights Reserved.
    21
    https://docs.microsoft.com/ja-jp/azure/architecture/serverless/?WT.mc_id=AZ-MVP-5002272
    https://docs.microsoft.com/ja-jp/azure/azure-functions/functions-best-practices?WT.mc_id=AZ-MVP-5002272
    LINE から送信されるメッセージのイベントドリブンで、
    サーバーを意識せずアプリケーション開発に集中したい
    サーバーレスのアーキテクチャを採用する
    Azure Functions v2 を採用し、.NET Core 2.1 ベースで開発する
    ホスティングプランは、Consumption Plan を採用する
    コールドスタート回避や高速スケーリングが必要なら、Premium Plan (Preview) の採用を検討する
    API は、Azure Functions の API Key 認証で保護する
    HTTP 処理の同時並行処理数を調整して、スケーリングをチューニングする(host.json)

    View Slide

  22. 余談:HttpClient をする際の注意事項
    HttpClient オブジェクトは1度だけ生成して再利用する
    C# で HTTP 通信するサンプルの多くが using で囲っているが、この
    使い方は間違い
    Azure Functions(App Services)でソケットが再利用されず、SNAT
    ポートが枯渇し、高負荷時に突然ダウンしてしまう
    Azure Functions v2 では、HttpClientFactory を使うのがオススメ
    Copyright© 2019, @thara All Rights Reserved.
    22
    https://qiita.com/superriver/items/91781bca04a76aec7dc0

    View Slide

  23. ⑥ External Configuration Store
    Copyright© 2019, @thara All Rights Reserved.
    23
    Function
    Apps
    Web Apps
    Azure
    Cosmos DB
    Azure Search
    Function
    Apps
    Static Website with
    Azure Storage
    Key Vault App Configuration App Insights
    Azure Search
    Indexer
    LINE Messaging API
    LINE BOT
    Web
    Browser

    View Slide

  24. ⑥ External Configuration Store
    Copyright© 2019, @thara All Rights Reserved.
    24
    https://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 Management
    Identity を利用することで、アプリケーションで管理する必要がなくなる

    View Slide

  25. ⑦ Monitoring
    Copyright© 2019, @thara All Rights Reserved.
    25
    Function
    Apps
    Web Apps
    Azure
    Cosmos DB
    Azure Search
    Function
    Apps
    Static Website with
    Azure Storage
    Key Vault App Configuration App Insights
    Azure Search
    Indexer
    LINE Messaging API
    LINE BOT
    Web
    Browser

    View Slide

  26. ⑦ Monitoring
    Copyright© 2019, @thara All Rights Reserved.
    26
    https://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 を共有する

    View Slide

  27. まとめ
    Azure アーキテクチャ センターは、実践向けの役立つ情報が多
    いので、積極的に活用しましょう!
    ただし、参照アーキテクチャやパターンを無理に適用する必要
    はありません
    設計はシンプルさが大事なので、バランスとトレードオフを判
    断しましょう
    Copyright© 2019, @thara All Rights Reserved.
    27

    View Slide

  28. Appendix
    Repository
    Frontend for LINE BOT API
    • https://github.com/thara0402/goro-bot
    Backend for Gourmet API
    • https://github.com/thara0402/goro-api
    Copyright© 2019, @thara All Rights Reserved.
    28

    View Slide