Remote services made easy. This gem is basically RPC client/server implemented on top of awesome NATS project. Every service you want to use is exposed through class that extends RemoteService::Proxy
. Service itself extends RemoteService::Service
and should define all methods that you want to call from clients.
Installation
Add this line to your application's Gemfile:
gem 'remote_service'
And then execute:
$ bundle
Or install it yourself as:
$ gem install remote_service
Usage
First you need to start NATS cluster. This can be either single broker or n-node cluster. I've made easy for you to play with this gem, so all you need to do is install Docker
and Docker compose
(if you haven't yet) and run following command.
docker-compose up
Then you need to define and run your service. Minimal service can be run with a following script where return value of each method will be sent back as a response.
require "remote_service"
class ServiceA < RemoteService::Service
def all(count, keyword)
# exceptions raised here will raise RemoteService::Errors::RemoteCallError exception in client
count
end
end
# NATS uses autodiscovery, so third broker will be added automatically
ServiceA.start(brokers: ['nats://127.0.0.1:4222', 'nats://127.0.0.1:5222'])
Last thing is to call the service defined previously. All you need to do is define a service proxy, connect to NATS cluster and call a remote method as if it was local.
require "remote_service"
RemoteService.connect(brokers: ['nats://127.0.0.1:4222', 'nats://127.0.0.1:6222'])
class ServiceA < RemoteService::Proxy
timeout 100 # optional, default value is 10ms
end
# non-blocking call
ServiceA.all(123, keyword: 'value') do |result, error|
puts result
end
sleep(0.1) # wait for non-blocking call to execute
# blocking call
# this will raise RemoteService::Errors::RemoteCallError if an error was raised in remote service
puts ServiceA.all(123, keyword: 'value')
Todo
Add full support for NATS configuration options.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/marekgalovic/ruby-remote-service. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.