Pure ruby client for ZooKeeper (zookeeper.apache.org)
Supports full ZooKeeper API, synchronous or asynchronous style, watches etc.. with implementations over EventMachine or plain old Ruby IO/Threads
Other ruby libraries for zookeeper tend to use the underlying C/Java client libraries while zkruby implements the zookeeper wire protocol directly.
Advantages:
-
Rubyist API - with block is asynchronous, without block is synchronous
-
Avoids conflicts between various Ruby threading models and the C/Java apis
-
Same code for JRuby or MRI
-
Connection bindings for pure ruby IO and eventmachine, or roll your own
Disadvantages:
-
Duplicated code from Java/C libraries, particularly around herd effect protection
-
Needs to keep up with changes in wire protocol which are possibly more likely than changes in the client API
-
Possibly not as optimised in terms of performance (but your client code is ruby anyway)
-
Not production tested (yet- do you want to be the first?)
-
Configure Slf4R Logging (rubygems.org/gems/slf4r)
-
Get a connection {ZooKeeper.connect}
-
Make requests on the returned {ZooKeeper::Client}
require 'slf4r/ruby_logging' require 'zkruby' # Using ruby threads and sockets (default) zk = ZooKeeper.connect("localhost:2181") # Synchronous stat = zk.exists("/aPath") # Asynchronous zk.exists("/aPath) { |stat| puts stat.inspect } # Watches watch = lambda { |state,path,event| puts "Watch fired #{state} #{path} #{event}" } stat,data = zk.get("/aPath",watch) # OR with EventMachine require 'em_zkruby' EventMachine.run do # Magic of Fibers (via Empathy) lets us code normally and execute asynchronously Empathy::EM::Thread.new() do begin zk = ZooKeeper.connect("localhost:2181") #Sync path = zk.create("/aPath/mynode",ZK::ACL_ANYONE_UNSAFE,:ephemeral,:sequential) #Async zk.get(path) do |stat,data| puts "#{stat.inspect} #{data}" end rescue ZooKeeper::Error => zkex puts zkex.message end end end
-
A ZooKeeper cluster to connect to
-
Ruby 1.9 (but a backport should be straightforward)
$ (sudo) gem install zkruby
Download ZooKeeper from zookeeper.apache.org
Create a conf/zoo.cfg file (copying sample.zoo.cfg is fine)
Checkout the zkruby source into the contrib directory
Copy (if different) src/zookeeper.jute to contrib/zkruby/src/zookeeper.jute
Get gem dependencies
$ bundle install
Generate docs and run the tests/specs
$ rake
(The MIT License)
Copyright © 2012 Grant Gardner
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.