12
DECOUPEZ VOTRE APPLI EN MICRO-APIS SOA ON RAILS :-) by Nicolas Blanco http://twitter.com/slainer68

Découplez votre appli en micro-APIs

Embed Size (px)

Citation preview

Page 1: Découplez votre appli en micro-APIs

DECOUPEZ VOTRE APPLI EN MICRO-APIS

SOA ON RAILS :-)by Nicolas Blanco

http://twitter.com/slainer68

Page 2: Découplez votre appli en micro-APIs

CODE SPAGHETTI ? USINE A GAZ ?

Page 3: Découplez votre appli en micro-APIs

THIN CONTROLER / FAT MODEL

MODULES

CLASSES ACTIVE MODEL

VOTRE PROPRE ENGINE / GEM !

Page 4: Découplez votre appli en micro-APIs

DECOUPLAGE EN SOA ?

DECOUPER UNE FONCTIONNALITE EN MICRO-API

INDEPENDANCE

API REST JSON

STOCKAGE RAPIDE (REDIS ?)

INCONVENIENTS

Page 5: Découplez votre appli en micro-APIs

BUTREPONSE INSTANTANEE EN JSON

ID UNIQUE POUR CHAQUE JOB

Page 6: Découplez votre appli en micro-APIs

EXEMPLE=> POST api.myapp.com/imports{ id: “42”, status: “scheduled”, ... }

=> GET api.myapp.com/imports/42{ id: “42”, status: “finished”, result: { ... }, finished_at: ... }

Page 7: Découplez votre appli en micro-APIs

LES GEMS

SIDEKIQREDISGRAPE

REDIS PERSISTENCEFOREMAN

Page 8: Découplez votre appli en micro-APIs

FOREMANweb: bundle exec thin start -p $PORT

worker: bundle exec sidekiq -r ./workers/import_xml.rb -c 2

Page 9: Découplez votre appli en micro-APIs

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

Page 10: Découplez votre appli en micro-APIs

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

Page 11: Découplez votre appli en micro-APIs

WORKER module 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

Page 12: Découplez votre appli en micro-APIs

ALWAYS REFACTOR !