Microservice communication, job message, event broadcast and remote procedure call based on RabbitMQ.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
CHANGELOG.md
Gemfile
Gemfile.lock
LICENSE.md
README.md
Rakefile
sneakers_packer.gemspec

README.md

SneakersPacker Build Status

SneakersPacker is a gem for using sneakers to realize 3 message communication patterns job message, broadcast and RPC(remote procedure call).

Installation

Intall the `sneakers' gem first. see sneakers

Then add this line to your application's Gemfile:

gem 'sneakers_packer'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sneakers_packer

Usage

Configuation

SneakersPacker uses most of sneakers configuration for simpleness. There are app_name and rpc_timeout should be set for SneakersPacker.

Append below to config/initializers/sneakers.rb.

SneakersPacker.configure do |conf|
  conf.rpc_timeout = 3             # rpc client timeout. default is 5 seconds.
  conf.app_name = "sneakers_test"  # rpc client or server app's name. default is 'unknown'
end

API usage examples

  • Job Message

Client

SneakersPacker.publish("demo", "hello world")

Server

  class DemoWorker
    include SneakersPacker::CommonWorker
    from_queue :demo

    def call(data)
      puts "data is #{data}"
      # do something...
    end
  end
  • Broadcast

Client

It is same with Job Message

SneakersPacker.publish("demo.suprise", "hello world")

Server

It is almost same with Job Message except that one routing_key with multiple queues.

  class OneWorker
    include SneakersPacker::CommonWorker

    from_queue :one_name, routing_key: "demo.suprise"

    def call(data)
      puts "one: #{data}"
      # do something...
    end
  end
  class OtherWorker
    include SneakersPacker::CommonWorker

    from_queue :other_name, routing_key: "demo.suprise"

    def call(data)
      puts "other: #{data}"
    end
  end
  • RPC

Client

remote call with default timeouit. default is 5 seconds. SneakersPacker.remote_call("rpc_server", 10)

remote call with custom timeouit. SneakersPacker.remote_call("rpc_server", 12, timeout: 2)

Server

  class RpcServerWorker
    include SneakersPacker::RpcWorker

    from_queue :rpc_server

    # return value of call will be result of remote procedure call
    def call(data)
      data.to_i ** 3
    end
  end

See the gem doc or source code for accurate detail You can see the demo app for SneakersPacker by Sneakers.

Development

How to run test?

1.start RabbitMQ Server. for mac rabbitmq-server

2.start test workers ruby test/sneakers_test_workers.rb

3.run test bundle exec rake test

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/xiewenwei/sneakers_packer. 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.