共通基盤システムの開発について

 共通基盤システムの開発について

http://aiming.connpass.com/event/25004/ での発表資料です

Cc81dc2acad7b0d070a2f2d3f10eef7d?s=128

Tsukuru Tanimichi

January 27, 2016
Tweet

Transcript

  1. ڞ௨ج൫γεςϜͷ։ൃʹ͍ͭͯ גࣜձࣾ Aiming ιϑτ΢ΣΞΤϯδχΞ ୩ಓ ૑ 2016/01/27 Aiming Study #8

  2. ࣗݾ঺հ • ୩ಓ ૑ (Tsukuru Tanimichi) • @ttanimichi • Ruby,

    Java, DevOps • 2014/06 ೖࣾ • ౷߹ KPI γεςϜͷ։ൃ • ڞ௨ج൫γεςϜͷ։ൃ • ήʔϜλΠτϧͷ API ։ൃ • https://github.com/ttanimichi • https://twitter.com/ttanimichi
  3. Aiming ͷήʔϜλΠτϧ 10 λΠτϧ͋Γ·͢

  4. ͦΕͧΕͷήʔϜλΠτϧ͕

  5. ·ͬͨ͘ผݸͷγεςϜ ҟͳΔωοτϫʔΫ

  6. ݸʑͷήʔϜ͸ಠཱͨ͠γεςϜ ྫ) ͙Δ͙ΔΠʔάϧͷωοτϫʔΫ ී௨ͷ Web ΞϓϦέʔγϣϯʹ͔ͳΓ͍ۙߏ੒ ϩʔυόϥϯα app app app

    MySQL Redis
  7. ήʔϜλΠτϧΛ࡞Δͨͼʹ ຖճඞཁʹͳΔ΍ͭ

  8. • ೝূ • ՝ۚॲཧ • νϟοτ • Push ௨஌ •

    ૬ޓૹ٬ • ͳͲͳͲ
  9. ॎׂΓͰͦΕͧΕ࣮૷͍ͯͨ͠ɾɾɾ • ೝূ • ՝ۚॲཧ • νϟοτ • Push ௨஌

    • ೝূ • ՝ۚॲཧ • Push ௨஌ • ೝূ • ՝ۚॲཧ • νϟοτ • Push ௨஌
  10. ʊਓਓਓਓਓਓਓਓʊ ʼɹंྠͷ࠶ൃ໌ɹʻ ʉY^Y^Y^Y^Y^Y^Yʉ

  11. • ଞͷήʔϜλΠτϧͷϞδϡʔϧΛίϐϖ ͯ͠࡞ͬͯ͠·͍ͬͯΔ৔߹΋͋Δ • ιʔείʔυΛಡΜͰ͍ΔͱଞͷήʔϜλ ΠτϧͷλΠτϧ໊͕ಥવग़͖ͯͨΓ • ͋·Γग़དྷͷྑ͘ͳ͍Ϟδϡʔϧ΍ઃܭ͕ ίϐϖͰൟ৩͢Δ

  12. ͦΕʹՃ͑ͯɺ

  13. • ϩετϨΨϦΞ • 2015೥10݄22೔ϦϦʔε • ӡӦݩ: גࣜձࣾ Aiming • ։ൃݩ:

    Perfect World Co.,Ltd
  14. • ϩετϨΨϦΞ • 2015೥10݄22೔ϦϦʔε • ӡӦݩ: גࣜձࣾ Aiming • ։ൃݩ:

    Perfect World Co.,Ltd
  15. • ϩετϨΨϦΞ • 2015೥10݄22೔ϦϦʔε • ӡӦݩ: גࣜձࣾ Aiming • ։ൃݩ:

    Perfect World Co.,Ltd ։ൃݩ͸தࠃͷ Perfect World ࣾ
  16. தࠃͷήʔϜλΠτϧΛ ೔ຊͰϦϦʔε͢Δ৔߹ɺ ೝূͱ՝ۚͷॲཧʹ͍ͭͯ͸ ͪ͜ΒͰ༻ҙ͢Δඞཁ͕͋Δ

  17. Ͳ͏͍͏͜ͱʁ • தࠃ͸ GooglePlay Store ͡Όͳ͍ • தࠃಠࣗͷετΞʹܨ͕ΔΑ͏ʹͳ͍ͬͯΔ • ͦͷลͷ࢓૊Έ͸ӡӦݩ͕༻ҙ͢ΔจԽ

    • SNS ΞΧ΢ϯτͱͷඥ෇͚ʢޙड़ʣ • Facebook, Twitter ͸தࠃ͔Βܨ͕Βͳ͍ • άϨʔτϑΝΠΞ΢Υʔϧ
  18. ڞ௨ج൫γεςϜ࡞Δͧʂʂ

  19. ڞ௨ج൫γεςϜΛ࡞ͬͨܦҢ·ͱΊ • ήʔϜλΠτϧؒͰॏෳ͢ΔϞδϡʔϧͷ ଟॏ։ൃΛ΍Ί͔ͨͬͨ • தࠃͷήʔϜλΠτϧΛ೔ຊͰϦϦʔε
 ͢ΔͨΊʹೝূͱ՝ۚͷॲཧΛ༻ҙ͢Δ
 ඞཁ͕͋ͬͨ

  20. ࠓճͷ Scope

  21. • ೝূ • ՝ۚ • νϟοτ • Push ௨஌ •

    ૬ޓૹ٬ • ͳͲͳͲ
  22. • ೝূ • ՝ۚ • νϟοτ • Push ௨஌ •

    ૬ޓૹ٬ • ͳͲͳͲ ← ࡞Γ·ͨ͠ ← ࡞Γ·ͨ͠
  23. ཁ݅

  24. ೝূ • ΞΧ΢ϯτΛൃߦͰ͖Δ • SNS ΞΧ΢ϯτͱඥ෇͚Ͱ͖Δ • ػछมߋͳͲΛͨ͠ͱ͖ʹ SNS ΞΧ΢ϯ

    τ͔ΒήʔϜσʔλΛ෮ݩͰ͖Δ
  25. ՝ۚ • Ϣʔβʔʹੜ೥݄Λೖྗ͍ͤͨ͞ • ະ੒೥͕՝ۚ͠Α͏ͱͨ͠ͱ͖ʹ͸อޢऀ ͷ͓ΏΔ͠ΛಘΔΑ͏ද͍ࣔͨ͠ • Ϩγʔτͷ verify ͳͲνʔτରࡦ͕͍ͨ͠

    ʢ͍͕ͭ͜Ұ൪େมʂʣ
  26. ڞ௨ج൫γεςϜͷ։ൃ

  27. ϝϯόʔߏ੒ • औΓ·ͱΊʢ1໊ʣ • αʔόʔʢ1໊ʣ← ࢲ͕୲౰ • iOSΤϯδχΞʢ1໊ʣ • Android

    ΤϯδχΞʢ1໊ʣ ͳͷͰࠓ೔͸αʔόʔͷ࿩Λ͠·͢
  28. ։ൃظؒ • 7,8 ϲ݄ؒ͘Β͍ • 2015೥9݄ v1.0 ϦϦʔε

  29. νʔϜϏϧσΟϯά • ࣾ಺ͷΫϥΠΞϯταΠυͷΤϯδχΞʹ͸ Unity(C#) ͕ଟ͍ɻiOS(Objective-C) ΍ Android(Java) Λॻ͚Δਓ͕গͳ͍ • ۀ຿ҕୗͷ

    iOS, Android ΤϯδχΞΛޏͬͨ • ۀ຿ҕୗͷΤϯδχΞʹ GitHub ͷ࢖͍ํ΍ ΞδϟΠϧ։ൃͷϑϩʔΛ·֮ͣ͑ͯ΋Β͏
  30. • Ruby 2.2 • Ruby on Rails 4.2 • MySQL

    • nginx
  31. • CircleCI • Slack • GitHub • Ansible • serverspec

  32. iOS • CocoaPods • Objective-C Android • Gradle • Android

    Studio
  33. Ͳ͏͍͏࡞Γʹ͔ͨ͠

  34. Α͋͘Δڞ௨ج൫γεςϜͷ࡞Γ • αʔϏεͱڞ௨ج൫Λͻͱͭͷ mBaaS ͱͯ͠࡞Δ • ڞ௨ج൫γεςϜ্ʹ֤αʔϏεΛ૊ΈࠐΉ ໰୊఺ • ڞ௨ج൫γεςϜͱήʔϜλΠτϧͷґଘؔ܎

    • ಛఆͷϓϩάϥϛϯάݴޠʹશαʔϏε͕ϩοΫΠϯ • ݹ͍όʔδϣϯͷݴޠॲཧܥͷར༻Λڧ੍͞ΕΔ
  35. ࣌୅͸ Microservices

  36. Network ڞ௨ج൫ γεςϜ ήʔϜ αʔόʔ HTTPS (JSON)

  37. • ֤ήʔϜλΠτϧͷωοτϫʔΫ಺ʹ
 ڞ௨ج൫γεςϜΛσϓϩΠ • HTTPS (JSON)Ͱ΍ΓͱΓ͢Δ෼ࢄγεςϜ • ήʔϜαʔόʔ͸ڞ௨ج൫γεςϜͷ
 ϚχϡΞϧʹै͍ϦΫΤετΛૹड৴ Network

    ڞ௨ج൫ γεςϜ ήʔϜ αʔόʔ HTTPS (JSON)
  38. • Objective-C, Java Ͱ 
 ࡞ͬͨڞ௨ج൫
 ϥΠϒϥϦΛϥοϓͯ͠ Unity Plugin ʹ͢Δ

    • ΫϥΠΞϯτଆʹ͸
 ڞ௨ج൫γεςϜ༻ͷ Unity Plugin Λ૊ΈࠐΉ • αʔόʔͱ HTTPS Ͱ௨৴ ڞ௨ج൫ γεςϜ HTTPS (JSON) Client Unity Plugin
  39. Network ήʔϜ αʔόʔ ڞ௨ج൫ γεςϜ • ڞ௨ج൫ͱήʔϜ͸
 ผʑͷݴޠͰ΋͍͍ • IaaS

    ͷো֐ͰҰํ͚ͩ
 མͪΔ͜ͱ͕ͳ͍Α͏ಉҰ ͷωοτϫʔΫ಺ʹஔ͘ Client Unity Plugin
  40. ϑϩʔ

  41. ৽نొ࿥ ᶃ ΞΧ΢ϯτൃߦ Client Unity Plugin ڞ௨ج൫ γεςϜ ήʔϜ αʔόʔ

  42. ϩάΠϯ ήʔϜ αʔόʔ ᶄ ΞΧ΢ϯτΛ໰͍߹ΘͤΔ ᶃϩάΠϯΛϦΫΤετ ᶅ Ϩεϙϯε Client Unity

    Plugin ڞ௨ج൫ γεςϜ
  43. SNS ΞΧ΢ϯτͱͷඥ෇͚ ڞ௨ج൫ γεςϜ ᶄ αʔόʔʹૹΔ Twitter Facebook google+ ᶃ

    access_token Λऔಘ ᶅ SNS ͷ uid Λऔಘ ΞΧ΢ϯτͱ uid Λඥ͚ͮΔ Client Unity Plugin
  44. ػछมߋ࣌ͳͲͷҾ͖ܧ͗ ᶅ SNS ͷ uid Λऔಘ SNS ͷ uid ͔ΒΞΧ΢ϯτΛऔಘ

    → ͦͷΞΧ΢ϯτΛ࢖͏ ڞ௨ج൫ γεςϜ ᶄ αʔόʔʹૹΔ Twitter Facebook google+ ᶃ access_token Λऔಘ Client Unity Plugin
  45. # app/models/account.rb class Account < ActiveRecord::Base has_many :connections, dependent: :destroy

    has_many :orders, class_name: 'Purchase::Order' before_create do self.auth_id = SecureRandom.uuid self.auth_token = SecureRandom.hex end def already_connected_with?(provider) connections.find_by(provider: provider).present? end def overwrite_connection(provider:, provider_uid:) connections.find_by!(provider: provider).update!(provider_uid: provider_uid) end def register_birth_date(year, month) update!(birth_date: build_birth_date(year, month)) end def child? become_adult_day.future? end private def become_adult_day birth_date + 20.years + 1.month end
  46. ՝ۚͷϑϩʔ

  47. τϥϯβΫγϣϯIDൃߦ ήʔϜ αʔόʔ ڞ௨ج൫ γεςϜ ᶃ τϥϯβΫγϣϯIDൃߦ Client Unity Plugin

  48. ߪೖͱϨγʔτͷडཧ Client Unity Plugin ڞ௨ج൫ γεςϜ ᶄϨγʔτૹ৴ AppStore GooglePlay τϥϯβΫγϣϯID

    Λ
 ɾapplicationUsername (iOS)
 ɾdeveloper payload (Android) ʹ٧ΊͯɺߪೖΛ࣮ߦ
 ͜͏͓ͯ͘͠ͱϨγʔτͱҰॹʹ τϥϯβΫγϣϯ ID ͕ฦͬͯ͘Δ → ࠶Πϯετʔϧͯ͠΋อ࣋͞ΕΔ ᶃ ߪೖ
  49. Verify ͱৼΓࠐΈ ήʔϜ αʔόʔ ڞ௨ج൫ γεςϜ AppStore ᶃ Ϩγʔτͷݕূ ᶄৼࠐ

    iOS ͸ Apple ͷαʔόʔͰ verify
 Android ͸खݩͰ verify Ͱ͖Δ verify Ͱ͖ͨΒΞΠςϜΛৼࠐΉ
  50. ՝ۚͷνʔτରࡦ աڈʹฐ͕ࣾܦݧͨ͠νʔτํ๏͢΂ͯʹରࡦ • ِ෺ͷϨγʔτΛૹΓ͚ͭͯ͘Δ • ຊ෺ͷϨγʔτ͚ͩͲطʹৼࠐࡁΈͷϨγʔτ • ຊ෺ͷϨγʔτ͚ͩͲଞͷΞϓϦͷϨγʔτ ΛϚϧνϓϩηεલఏͰରࡦ •

    τϥϯβΫγϣϯΛऔͬͯߦϩοΫͰഉଞ੍ޚ
 ʢActiveRecord ͷ `#with_lock` Λ࢖ͬͨʣ
  51. ݁ߏ͠ΜͲ͔ͬͨ͆

  52. • ͍͕ͭ͜όάͬͯͨΓରࡦ͕͋·͔ͬͨΓ ͢Δͱ࠾༻͍ͯ͠Δࣾ಺ͷશήʔϜλΠτ ϧ͕νʔτ͞ΕΔ • νʔτରࡦɺ࣮ࡍʹίʔσΟϯάͯ͠ΈΔ ͱ͚ͬ͜͏ߟ͑ͳ͍ͱ͍͚ͳ͍͜ͱ͕ଟ͍ • Ϛϧνϓϩηεલఏ •

    ฒྻॲཧͬͯͦ΋ͦ΋೉͗͢͠ΔΑͶ
  53. Gemfile ެ։ͪ͠Ό͍·͢ https://gist.github.com/ttanimichi/5f63897d746b7491fbf7

  54. • gem “venice” • iOS Ϩγʔτͷ verify Λ΍ͬͯ͘ΕΔ gem •

    iOS 7ͷ৽ϨγʔτܗࣜʹରԠ͔ͨͬͨ͠
 ͷͰ fork ൛ͷ “amoslanka/venice” Λ࢖ͬͨ • ສ͕Ұফ͞ΕΔͱࠔΔͷͰ fork ͠ͱ͍ͨ ศརͩͬͨ gem
  55. # app/models/purchase/receipt/app_store.rb module Purchase module Receipt class AppStore def initialize(raw_receipt:,

    bundle_id:, store_transaction_id:) @raw_receipt = raw_receipt @bundle_id = bundle_id @store_transaction_id = store_transaction_id end def verify verify_receipt fail_unless_correct_bundle_id build_result end private def verify_receipt begin @receipt ||= Venice::Receipt.verify!(@raw_receipt).to_h rescue Venice::Receipt::VerificationError => e handle_error(e.code, e.message) end end def fail_unless_correct_bundle_id unless @bundle_id == receipt[:bundle_id] fail Incorrect, "bundle_id is incorrect" end end
  56. ։ൃ্ͷϙΠϯτ

  57. ΞϓϦέʔγϣϯαʔόʔͷબఆ ΞʔΩςΫνϟ unicorn ϓϩηεϕʔε fork Λ࢖ͬͨ master-slave Puma ϚϧνεϨου Thin

    Πϕϯτۦಈ EventMachine ʹґଘ
  58. ΞϓϦέʔγϣϯαʔόʔͷબఆ • ࠷ॳ͸Կ΋ߟ͑ͣʹ unicorn Λ࢖͍ͬͯͨ • ՝ۚͷΤϯυϙΠϯτʹͪΐͬͱෛՙΛ͔͚ ͯΈΔͱ٧·Δ • ਺ेඵ΄Ͳ଴ͭͱϨεϙϯε͕ฦͬͯ͘Δ

  59. verify ͷͨΊʹ Apple ͷαʔόʔʹϦΫΤετΛ ౤͍͛ͯͯɺ600ms ΄ͲϒϩοΫ͞Εͯ͠·͏
 → ෛՙ͕͔͔Δͱ͢΂ͯͷ Worker ͕٧·Δ

  60. ΞϓϦέʔγϣϯαʔόʔͷબఆ iOS ͷϨγʔτΛ verify ͢ΔαʔόʔͷΑ͏ ͳ஗͍ॲཧ (ϦϞʔτHTTP ϦΫΤετͱ͔) ΛؚΉΤϯυϙΠϯτͷ৔߹ɺunicorn ͷΑ͏

    ͳϓϩηεϕʔεͷΞϓϦέʔγϣϯαʔόʔ Λ࢖͏΂͖Ͱ͸ͳ͍
  61. ΞϓϦέʔγϣϯαʔόʔͷબఆ • unicorn ͸ߴ଎͕ͩɺϓϩηεϕʔεͷͨΊ
 ಉ͡ϝϞϦʔྔͰ༻ҙͰ͖Δ Worker ਺͸
 εϨουϕʔεͷ Puma ͷ΄͏͕ଟ͍

    • Puma ʹมߋ • ຊ౰͸Πϕϯτۦಈͷ Thin ͕ϕετͩͬͨ ͷ͔΋ɻPuma Ͱे෼ͳੑೳ͕ग़͔ͨΒ Thin ͸ݕূ͠ͳ͔ͬͨ
  62. iOS ͷ applicationUsername • ࠶Πϯετʔϧͨ͠৔߹Ͱ΋τϥϯβΫγϣ ϯIDΛอ࣋Ͱ͖ΔΑ͏ɺ঎඼ͷߪೖ࣌ʹ applicationUsername ʹڞ௨ج൫γεςϜͷ τϥϯβΫγϣϯID Λ٧Ίͨ

    • ͕ɺͳ͔ͥ production ͩͱ100 ݅ʹ1݅͘Β ͍ applicationUsername ʹ٧Ίͨσʔλ͕ ਧͬඈΜͰ Null Ͱฦͬͯ͘Δɻɻ
  63. Apple ༷…

  64. ϦϦʔεͯ͠Έͯࢥͬͨ͜ͱ

  65. ಛʹ՝ۚ·ΘΓͷॲཧΛ ڞ௨ԽͰ͖ͨͷ͸ྑ͔ͬͨ • νʔτରࡦͱ͔ • όά͕ͳ͍Α͏ʹ࡞Δͷ͸ਆܦΛ࢖͏ • ݸผʹ࣮૷ͨ͘͠ͳ͍ • ձࣾͱͯ͠౷Ұ͞Ε͍ͯΔ΂͖

  66. Կ΋ߟ͑ͳͯ͘΋ձࣾͱͯ͠ͷ ΨΠυϥΠϯʹै͑Δͷ͸ྑ͍ • ೥ྸ֬ೝ·ΘΓͱ͔ • ੜ೥݄Λೖྗͤͯ͞ɺະ੒೥ʹ͸֬ೝ • ͜͏͍͏ͷ͸ձࣾͱͯ͠ͷΨΠυϥΠϯ͕ ༻ҙ͞Ε͍ͯΔ •

    ڞ௨ج൫γεςϜΛ࢖͏͚ͩͰΨΠυϥΠ ϯʹଇͬͱΕΔ
  67. SNS ΞΧ΢ϯτͰҾ͖ܧ͗ • ࠓճɺҾ͖ܧ͗ͷཧ૝ܗΛݕ౼ͨ͠ • ڞ௨ج൫γεςϜΛ࢖͏͚ͩͰձࣾͱͯ͠ ཧ૝ܗͱߟ͑ΔҾ͖ܧ͗ॲཧΛಋೖͰ͖Δ • ௿ίετͰߴ͍ϢʔβʔମݧΛఏڙͰ͖Δ

  68. ࠓޙʹ͍ͭͯ

  69. • ೝূ • ՝ۚ • νϟοτ • Push ௨஌ •

    ૬ޓૹ٬ • ͳͲͳͲ ͜ͷลΛਐΊ͍͖͍ͯͨ
  70. • ೝূ • ՝ۚ • νϟοτ • Push ௨஌ •

    ૬ޓૹ٬ • ͳͲͳͲ ͪ͜Βʹؔͯ͠΋ ػೳ௥Ճͷ ཁ๬͕Ͱ͍ͯΔ
  71. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠