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

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

Vincent Xie
September 23, 2016

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

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

Vincent Xie

September 23, 2016
Tweet

More Decks by Vincent Xie

Other Decks in Programming

Transcript

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

    ىԭᡀរ • ؋଼ᶾऒᑏۖ԰ᘶᗑلݪ • 2007 ଙ୏তֵአ Ruby
  2. ᯿ෛᕟᕢፓ୯޾෈կ • concerns • namespace • folder # Custom autoloadable

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

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

    #... end def call #... end private #... end ᭿عֵአᔄොဩֵ҅አ Object ݢզ ֵአ OOP ಋဩᬰᤈ᯿຅޾ಘ઀
  5. ہᐶ gem • ӱۓ෫ىጱ gem҅ই string/number/date utils ᔄ • ӱۓوՁጱ

    gem҅ই weight_model҅rule_engine • ॔አ models, controllers ޾ viewsֵ҅አ Rails Engine
  6. ද᭜ 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))
  7. 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
  8. ங๐ۓလቘᓌᬿ • ๐ۓဳٙ޾ݎሿ • ๐ۓᗑى • ᨮ᫹࣐ᤍ • ᴴၞ޾ᆤෙ๢ګ •

    ፊഴ޾෭ப… ங๐ۓ᩼ग़҅ங๐ۓလቘጱᵱ࿢᩼୩ᅱ