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

如何给 Rails 应用减肥 - 薄荷微服务化实践

26631f3b69f8975167d9475014494b06?s=47 Vincent Xie
September 23, 2016

如何给 Rails 应用减肥 - 薄荷微服务化实践

一个庞大臃肿的 Monolith Rails 应用面临诸多挑战,微服务化是给 Rails 应用减肥的一种有效手段,但是微服务化并非 Rails 的主菜,所以过程困难重重。另外微服务化并非银弹,它同时带来很多额外的问题,如何选择和取舍?如何应对其中的关键问题?这个 slide 分享薄荷在微服务化实践过程中的经验教训。

26631f3b69f8975167d9475014494b06?s=128

Vincent Xie

September 23, 2016
Tweet

Transcript

  1. ই֜ᕳ Rails ଫአٺᙑ 
 ᡀរங๐ۓ۸ਫ᪢ ᨀ෈শ @2016.9.23

  2. ىԭ౯޾ᡀរ • ىԭ౯ • vincent @RubyChina • xiewenwei @Github •

    ىԭᡀរ • ؋଼ᶾऒᑏۖ԰ᘶᗑلݪ • 2007 ଙ୏তֵአ Ruby
  3. ൉ᕐ • Rails ଫአጱங๐ۓ۸ • ਫෞங๐ۓ۸ጱىᲫᅩ • ங๐ۓ۸ጱ๦ᤍݐᛣ

  4. Rails ଫአெԍݰ౮ᙪৼ

  5. Monolith Rails ଫአᶎԁጱ೴౴ • ॔๥҅ᇘӞݎۖق᫝҅ᵙզᖌಷ޾ಘ઀ • ᛎᙠ҅ၥᦶౌ҅ސۖౌ҅ᩒრၾᘙ૤य़ • ੒ෛےفጱૡᑕ૵ຄଶӧ݋অ

  6. ங๐ۓ۸რᛔ Java Ӯኴ҅SOA ጱᄍᬰ҅ᇿᒈ ୏ݎ޾᮱ᗟ҅ᘶݳᕟ౮Ӟӻᔮᕹ

  7. ங๐ۓ۸ጱঅ॒ • ฃԭቘᥴ޾ᖌಷ҅ྯӞӻৼ๐ۓ᮷ӧᬦړ॔๥ • ᇿᒈ୏ݎ޾ၥᦶ҅ݢᭌӧݶದ๞຾҅ฃԭಘ઀ • ᇿᒈ᮱ᗟ҅ๅঅֵአᩒრ҅ๅঅጱ֐ᖽᚆێ

  8. “The Only Alternative to a Rails Monolith are Micro Services?

    Bullshit!” by DHH
  9. “౯ԅԧᭈ᭿ J2EE ಍᫨ Rails ጱ҅ᬯԶӳᥜྋ಩ Rails ݒ౮ݚक़Ӟӻ J2EE̶”

  10. Rails מ๵ • ᯿ᥤෆݳᔮᕹ҅Ԇ݀ᔜᭌ • ग़ز۸ጱᦡᦇཛྷୗ҅۱਻ଚ᯿ • ᑕଧާጱଛᐰ๋य़۸…

  11. ইຎӧங๐ۓ۸҅ݢզ؉ԶՋԍ

  12. ᯿ෛᕟᕢፓ୯޾෈կ • concerns • namespace • folder # Custom autoloadable

    directories. config.autoload_paths += Dir["#{config.root}/app/models/[a-z]*s/"]
  13. ୚ف๐ۓ੶ ServiceObject • MVC + Service Layer • ॔๥ጱ҅᪜ग़ӻ Model

    ጱӱۓ᭦ᬋ҅ྲই RegisterService҅CreateOrderService • चᏐᦡෞ๐ۓ҅ྲই ShortMessageService
  14. ServiceObject ᐏֺ class TrackEventService include Serviceable def initialize(user_key, event_name, params)

    #... end def call #... end private #... end ᭿عֵአᔄොဩֵ҅አ Object ݢզ ֵአ OOP ಋဩᬰᤈ᯿຅޾ಘ઀
  15. ServiceObject ᐏֺ module Serviceable extend ActiveSupport::Concern module ClassMethods def call(*args)

    new(*args).call end end end
  16. ہᐶ gem • ӱۓ෫ىጱ gem҅ই string/number/date utils ᔄ • ӱۓوՁጱ

    gem҅ই weight_model҅rule_engine • ॔አ models, controllers ޾ viewsֵ҅አ Rails Engine
  17. ሿਫӾݢᚆጱங๐ۓ۸

  18. ங๐ۓ۸୚فጱᳯ᷌ • ᪜๐ۓහഝᦢᳯ • ړ૲ୗ॒ቘ҅ইԪۓ̵਻Კ޾᧣ᦶᒵᒵ • ᬩᖌ॔๥ଶ

  19. ਫෞங๐ۓ۸ጱىᲫᅩ • ங๐ۓښړ • ᭗מ๢ګ • ங๐ۓလቘ

  20. ᡀរጱ๐ۓښړ ᡀរ ᨴಁ ၾ௳ վ֎ ᨻᇔ ᦕ୯ ๐ۓ ᷣᇔ ᦇښ

  21. ই֜ښړ - ᥯ଶ • ೲ௔ᨶ҅ᇿᒈचᏐ๐ۓ • ೲӱۓ҅ೲԾߝ • ೲࢫᴚ -

    ଼শਧ஌
  22. ই֜ښړ - ᔉଶ • ᔋᕡᭇӾ҅ॡᔋॡᕡ᮷ํஉग़ᳯ᷌ • ᡀរጱֺৼ ౯ᦊԅ 10 ӻૢݦྲ᫾ݳᭇ

  23. ই֜ଘჶද᭜ • Ḓضہᐶᨴಁᔮᕹ passport҅ള፳ฎचᏐ๐ۓ • ᆐݸෛӱۓսض • ଚਂᬩᤈ҅ଘჶᬢᑏ

  24. ங๐ۓᳵጱ᭗מ๢ګ • ᭗מཛྷୗ • ፗള᧣አғREST vs RPC • चԭၾ௳ጱ୑ྍ᭗מ

  25. ங๐ۓᳵଉᥠ᭗מཛྷୗ Ӟ੒Ӟ Ӟ੒ग़ ݶྍ ᧗࿢/ߥଫ — ୑ྍ ᭗Ꭳ ݎ૲/ᦈᴅ

  26. REST vs RPC REST RPC սᅩ ᓌܔ҅౮ᆧ҅᧣ᦶො׎ ṛප ᗌᅩ ௔ᚆ֗

    ፘ੒॔๥҅ඪ೮੝
  27. चԭၾ௳ጱ୑ྍ᭗מ • ୚فၾ௳Ӿᳵկই RabbitMQ … • սᅩғᥴᘠਮಁᒒ޾๐ۓᒒ҅Ի԰ොୗᅎၚग़໏ • ᗌᅩғ୚ف᷐क़ᕟկ҅᭗מཛྷୗๅ॔๥

  28. ද᭜ Sidekiq • Sidekiq ฎ Ruby ୑ྍձۓ॒ቘጱຽᯈ҅ᶋଉ౮ᆧ • Sidekiq ጱၾ௳ܐᦓᓌܔ҅ፗളٟف

    Redis ܨݢਫሿ • դᎱᐏֺғ msg = { 'class' => worker, 'args' => arguments, 'jid' => SecureRandom.hex(12), 'retry' => options[:retry] || true, 'enqueued_at' => Time.now.to_f } redis.lpush("#{@namespace}:queue:default", JSON.dump(msg))
  29. SneaksPacker • Sneakers ᔄ֒ԭ Sidekiq҅Redis ᤩ๊ഘ౮ RabbitMQ • SneakersPacker ीےၾ௳ଠඎ޾

    RPC ᇙ௔ • դᎱᐏֺғ # Notification SneakersPacker.publish("demo.notify", "hello world") # Broadcasting SneakersPacker.publish("demo.broadcast", "something happen") # RPC weight = SneakersPacker.remote_call("record.weight", user_key) https://github.com/xiewenwei/sneakers_packer
  30. ᪜๐ۓහഝᦢᳯ • وՁਂؙ • ॔ګහഝ • හഝՙପ • Ԫկၞ๐ۓ

  31. وՁਂؙ ୌᦓݝכኸӞӻٟفݗٌ҅ਙݝ᧛ᦢᳯ

  32. ॔ګහഝ ᭗ᬦݎ૲/ᦈᴅݶྍๅෛ॔ګහഝ

  33. හഝՙପ

  34. Ԫկၞ๐ۓ

  35. ங๐ۓလቘᓌᬿ • ๐ۓဳٙ޾ݎሿ • ๐ۓᗑى • ᨮ᫹࣐ᤍ • ᴴၞ޾ᆤෙ๢ګ •

    ፊഴ޾෭ப… ங๐ۓ᩼ग़҅ங๐ۓလቘጱᵱ࿢᩼୩ᅱ
  36. ங๐ۓ۸Өވ҅ই֜๦ᤍݐᛣ • ॔๥ଶ҅Ծߝ๚๶ጱݒ۸ • ࢫᴚᥢཛྷ҅ܐ֢ොୗ • ݢզ᭑ྍᄍᬰ

  37. ௛ᕮ • ங๐ۓ۸ฎଫ੒॔๥ᔮᕹ҅य़ࢫᴚጱӞᐿํපಋྦྷ • ங๐ۓ۸ӧฎᱷ୨҅ਙݶ෸୚ف᷐क़॔๥ଶ҅Rails ଫአ ੦ٌై᯿҅ӧॡᭇݳੜࣳଫአ • ங๐ۓ۸ጱىᲫғ໑ഝӱۓᬰᤈᔉଶᭇӾጱښړ҅ୌᒈᓌ ׎ጱ๐ۓᳵጱ᭗מ๢ګ޾᪜๐ۓහഝᦢᳯᚆێ

  38. Thank you Q&A ᡀរ޷ࠏ Ruby ޾ Web ڹᒒૡᑕ૵ ํ఺᧗ݎᭆᓌܲғok@boohee.com