Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GoとGAEによるWebアプリケーション開発(Go Con 2016 Spring)

GoとGAEによるWebアプリケーション開発(Go Con 2016 Spring)

2016年4月23日の Go Conference 2016 Spring の発表資料です。

Tatsuya Tsuruoka

April 23, 2016
Tweet

More Decks by Tatsuya Tsuruoka

Other Decks in Programming

Transcript

  1. (Pͱ("&ʹΑΔ
 8FCΞϓϦέʔγϣϯ։ൃ 2016/04/23

  2. ࣗݾ঺հ 2 • גࣜձࣾι΢κ΢ ˍ גࣜձࣾϝϧΧϦ • ٕज़తͳҙࢥܾఆͱνʔϜ࡞Γ͕࢓ࣄͰ͢ !UUTVSVPLB ௽Ԭ

    ୡ໵
  3. ࠓճ͓఻͍͑ͨ͜͠ͱ 3 1. Google App Engine / Go Ͱ
 ݱ࣮తͳ

    Web ΞϓϦΛ։ൃ͠ɺӡ༻͍ͯ͘͜͠ͱ͕Ͱ͖Δ
 2. ͦͯ͠ Go ʹΑΔ Web ΞϓϦ։ൃ͕ɺ
 ࠓޙ·͢·͋ͨ͢Γ·͑ʹͳ͍ͬͯ͘
  4. INDEX Why Google App Engineʁ εέʔϥϏϦςΟʢΩϡʔͱӬଓԽʣ ϩά෼ੳͱӡ༻

  5. Why GAE? - GAE ͱ͸ 5 • GAE ͸ Web

    ΞϓϦͷͨΊͷ PaaS • ༷ʑͳ։ൃݴޠʹରԠ͓ͯ͠Γ Go ΋ͦͷ͏ͪͷͻͱͭ • ࣮ݱͰ͖Δ͜ͱ • ۃΊͯߴ͍εέʔϥϏϦςΟ • ਺req/sec ΋਺ेສreq/sec ΋ಉ͡Α͏ʹѻ͑Δ • Google ϨϕϧͷՄ༻ੑ • ো֐ʹΑͬͯαʔϏε͕ఀࢭ͢Δ͜ͱ͸ك • ϝϯςφϯεϑϦʔ • ࠷ॳʹΞʔΩςΫνϟΛܾΊΕ͹͋ͱ͸΋͏ख͕͔͔Βͳ͍
  6. Why GAE? - GAE ͱ͸ 6 • ࣮ࡍʹ GAE ͰͲΜͳ

    Web ΞϓϦΛ࡞͍͚ͬͯΔʁ • ࣄྫɿSnapchatɺIngress • ೔ผͷར༻Ϣʔβ͕1ԯਓΛ௒͑Δن໛ͷ࣮੷͕ɺ͢Ͱʹ͋Δ
 ※ͨͩ͠ݴޠ͸ Go Ͱ͸ͳ͍ͱࢥΘΕ·͢ • ϝϧΧϦࢠձࣾͷ৽نࣄۀʮϝϧΧϦ Ξοςʯ΋ GAE Λ࠷େݶʹར༻ • ΠϯϑϥΤϯδχΞ 0 ਓ • ϦϦʔε͔Βͷ໿1ϲ݄ؒʹٸܹͳΞΫηε૿ͳͲͰࠔͬͨ͜ͱ͸Ұ౓΋ͳ͠ • μ΢ϯλΠϜθϩɻܭըఀࢭͳ͠
 • → ݱ࣮తͳ Web ΞϓϦΛ࡞ΔͨΊͷػೳ͸උΘ͍ͬͯΔ
  7. Why GAE? 7 • ࠓճɺGAE ͷػೳΛ঺հ͢ΔͨΊʹ Web ΞϓϦΛߟ͑ͯΈΔ • ྫ͑͹ɺ͍·੝Γ্͕͍ͬͯΔ

    Bot ΞϓϦ
 4݄7೔ LINE BOT API ެ։ɺ4݄13೔ Facebook bots on Messenger ެ։ • → ͜ͷ Bot ΞϓϦΛ GAE ͰϋΠεέʔϥϒϧʹ࡞Δͱͨ͠Β...
  8. εέʔϥϏϦςΟʢΩϡʔͱӬଓԽʣ

  9. εέʔϥϏϦςΟ - ୯७ͳ Bot 9 • ػೳɿ • νϟοταʔόʢ͜ͷྫͰ͸LINEʣ͔Β ίʔϧόοΫΛड͚औΓɺ


    ϢʔβͷϝοηʔδͳͲΛऔಘͰ͖Δ • ϝοηʔδΛݩʹԿΒ͔ͷॲཧΛͯ͠ɺ
 Ϣʔβʹฦ৴Ͱ͖Δ
  10. εέʔϥϏϦςΟ - ୯७ͳ Bot 10 • ίʔϧόοΫΛϋϯυϦϯά͢Δ func init() {


    http.HandleFunc("/line/callback", receiveLine)
 }
 
 func receiveLine(w http.ResponseWriter, r *http.Request) {
 ...
 } • func init ͸ GAE/Go ΞϓϦͷཪଆ͔Βݺ͹ΕΔॏཁͳؔ਺ • func receiveLine ͷதͰૹΒΕͯ͘ΔϝοηʔδΛॲཧͯ͠ฦ৴ͨ͠Γ͢Δ
  11. εέʔϥϏϦςΟ - ୯७ͳ Bot 11 • ͜ΕΛσϓϩΠ͢Δ͚ͩͰ…
 ʢ͍͔ͭ͘՝୊͸͋Δ΋ͷͷʣεέʔϥϒϧͳ Bot ͕Ͱ͖͕͋Γ

    • ͜ͷ࣌఺ͷεέʔϥϏϦςΟɿ • ༑ୡ͕͍ͳ͍ͱ͖ → Πϯελϯε͸ىಈ͍ͯ͠ͳ͍ • ༑ୡ͕૿͑ͨΓϝοηʔδ͕ૹΒΕͯ͘ΔͱίʔϧόοΫ͕ୟ͔ΕΔ
 → ༑ୡͷ૿͑ํ΍ϝοηʔδͷྲྀྔʹ߹ΘͤͯΠϯελϯε͕ࣗಈͰ૿ݮ • Πϯελϯεͷىಈ͸਺ेʙ200msͳͷͰऔΓ͜΅͠ͳ͠ • Πϯελϯεͷىಈ࣌ؒ͸࣮૷ݴޠʹΑͬͯҟͳΔ • ΋ͪΖΜ Go ͕࠷଎
  12. εέʔϥϏϦςΟ - ୯७ͳ Bot 12 • ՝୊ɿ • ͻͱͭͷϦΫΤετΛॲཧ͢Δͷʹ͕͔͔࣌ؒͬͯ͠·͏ͱ͖ •

    GAE ଆɿϦΫΤετ͸60ඵͰλΠϜΞ΢τ͢Δ • LINE BOT ଆɿ 1ϦΫΤετ͸10ඵͰλΠϜΞ΢τ͢Δ • → ίʔϧόοΫͷड෇ͱͦͷޙͷॲཧΛඇಉظʹ͢Δඞཁ͋Γ • աڈͷϝοηʔδͷ΍ΓͱΓͱൺ΂ͯฦ৴Λ͍ͨ͠ • → աڈͷ΍ΓͱΓΛอଘͯ͠ࢀরͰ͖Δඞཁ͋Γ
  13. εέʔϥϏϦςΟ - Task Queue 13 • ΩϡʔΛ࢖ͬͯඇಉظʹॲཧ͢Δ • GAE Ͱ͸

    Task Queue ͱ͍͏ػೳ͕
 ఏڙ͞Ε͍ͯΔ プロジェクト App Engine App Engine Datastore Instances Search BigQuery Task Queue - GCE - Logs etc. - Networks
  14. εέʔϥϏϦςΟ - Task Queue 14 • Task Queue ͷ Push

    Queue Λ࢖͏ • ͖Ίࡉ͔͍࣮ߦ੍ޚ͕؆୯ʹͰ͖Δ • Ұఆ࣌ؒʹॲཧ͢ΔλεΫͷ਺ʢྫɿຖඵ100݅ɺຖ࣌50݅ʣ • ಉ࣌ʹॲཧ͢ΔλεΫͷ੍ݶɺࣦഊ࣌ͷ࠶࣮ߦ੍ޚͳͲ • ؆୯ͳઃఆϑΝΠϧʹΩϡʔΛఆٛ͢Δ total_storage_limit: 1T
 queue:
 - name: received-messages
 rate: 100/s RVFVFZBNM
  15. εέʔϥϏϦςΟ - Task Queue 15 • enqueue ͷํ๏ɿλεΫΛ࡞ͬͯΩϡʔʹ௥Ճ͢Δ • λεΫʹ͸ॲཧઌͱϖΠϩʔυΛࢦఆ

    t := taskqueue.NewPOSTTask("/worker/reply-message", url.Values{...})
 resultTask, err := taskqueue.Add(ctx, t, "received-message")
 if err != nil {
 return err
 } • dequeue ͷํ๏ɿ • Ωϡʔͷઃఆʹ΋ͱ͍ͮͯλεΫ͕ॲཧ͞Ε͍ͯ͘ • ্ͷ৔߹ POST ϦΫΤετͰ /worker/reply-message ͕ୟ͔ΕΔ • → λεΫ͸୯७ͳ HTTP ϦΫΤετʹͳΔ
  16. εέʔϥϏϦςΟ - Task Queue 16

  17. εέʔϥϏϦςΟ - ӬଓԽ 17 • ՝୊ɿաڈͷϝοηʔδͷ΍ΓͱΓͱൺ΂ͯฦ৴Λ͍ͨ͠ • → աڈͷ΍ΓͱΓΛอଘͯ͠ࢀরͰ͖Δඞཁ͋Γ •

    NoSQL σʔλϕʔεʮDatastoreʯΛ࢖͏ • ΩʔʹΑΔ CRUD • ෳ߹ΠϯσοΫε • ʢ੍໿ଟΊͷʣτϥϯβΫγϣϯ • ΫΤϦͳͲ
  18. εέʔϥϏϦςΟ - ӬଓԽ 18 type Message struct {
 Id int64


    SenderId int64
 Sender *User `datastore:"-"`
 Text string
 Unread bool
 Location Location
 CreatedTime time.Time
 }
 
 type Location struct {
 Lat float64
 Lng float64
 } • Datastore ʹอଘ͢ΔΤϯςΟςΟΛ
 ఆٛ͢Δ • ͍ͨͬͯී௨ͷߏ଄ମ • datastore λά͕෇͍͍ͯΔ • อଘ࣌ͷ໊લมߋ • อଘ͢Δ͔Ͳ͏͔ • ୯ҰߦΠϯσοΫεΛ෇͚Δ͔
  19. εέʔϥϏϦςΟ - ӬଓԽ 19 key := datastore.NewKey(ctx, "Message", "", msg.Id,

    nil)
 if _, err := datastore.Put(ctx, key, msg); err != nil {
 return err
 } • ϝοηʔδͷอଘ *% 4FOEFS*E 5FYU 6OSFBE -PDBUJPO-BU -PDBUJPO-OH $SFBUFE5JNF   ͜Μʹͪ͸ 536&      ͓ෲ͍ͨ͢ '"-4&   
  20. εέʔϥϏϦςΟ - ӬଓԽ 20 • ϝοηʔδͷऔಘ key := datastore.NewKey(c, "Message",

    "", id, nil)
 if err := datastore.Get(c, key, &msg); err != nil {
 return nil, err
 } q := datastore.NewQuery("Message")
 q = q.Filter("SenderId =", senderId).Order("-CreatedTime").Limit(10)
 var msgs []Message
 _, err := q.GetAll(c, &msgs) • ΫΤϦ
  21. εέʔϥϏϦςΟ - Datastore ͷಛ௃ 21 • Pros • ۃΊͯߴ͍εέʔϥϏϦςΟ •

    ӬଓԽͰ͖ΔσʔλʢΤϯςΟςΟʣͷ਺ʹ੍ݶ͸ͳ͍ • ӬଓԽσʔλ͕ͲΕ͚ͩ૿͑ͯ΋औಘͱ௥Ճͷ଎౓͸ྼԽ͠ͳ͍ • 1݅Ͱ΋10ԯ݅Ͱ΋ಉ͡ • ಉҰΤϯςΟςΟͰͳ͚Ε͹੍ݶͳ͠ͷฒྻॻ͖ࠐΈ • ۃΊͯߴ͍Մ༻ੑ • μ΢ϯλΠϜͳ͠ • Cons • BigTable ΑΓߴػೳͱ͸͍͑ϓϦϛςΟϒ • ݶఆ͞Εͨ৚݅Ͱ͔͠ ACID τϥϯβΫγϣϯ͕αϙʔτ͞Εͳ͍ • JOIN ͕ͳ͍ɺू໿ؔ਺͕ͳ͍ɺetc.
  22. ϩά෼ੳͱӡ༻

  23. ϩά෼ੳͱӡ༻ 23 • Web ΞϓϦΛެ։͢Ε͹ɺ
 ͦͷΞΫηεϩάΛݟͨΓɺ༷ʑͳσʔλ෼ੳΛ͢Δඞཁ΋͋Δ • Web ΞϓϦ͕ਖ਼ৗʹಈ͍͍ͯΔͷ͔Ͳ͏͔؂ࢹ΋͍ͨ͠ •

    → ΋ͪΖΜ GAEʢGCPʣ͚ͩͰରԠՄೳ
  24. ౷߹͞Ε͍ͯΔϦΞϧλΠϜͷϩάػೳ 24

  25. ౷߹͞Ε͍ͯΔϦΞϧλΠϜͷϩάػೳ - τϨʔε 25

  26. ౷߹͞Ε͍ͯΔϦΞϧλΠϜͷϩάػೳ - τϨʔε 26

  27. ౷߹͞Ε͍ͯΔϦΞϧλΠϜͷϩάػೳ - τϨʔε 27

  28. ϩά෼ੳͱӡ༻ 28 • ϦΫΤετʹͻ΋෇͍ͨΞϓϦέʔγϣϯϩάΛग़ྗ͢Δ log.Infof(ctx, "Read Message: id=%v", ids[i])
 


    log.Warningf(ctx, "missing Message entity: id=%v, err=%v", ids[i], err) ͦͷ··ϩάϏϡʔϫʹग़ྗ͞ΕΔ #JH2VFSZ΁ͷϦΞϧλΠϜૠೖ΋
 ઃఆϘλϯΛԡ͚ͩ͢
  29. ϩά෼ੳͱӡ༻ 29 ϩάͷݕࡧ৚݅Λࢦఆ ϑΟϧλ໊Λ෇͚Δ

  30. ϩά෼ੳͱӡ༻ 30 • ࡞ͬͨϩάϑΟϧλΛ࢖ͬͯɺ
 ৚݅Λ࡞੒ɻ
 ͖͍͠஋Λ௒͑ͨΒΞϥʔτ
 ʢϝʔϧɺSlack ͳͲ΁௨஌ʣ

  31. ·ͱΊ 31 1. Google App Engine / Go Ͱ
 ݱ࣮తͳ

    Web ΞϓϦΛ։ൃ͠ɺӡ༻͍ͯ͘͜͠ͱ͕Ͱ͖Δ
 2. ͦͯ͠ Go ʹΑΔ Web ΞϓϦ։ൃ͕ɺ
 ࠓޙ·͢·͋ͨ͢Γ·͑ʹͳ͍ͬͯ͘