$30 off During Our Annual Pro Sale. View Details »

Railsconf2014

 Railsconf2014

Rails as an SOA client - Railsconf 2014

Pete Hodgson

April 23, 2014
Tweet

More Decks by Pete Hodgson

Other Decks in Programming

Transcript

  1. RAILS AS 

    AN SOA CLIENT
    Pete Hodgson | @ph1 | [email protected]

    View Slide

  2. @ph1
    rails
    DB
    DB
    rails
    app

    View Slide

  3. @ph1
    rails
    a
    service
    a
    service
    DB
    DB
    rails
    app

    View Slide

  4. @ph1
    rails
    a
    service
    a
    service a
    service
    a
    service
    DB
    DB
    rails
    app

    View Slide

  5. @ph1
    rails
    a
    service
    a
    service a
    service
    a
    service
    rails
    app

    View Slide

  6. me me me
    Pete Hodgson
    Consultant at ThoughtWorks
    @ph1
    blog.thepete.net

    View Slide

  7. @ph1
    ACT ONE
    INDEPENDENCE
    CO-DEPENDENCE
    and

    View Slide

  8. @ph1
    a
    service
    rails
    app
    a
    service

    View Slide

  9. @ph1
    SELECTION OF DEALS
    ARRAY OF ENTICING
    PRODUCT CATEGORIES

    View Slide

  10. @ph1
    rails
    app
    product
    service
    deals
    service

    View Slide

  11. @ph1
    rails
    app

    View Slide

  12. @ph1
    rails
    app
    product
    service
    deals
    service
    RED
    TEAM
    GREEN
    TEAM
    BLUE
    TEAM

    View Slide

  13. @ph1
    CONWAY’S
    LAW

    View Slide

  14. @ph1
    SYSTEM
    ARCHITECTURE
    HOW PEOPLE
    WORK TOGETHER
    AFFECTS

    View Slide

  15. @ph1
    HOW PEOPLE
    WORK TOGETHER
    AFFECTS
    SYSTEM
    ARCHITECTURE

    View Slide

  16. @ph1
    rails
    app
    product
    service
    deals
    service
    RED
    TEAM
    GREEN
    TEAM
    BLUE
    TEAM

    View Slide

  17. @ph1
    WHO’S BUG
    IS IT
    ANYWAY?

    View Slide

  18. @ph1
    rails
    app
    deals
    service
    product
    service

    View Slide

  19. @ph1
    fake
    service
    fake
    service
    rails
    BOUNDED
    INTEGRATION

    TESTS
    rails
    app

    View Slide

  20. @ph1
    IN PROCESS OUT-OF-PROCESS
    vcr
    webmock
    mimic
    moco
    stubby
    montebank

    View Slide

  21. @ph1
    deals
    service
    product
    service
    rails
    product
    service
    deals
    service

    ? ?
    rails
    app

    View Slide

  22. @ph1
    CONTRACT
    TESTS

    View Slide

  23. @ph1
    rails
    app
    product
    service
    deals
    service

    View Slide

  24. @ph1
    product
    service
    deals
    service
    rails
    app

    View Slide

  25. @ph1
    (or minitest, or whatever)
    plain old rspec
    pacto
    pact

    View Slide

  26. END-TO-END
    TESTS
    UNIT
    TESTS
    BOUNDED
    INTEGRATION TESTS
    SERVICE A
    CONTRACT TESTS
    SERVICE B
    CONTRACT TESTS
    SERVICE D
    CONTRACT TESTS
    SERVICE C
    CONTRACT TESTS
    FUNCTIONAL
    TESTS

    View Slide

  27. END-TO-END
    TESTS
    UNIT
    TESTS
    BOUNDED
    INTEGRATION TESTS
    SERVICE A
    CONTRACT TESTS
    SERVICE B
    CONTRACT TESTS
    SERVICE D
    CONTRACT TESTS
    SERVICE C
    CONTRACT TESTS
    FUNCTIONAL
    TESTS

    View Slide

  28. @ph1
    ACT TWO
    SERVICE
    GATEWAYS

    View Slide

  29. @ph1
    faraday

    View Slide

  30. @ph1
    rack

    View Slide

  31. CACHING
    AUTHENTICATION
    HTTP SERVER
    COOKIES
    LOGGING
    rack

    View Slide

  32. @ph1
    use Rack::Sendfile
    use ActionDispatch::Static
    use Rack::Lock
    use #
    use Rack::Runtime
    use Rack::MethodOverride
    use ActionDispatch::RequestId
    use Rails::Rack::Logger
    use ActionDispatch::ShowExceptions
    use ActionDispatch::DebugExceptions
    use ActionDispatch::RemoteIp
    use ActionDispatch::Reloader
    use ActionDispatch::Callbacks
    use ActiveRecord::Migration::CheckPending
    use ActiveRecord::ConnectionAdapters::ConnectionManagement
    use ActiveRecord::QueryCache
    use ActionDispatch::Cookies
    use ActionDispatch::Session::CookieStore
    use ActionDispatch::Flash
    use ActionDispatch::ParamsParser
    use Rack::Head
    use Rack::ConditionalGet
    use Rack::ETag
    run MyApp::Application.routes

    View Slide

  33. @ph1
    CACHING
    AUTHENTICATION
    HTTP CLIENT
    SERIALIZATION
    faraday

    View Slide

  34. @ph1
    class BikeStationGateway
    STATIONS_PATH = '/stations/json'
    !
    def initialize( base_url = 'http://bayareabikeshare.com' )
    @conn = Faraday.new(:url => base_url ) do |faraday|
    faraday.use :instrumentation
    faraday.response :json
    faraday.request :encode_json
    faraday.response :follow_redirects
    faraday.response :logger
    faraday.adapter Faraday.default_adapter
    end
    end
    !
    def stations
    response = @conn.get(STATIONS_PATH)
    # ... parse response.body into a list of stations
    end
    end

    View Slide

  35. @ph1
    class BikeStationGateway
    STATIONS_PATH = '/stations/json'
    !
    def initialize( base_url = 'http://bayareabikeshare.com' )
    @conn = Faraday.new(:url => base_url ) do |faraday|
    faraday.use :instrumentation
    faraday.response :json
    faraday.request :encode_json
    faraday.response :follow_redirects
    faraday.response :logger
    faraday.adapter Faraday.default_adapter
    end
    end
    !
    def stations
    response = @conn.get(STATIONS_PATH)
    # ... parse response.body into a list of stations
    end
    end

    View Slide

  36. @ph1
    a
    service
    a
    service
    service gateways

    View Slide

  37. @ph1
    hexagonal 

    architecture

    View Slide

  38. @ph1
    CACHING
    AUTHENTICATION
    HTTP CLIENT
    SERIALIZATION

    View Slide

  39. @ph1
    {
    “JSON”: “parsing”
    }
    !



    View Slide

  40. @ph1
    {
    "meta": {
    "response-time": "141",
    "server": "http://foo.com",
    ...
    },
    "product": {
    "uid": "151231511315083",
    "summary": {
    "title": "A Book Of Things",
    "shortDescription": “.....",
    ...
    },
    "author": {
    "firstName": "Bob",
    "lastName": "Smith",
    ...
    },
    ...
    }
    }

    View Slide

  41. @ph1
    hashie
    lazy_doc
    embedded_doc
    representable

    View Slide

  42. @ph1
    CACHING
    AUTHENTICATION
    HTTP CLIENT
    SERIALIZATION

    View Slide

  43. @ph1
    rails
    app
    product
    service
    pricing
    service

    View Slide

  44. @ph1
    cache the crap
    out of this!
    /prod-images/15123_front.png

    View Slide

  45. @ph1
    cache the crap
    out of this too!
    /products/15123.json

    View Slide

  46. @ph1
    CACHING
    AUTHENTICATION
    HTTP CLIENT
    SERIALIZATION

    View Slide

  47. @ph1
    CACHING
    AUTHENTICATION
    HTTP CLIENT
    SERIALIZATION

    View Slide

  48. @ph1
    class BikeStationGateway
    STATIONS_PATH = '/stations/json'
    !
    def initialize( base_url = 'http://bayareabikeshare.com' )
    @conn = Faraday.new(:url => base_url ) do |faraday|
    faraday.use :instrumentation
    faraday.response :json
    faraday.request :encode_json
    faraday.response :follow_redirects
    faraday.response :logger
    !
    faraday.use
    FaradayMiddleware::RackCompatible,
    Rack::Cache::Context,
    :metastore => "file:#{cache_dir}/faraday/meta",
    :entitystore => “file:#{cache_dir}/faraday/body",
    :ignore_headers => %w[Set-Cookie X-Content-Digest]
    !
    !
    faraday.adapter Faraday.default_adapter
    end
    end
    end

    View Slide

  49. @ph1
    on the web

    OF the web

    View Slide

  50. @ph1
    EPILOGUE
    SOME

    PRINCIPLES

    View Slide

  51. @ph1
    CONWAY’S
    LAW

    View Slide

  52. @ph1
    DOMAIN
    DRIVEN
    DESIGN

    View Slide

  53. @ph1
    POSTEL’S
    LAW

    View Slide

  54. @ph1
    HEXAGONAL
    ARCHITECTURE

    View Slide

  55. @ph1
    EMBRACE

    THE WEB

    View Slide

  56. THANKS!
    Pete Hodgson
    @ph1

    [email protected]

    View Slide

  57. 57
    SLIDES =>
    Pete Hodgson
    @ph1

    [email protected]

    View Slide