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

Barrister RPC

Barrister RPC

Barris RPC presentation Mumbai Ruby Users Group

Saurabh Bhatia

October 18, 2016
Tweet

More Decks by Saurabh Bhatia

Other Decks in Technology

Transcript

  1. what is rpc ? • Remote Procedure Call - In

    computer science, a remote procedure call (RPC) is an inter-process communication that allows a computer program to cause a subroutine or procedure to execute in another address space (commonly on another computer on a shared network) without the programmer explicitly coding the details for this remote interaction. Source - https://en.wikipedia.org/wiki/Remote_procedure_call
  2. why use it? • Developing apps is costly, bridging legacy

    systems with newer ones is cheaper. • Any language is not fit for everything - get best of all worlds by being polygot. • Connecting & interfacing microservices
  3. barrister rpc • Combines json-rpc with an easy to use

    idl • human readable client-server implementation
  4. installation • pip install —pre barrister • gem install barrister

    • Rails - add, gem ‘barrister’ to Gemfile & bundle
  5. define idl • app/interfaces/calc.idl • barrister -t "Calculator Service" -j

    calc.json calc.idl interface Calculator { add(a float, b float) float }
  6. define idl • app/interfaces/calc.json [{"comment": "", "functions": [{"comment": "", "returns":

    {"optional": false, "is_array": false, "type": "float"}, "params": [{"is_array": false, "type": "float", "name": "a"}, {"is_array": false, "type": "float", "name": "b"}], "name": "add"}], "type": "interface", "name": "Calculator"}, {"barrister_version": "0.1.6", "type": "meta", "date_generated": 1416594045033, "checksum": "8c13de1b0fa6797b80df3f873 c86c509"}]
  7. initialize server • calc_controller.rb before_action :initialize_server def initialize_server file =

    Rails.root.join('app/interfaces/calc.json') contract = Barrister::contract_from_file(file) @server = Barrister::Server.new(contract) @server.add_handler("Calculator", Calculator.new) end
  8. respond to client call • calc_controller.rb before_action :initialize_server def add

    resp = @server.handle_json(request.body.read) resp render nothing: true end def initialize_server file = Rails.root.join('app/interfaces/calc.json') contract = Barrister::contract_from_file(file) @server = Barrister::Server.new(contract) @server.add_handler("Calculator", Calculator.new) end
  9. route • routes.rb post 'add' => 'calc#add' Started POST "/add"

    for ::1 at 2014-11-22 08:40:07 +0530 Processing by CalcController#add as */* Parameters: {"jsonrpc"=>"2.0", "id"=>"1", "method"=>"barrister-idl", "calc"=>{"id"=>"1"}} Rendered text template (0.1ms) Completed 200 OK in 14ms (Views: 9.3ms | ActiveRecord: 0.9ms)
  10. client • calc_client.rb require 'barrister' #initialize the endpoint trans =

    Barrister::HttpTransport.new("http://localhost:30020/add") #loads contract, and idl to the endpoint client = Barrister::Client.new(trans) #rpc call puts client.Calculator.add(1, 5.1)
  11. links • http://barrister.bitmechanic.com • https://github.com/coopernurse/barrister • Rails Client - https://github.com/laser/barrister-

    rails • http://blog.carbonfive.com/2014/05/13/new-hat- meets-old-polyglot-distributed-systems-with- barrister-rpc/