Grenache is a micro-framework for connecting microservices. Its simple and optimized for performance.
Internally, Grenache uses Distributed Hash Tables (DHT, known from Bittorrent) for Peer to Peer connections. You can find more details how Grenche internally works at the Main Project Homepage
gem install grenache-ruby-ws
Install Grenache Grape
: https://github.com/bitfinexcom/grenache-grape:
npm i -g grenache-grape
// Start 2 Grapes
grape --dp 20001 --aph 30001 --bn '127.0.0.1:20002'
grape --dp 20002 --aph 40001 --bn '127.0.0.1:20001'
This RPC Server example announces a service called rpc_test
on the overlay network. When a request from a client is received,
it replies with world
. It receives the payload hello
from the
client.
The client sends hello
and receives world
from the server.
Internally the DHT is asked for the IP of the server and then the request is done as Peer-to-Peer request via websockets.
Grape:
grape --dp 20001 --aph 30001 --bn '127.0.0.1:20002'
grape --dp 20002 --aph 40001 --bn '127.0.0.1:20001'
Server:
require "grenache-ruby-ws"
EM.run do
Signal.trap("INT") { EventMachine.stop }
Signal.trap("TERM") { EventMachine.stop }
c = Grenache::Ws.new(grape_address: "http://127.0.0.1:40001/")
port = 5001
c.listen("test_service", port) do |req|
error = nil
[error,"hello #{req.payload}"]
end
end
Client:
require "grenache-ruby-ws"
c = Grenache::Ws.new(grape_address: "http://127.0.0.1:40001/")
EM.run do
10.times do |n|
c.request("test_service","world #{n}") do |err, msg|
if err
puts "error: #{err}"
else
puts "response: #{msg}"
end
end
end
end
options
-
:grape_address
<String> -
:timeout
<Number> -
:auto_announce_interval
<Number> -
:auto_announce
<Boolean> -
:service_timeout
<Number> -
:service_host
<String>
-
name
<String> Name of the service to addresspayload
Payload to sendoptions
:timeout
Timeout for the request
Sends a single request to a RPC server/worker. Example.
name
<String> Name of the service to announceport
<Number> Port to listenoptions
Sets up a worker which connects to the DHT.
Listens on the given port
.