How to use eventbus messaging in vertx?

2019-09-02 04:28发布

问题:

I followed the instructions in sample and ran:

vertx run eventbus_pointtopoint/receiver.rb -cluster
vertx run eventbus_pointtopoint/sender.rb -cluster

Then I only got:

➜  ruby  vertx run eventbus_pointtopoint/receiver.rb -cluster
Starting clustering...
No cluster-host specified so using address 192.168.56.1
Succeeded in deploying verticle

➜  ruby  vertx run eventbus_pointtopoint/sender.rb -cluster
Starting clustering...
No cluster-host specified so using address 192.168.56.1
Succeeded in deploying verticle

But no message received. What am I doing wrong?

回答1:

If you just want to see how event bus messaging works you can just move both pieces of code into the one file and run it.

require "vertx"
include Vertx

EventBus.register_handler('ping-address') do |msg|
  puts "Received message: #{msg.body}"
  # Now reply to it
  msg.reply('pong!')
end

Vertx::set_periodic(1000) do
  EventBus.send('ping-address', 'ping') do |reply|
    puts "Received reply: #{reply.body}"
  end
end

To run clustered

If you want to run the example specified with clustering you may need to change your cluster.xml config file, it is in the conf folder of your vertx install. I had to change two lines, first change

<multicast enabled="true">

to false:

<multicast enabled="false">

then change

<tcp-ip enabled="false">

to true

<tcp-ip enabled="true">

and make sure the interface tag has the correct IP address. Then the commands you specified above should run.