0.0
No commit activity in last 3 years
No release in over 3 years
NATS based client/server implementation that allows you to easily call remote services.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.12
~> 5.0
~> 1.1
~> 10.0

Runtime

~> 0.7.4
~> 0.8.0
 Project Readme

RemoteService img Code Climate Gem Version

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.