diff --git a/README.md b/README.md index aed8f6a..a83b9f2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,42 @@ # Apache Kafka example for Ruby -## UNDER DEVELOPMENT +## Setup + +Make sure you have any modern version of Ruby up and running. +Then install bundler and dependencies for this codebase. + +```bash +cd ruby-kafka-example +gem install bundler && bundle +``` + +Create your account on [cloudkarafka], and set this environment variables: + +```bash +export CLOUDKARAFKA_BROKERS=a....cloudkafka.com:9094,a....cloudkafka.com:9094,ar....cloudkafka.com:9094 +export CLOUDKARAFKA_USERNAME=your-username +export CLOUDKARAFKA_PASSWORD=your-password +export CLOUDKARAFKA_TOPIC_PREFIX=topic_prefix- +``` + +Visit Dashboard on [cloudkarafka] and create a topic with name similar to this `topic_prefix-test`. + + +Bootup the [consumer.rb](consumer.rb) + +```bash +ruby consumer.rb +``` + +Then run the [producer.rb](producer.rb) that will generate some messages: + +```bash +ruby producer.rb +``` + +And you sould be good to go with your experiments. + +Fin. + + +[cloudkarafka]:https://www.cloudkarafka.com/ diff --git a/consumer.rb b/consumer.rb index 1a733c7..3fc0eea 100644 --- a/consumer.rb +++ b/consumer.rb @@ -2,10 +2,10 @@ require 'rdkafka' config = { - :"bootstrap.servers" => ENV['CLOUDKARAFKA_BROKERS'], + :"bootstrap.servers" => ENV.fetch('CLOUDKARAFKA_BROKERS'), :"group.id" => "cloudkarafka-example", - :"sasl.username" => ENV['CLOUDKARAFKA_USERNAME'], - :"sasl.password" => ENV['CLOUDKARAFKA_PASSWORD'], + :"sasl.username" => ENV.fetch('CLOUDKARAFKA_USERNAME'), + :"sasl.password" => ENV.fetch('CLOUDKARAFKA_PASSWORD'), :"security.protocol" => "SASL_SSL", :"sasl.mechanisms" => "SCRAM-SHA-256" } diff --git a/producer.rb b/producer.rb index c48d954..9418803 100644 --- a/producer.rb +++ b/producer.rb @@ -2,10 +2,10 @@ require 'rdkafka' config = { - :"bootstrap.servers" => ENV['CLOUDKARAFKA_BROKERS'], + :"bootstrap.servers" => ENV.fetch('CLOUDKARAFKA_BROKERS'), :"group.id" => "cloudkarafka-example", - :"sasl.username" => ENV['CLOUDKARAFKA_USERNAME'], - :"sasl.password" => ENV['CLOUDKARAFKA_PASSWORD'], + :"sasl.username" => ENV.fetch('CLOUDKARAFKA_USERNAME'), + :"sasl.password" => ENV.fetch('CLOUDKARAFKA_PASSWORD'), :"security.protocol" => "SASL_SSL", :"sasl.mechanisms" => "SCRAM-SHA-256" } @@ -14,11 +14,11 @@ rdkafka = Rdkafka::Config.new(config) producer = rdkafka.producer -100.times do |i| +1000.times do |i| puts "Producing message #{i}" producer.produce( topic: topic, - payload: "Payload #{i}", + payload: "This is my payload #{i} TEST 2", key: "Key #{i}" ).wait end