Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Découplez votre appli en micro-APIs

9,034 views

Published on

Published in: Technology
  • Be the first to comment

Découplez votre appli en micro-APIs

  1. 1. DECOUPEZ VOTRE APPLI EN MICRO-APIS SOA ON RAILS :-) by Nicolas Blanco http://twitter.com/slainer68
  2. 2. CODE SPAGHETTI ? USINE A GAZ ?
  3. 3. THIN CONTROLER / FAT MODEL MODULES CLASSES ACTIVE MODELVOTRE PROPRE ENGINE / GEM !
  4. 4. DECOUPLAGE EN SOA ?DECOUPER UNE FONCTIONNALITE EN MICRO-API INDEPENDANCE API REST JSON STOCKAGE RAPIDE (REDIS ?) INCONVENIENTS
  5. 5. BUTREPONSE INSTANTANEE EN JSONID UNIQUE POUR CHAQUE JOB
  6. 6. EXEMPLE=> POST api.myapp.com/imports{ id: “42”, status: “scheduled”, ... }=> GET api.myapp.com/imports/42{ id: “42”, status: “finished”, result: { ... }, finished_at: ... }
  7. 7. LES GEMS SIDEKIQ REDIS GRAPEREDIS PERSISTENCE FOREMAN
  8. 8. FOREMAN web: bundle exec thin start -p $PORTworker: bundle exec sidekiq -r ./workers/import_xml.rb -c 2
  9. 9. MODEL IMPORTmodule Models class Import include Redis::Persistence property :state, default: "scheduled" property :created_at property :finished_at property :fail_message property :result, default: {} def start! update_attributes(state: "started") end def finish! update_attributes(state: "finished", finished_at: Time.now) end def fail_with!(message) update_attributes(state: "failed", fail_message: message) end end end
  10. 10. class API < Grape::API version v1, using: :header, vendor: "pluriporter" format :json resources :imports do desc "Creates new import from XML data" params do requires :xml, type: String, desc: "XML data" end post do Models::Import.create(created_at: Time.now).tap do |import| Workers::Import.perform_async(import.id, params[:xml]) end endend
  11. 11. WORKERmodule Workers class Import include Sidekiq::Worker def perform(import_id, xml) require File.expand_path("../../models/import", __FILE__) import = Models::Import.find(import_id) import.start! begin # ... end rescue => e import.fail_with!(e.to_s) raise e end import.finish! end
  12. 12. ALWAYS REFACTOR !

×