Save 37% off PRO during our Black Friday Sale! »

GAE/Go の勘どころ

GAE/Go の勘どころ

golang.tokyo #5

C3d309cfa22d888d93a6187b786d1998?s=128

Osamu TONOMORI

April 27, 2017
Tweet

Transcript

  1. GAE/Go ͷצͲ͜Ζ @osamingo golang.tokyo #5 (2017/04/27)

  2. The golang.tokyo logo was made by Takuya Ueda. The Go

    gopher was designed by Renee French.
  3. Agenda 1. ࣗݾ঺հ 2. GAE ʹ͍ͭͯ 3. GAE ্Ͱͷ Go

    app ͷצͲ͜Ζ 4. ӡ༻͢Δͱ͖ͷ Tips 5. ·ͱΊ 3
  4. Agenda 1. ࣗݾ঺հ 2. GAE ʹ͍ͭͯ 3. GAE ্Ͱͷ Go

    app ͷצͲ͜Ζ 4. ӡ༻͢Δͱ͖ͷ Tips 5. ·ͱΊ 4
  5. ࣗݾ঺հ

  6. ۭલઈޙͷ͓͓͓͒ʂʂौ୩/࿡ຊ ໦ʹੜଉ͍ͯ͠ΔΰΥΥϑΝΝΝΝ 㷉ˑϖˑ㷊ʂʂʂ৽ن։ൃΛѪ͠ɺ ৽ن։ൃʹѪ͞Ε͓ͨͱ͒͜ʔʂʂ

  7. ;͚ͯ͟ɺ͢Έ·ͤΜͰͨ͠ɻ

  8. ࣗݾ঺հ • ओ৿ ཧ - Osamu TONOMORI • @osamingo •

    גࣜձࣾι΢κ΢ • 2016/08 ~ • Software engineer • Go ྺ • 2014/10 ~ • GAE/Go ͸ɺ2016/08 ~ 8
  9. ࣗݾ঺հ

  10. None
  11. None
  12. Agenda 1. ࣗݾ঺հ 2. GAE ʹ͍ͭͯ 3. GAE ্Ͱͷ Go

    app ͷצͲ͜Ζ 4. ӡ༻͢Δͱ͖ͷ Tips 5. ·ͱΊ 12
  13. GAE ʹ͍ͭͯ

  14. GAE ͱ͸ • Google App Engine • Google ͕΍͍ͬͯΔ PaaS

    ɻ • 2008 ೥͙Β͍͔Β͋ΔɻʢGCPͰ͸࠷ݹࢀ૊ʣ • Scaling Λௐ੔͢Ε͹ɺແྉͰ࢖͑Δɻ 14
  15. GAE ͱ͸ • Google App Engine • Google ͕΍͍ͬͯΔ PaaS

    ɻ • 2008 ೥͙Β͍͔Β͋ΔɻʢGCPͰ͸࠷ݹࢀ૊ʣ • Scaling Λௐ੔͢Ε͹ɺແྉͰ࢖͑Δɻ • Heroku ͱಉ͡ײ͡Ͱ৮Δͱ (ʋ´ω`) ͳײ͡ʹͳΔɻ • ಠࣗͷϧʔϧ΍ɺ֮͑Δ͜ͱ͕ଟ͍ɻ 15
  16. Standard and Flexible Environment • Standard Environment (SE) • GAE

    ͱ͍ͬͯࢦ͢ͷ͸ɺ͍͍ͩͨͬͪ͜ɻ • ݴޠ੍໿΍ɺಠࣗͷϧʔϧ͸ଟ͍͕͓खܰʹࢼͤΔɻ • Go, Python, Java, PHP • Flexible Environment (FE) • Cloud Next 2017 Ͱɺ GA ʹͳͬͨɻ • Docker ͳͷͰɺಛʹ੍໿͸ແ͍ɻ 16
  17. Flexible Environment Λ࢖͏৔໘ • ωοτϫʔΫىҼͷཁٻ͕͋Δ৔߹ • ݻఆ IP Ͱ֎෦઀ଓ͠ͳ͍ͱ͍͚ͳ͍ɻ •

    ಺෦ެ։ݶఆͷαʔϏε΁ͷΞΫηεɻ • Ұ࣌తͳσΟεΫॻ͖ࠐΈ͕ඞཁͳ৔߹ • ϑΝΠϧΛ࡞੒͍ͨ͠৔߹ͳͲɻ (tmpfs) 17
  18. GAE/Go (SE) Ͱͷز͔ͭͷ੍໿ • Go 1.6.4 • syscall, unsafe package

    ͸ɺجຊతʹ࢖͑ͳ͍ɻ • // +build !appengine ͷೝ஌ͷ௿͞… 18
  19. GAE/Go (SE) Ͱͷز͔ͭͷ੍໿ • Go 1.6.4 • syscall, unsafe package

    ͸ɺجຊతʹ࢖͑ͳ͍ɻ • // +build !appengine ͷೝ஌ͷ௿͞… • context.Context Λ࣋ͪӡΜͰߦ͘ɻ • context Λ࢖ͬͯ DB ΞΫηε౳Λߦ͏ɻ • ٯʹ͍͏ͱɺcontext ͕ແ͍ͱ΄΅Կ΋ग़དྷͳ͍ɻ 19
  20. GAE/Go (SE) Ͱͷز͔ͭͷ੍໿ • Go 1.6.4 • syscall, unsafe package

    ͸ɺجຊతʹ࢖͑ͳ͍ɻ • // +build !appengine ͷೝ஌ͷ௿͞… • context.Context Λ࣋ͪӡΜͰߦ͘ɻ • context Λ࢖ͬͯ DB ΞΫηε౳Λߦ͏ɻ • ٯʹ͍͏ͱɺcontext ͕ແ͍ͱ΄΅Կ΋ग़དྷͳ͍ɻ 20
  21. Go 1.8 ͕͘Δ͔΋

  22. None
  23. None
  24. ଴ͭͷ

  25. Agenda 1. ࣗݾ঺հ 2. GAE ʹ͍ͭͯ 3. GAE ্Ͱͷ Go

    app ͷצͲ͜Ζ 4. ӡ༻͢Δͱ͖ͷ Tips 5. ·ͱΊ 25
  26. GAE ্Ͱͷ Go app ͷצͲ͜Ζ

  27. DB ͬͯԿ࢖͏ͷʁ

  28. DB ͷબ୒ʹ͍ͭͯ • Cloud Datastore • NoSQL (Based BigTable) •

    GAE ͷ৔߹ɺجຊతʹ Datastore Λར༻͢Δɻ • มଶྗઃܭྗ͕ࢼ͞ΕΔɻ 28
  29. DB ͷબ୒ʹ͍ͭͯ • Cloud Datastore • NoSQL (Based BigTable) •

    GAE ͷ৔߹ɺجຊతʹ Datastore Λར༻͢Δɻ • มଶྗઃܭྗ͕ࢼ͞ΕΔɻ • Cloud SQL • Managed MySQL/PostgreSQL(β) • ౦ژϦʔδϣϯͷ Latency ͕Ϋι໰୊΋ղܾࡁΈɻ 29
  30. Cloud Datastore ͷ৔߹ • ެࣜͷ datastore ύοέʔδΛ௚Ͱ͸࢖Θͳ͍ɻ • ඞͣ wrap

    ͯ͠࢖ͬͨํ͕Α͍ɻ • interface, validation ͳͲͪΌΜͱ΍Γ·͠ΐ͏ɻ • goon ͱ͔΋͋ΔͷͰɺࢀߟʹ͢ΔͱΑ͍ɻ 30
  31. Cloud Datastore ͷ৔߹ • ެࣜͷ datastore ύοέʔδΛ௚Ͱ͸࢖Θͳ͍ɻ • ඞͣ wrap

    ͯ͠࢖ͬͨํ͕Α͍ɻ • interface, validation ͳͲͪΌΜͱ΍Γ·͠ΐ͏ɻ • goon ͱ͔΋͋ΔͷͰɺࢀߟʹ͢ΔͱΑ͍ɻ • ͨͩɺઃܭ͕㙽ʹ֯೉͍͠ɻ 31
  32. None
  33. None
  34. Cloud SQL ͷ৔߹ • ͳΜ΍͔Μ΍ී௨ʹ࢖͑·͢ɻ • sql ύοέʔδͰ઀ଓ؅ཧ͢ΔͷͰɺྑ͠ͳʹɻ 34

  35. Cloud SQL ͷ৔߹ • ͳΜ΍͔Μ΍ී௨ʹ࢖͑·͢ɻ • sql ύοέʔδͰ઀ଓ؅ཧ͢ΔͷͰɺྑ͠ͳʹɻ • ͱ͸͍͑ɺ࢖͏લʹ஫ҙ఺͕͋Δ…

    35
  36. None
  37. None
  38. ಉ࣌઀ଓ਺͕࠷େ 12 ݸ

  39. ಉ࣌઀ଓ਺͕࠷େ 12 ݸ (Master, Slave ͷ߹ܭ)

  40. sql.DB_SetMaxOpenConns(n int)

  41. Vendoring ͬͯͲ͏ͳͷʁ

  42. Vendoring ʹ͍ͭͯ • GOPATH ͕ɺϓϩδΣΫτϕʔεʹͳΔɻ 42

  43. Vendoring ʹ͍ͭͯ • GOPATH ͕ɺϓϩδΣΫτϕʔεʹͳΔɻ • vendor σΟϨΫτϦͱ͔ɺ࢖͑ͳ͍ɻ • dep,

    glide, gvt ͳͲݢฒΈ࢖͑·ͤΜɻ 43
  44. Vendoring ʹ͍ͭͯ • GOPATH ͕ɺϓϩδΣΫτϕʔεʹͳΔɻ • vendor σΟϨΫτϦͱ͔ɺ࢖͑ͳ͍ɻ • dep,

    glide, gvt ͳͲݢฒΈ࢖͑·ͤΜɻ • ےྗͰΧόʔʂʂ 44
  45. Vendoring ʹ͍ͭͯ • GOPATH ͕ɺϓϩδΣΫτϕʔεʹͳΔɻ • vendor σΟϨΫτϦͱ͔ɺ࢖͑ͳ͍ɻ • dep,

    glide, gvt ͳͲݢฒΈ࢖͑·ͤΜɻ • ےྗͰΧόʔʂʂ • ϓϩδΣΫτϕʔεͷґଘ؅ཧ 45
  46. None
  47. gb ʹΑΔvendoring • gb vendor ίϚϯυʹΑΔґଘ؅ཧ • fetch, update, purge

    ౳ͻͱ௨ΓͰ͖Δɻ 47
  48. gb ʹΑΔvendoring • gb vendor ίϚϯυʹΑΔґଘ؅ཧ • fetch, update, purge

    ౳ͻͱ௨ΓͰ͖Δɻ • ͨͩɺinit ͚ͩαϒίϚϯυ͕ແ͍ͷͰɺےྗ 48
  49. gb ʹΑΔvendoring • gb vonder ίϚϯυʹΑΔґଘ؅ཧ • fetch, update, purge

    ౳ͻͱ௨ΓͰ͖Δɻ • ͨͩɺinit ͚ͩαϒίϚϯυ͕ແ͍ͷͰɺےྗ 49 HPMJTUGb\\SBOHF*NQPSUT^^\\^^\\FOE^^\\SBOHF5FTU*NQPSUT^^\\^^ \\FOE^^ 71"5) cUSaOcTPSUcVOJRcHSFQ& a DPNcPSHc JO  cYBSHTO*\^TIDHCWFOEPSGFUDI\^
  50. gb ʹΑΔ GOPATH ؅ཧ • gb gae ίϚϯυʹΑΔ GOPATH ͷ؅ཧ

    • test, serve, deploy ͷ GOPATH Λղܾͯ͘͠ΕΔɻ • ެࣜఏڙͰ͸ແ͍ͷͰɺผ్Πϯετʔϧ͢Δɻ • go get -u github.com/PalmStoneGames/gb-gae 50
  51. ConfigurationͬͯͲ͏ͳͷʁ

  52. Configuration ʹ͍ͭͯ • The Twelve-Factor App - https://12factor.net/ • The

    twelve-factor app stores config in environment variables. 52
  53. Configuration ʹ͍ͭͯ • The Twelve-Factor App - https://12factor.net/ • The

    twelve-factor app stores config in environment variables. • GAE ΋ಉ͡ࢥ૝ͳͷͰɺ؀ڥม਺ʹೖΕ·͠ΐ͏ɻ 53
  54. Configuration ʹ͍ͭͯ • Test ͷͱ͖͸ɺͲ͏͢Δͷ͔ʁ 54

  55. Configuration ʹ͍ͭͯ • Test ͷͱ͖͸ɺͲ͏͢Δͷ͔ʁ • ં֯ Go ࢖ͬͯΔΜͩ͠ɺ direnv

    ࢖͓͏ɻ • ./test/.envrc ʹςετ༻ͷ؀ڥม਺Λઃஔ͢Δɻ 55 EJSFOWFYFDUFTUHCHBFUFTUTSDʜ
  56. Agenda 1. ࣗݾ঺հ 2. GAE ʹ͍ͭͯ 3. GAE ্Ͱͷ Go

    app ͷצͲ͜Ζ 4. ӡ༻͢Δͱ͖ͷ Tips 5. ·ͱΊ 56
  57. ӡ༻͢Δͱ͖ͷ Tips

  58. Deploy ͬͯͲ͏ͳͷʁ • Traffic migration Λ࢖͏ͱɺ͍Ζ͍ΖͰ͖Δɻ • Blue/Green deployment, Canary

    Release 58
  59. Deploy ͬͯͲ͏ͳͷʁ • Traffic migration Λ࢖͏ͱɺ͍Ζ͍ΖͰ͖Δɻ • Blue/Green deployment, Canary

    Release • context Λ࢖ͬͨࣄલॲཧ 59
  60. Deploy ͬͯͲ͏ͳͷʁ • Traffic migration Λ࢖͏ͱɺ͍Ζ͍ΖͰ͖Δɻ • Blue/Green deployment, Canary

    Release • context Λ࢖ͬͨࣄલॲཧ 60
  61. Monitoring ͬͯͲ͏ͳͷʁ • Stackdriver ͕ɺͻͱ௨Γʹͳͬͯ͘ΕΔɻ • http status code, datastore,

    taskqueue etc.
  62. Monitoring ͬͯͲ͏ͳͷʁ • Stackdriver ͕ɺͻͱ௨Γʹͳͬͯ͘ΕΔɻ • http status code, datastore,

    taskqueue etc. • Error log ؂ࢹ΋ɺStackdriver Logging ͱ࿈ܞͰ͖Δɻ • Slack ࿈ܞ΋ϥΫϥΫ
  63. Agenda 1. ࣗݾ঺հ 2. GAE ʹ͍ͭͯ 3. GAE ্Ͱͷ Go

    app ͷצͲ͜Ζ 4. ӡ༻͢Δͱ͖ͷ Tips 5. ·ͱΊ 63
  64. ·ͱΊ • …ʹೖΔલʹ Release Notes ͷ֬ೝ IUUQTDMPVEHPPHMFDPNBQQFOHJOFEPDTTUBOEBSEHPSFMFBTFOPUFT

  65. ·ͱΊ • Go 1.8 ͸΍͍͘͜ʂʂʂ

  66. Thanks for your attention.