Share
Embed
Transcript
16 slides
-
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 -
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 -
barrister rpc • Combines json-rpc with an easy to use
idl • human readable client-server implementation -
installation • pip install —pre barrister • gem install barrister
• Rails - add, gem ‘barrister’ to Gemfile & bundle -
class • lib/calculator.rb class Calculator def initialize end def add(a,
b) return a+b end end -
define idl • create - app/interfaces • place idl file
here. • compile the json here. -
define idl • app/interfaces/calc.idl • barrister -t "Calculator Service" -j
calc.json calc.idl interface Calculator { add(a float, b float) float } -
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"}] -
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 -
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 -
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) -
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) -
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/