Using vhosts

With virtual hosts (vhosts), it is possible to have many different, independent virtual brokers within one single RabbitMQ instance. In this way, it is possible to use the same broker on the parts of many different applications without worrying about name clashes. This is the same approach used by web servers with virtual hosts.

You can find the simple Java example in the Chapter03/Recipe01 directory, which is identical to the example in the first recipe of the book, except for the usage of the vhost.

Getting ready

To exercise this recipe, you just need to issue some commands at the Linux command prompt, that is, RabbitMQ Command Prompt (sbindir) in the Windows Start menu.

How to do it…

To create a new vhost, perform the following steps:

  1. List available virtual hosts with the command:
    rabbitmqctl list_vhosts
  2. Create a new virtual host book_orders by issuing the command:
    rabbitmqctl add_vhost book_orders
  3. List its exchanges:
    rabbitmqctl list_exchanges -p book_orders
  4. List user permissions:
    rabbitmqctl list_permissions
    rabbitmqctl list_permissions -p book_orders
  5. Allow the user, guest, to access the book_orders vhost:
    rabbitmqctl set_permissions guest .* .* .* -p book_orders
  6. Use it with the Java client library:
    factory.setVirtualHost("book_orders");

How it works…

Once installed, RabbitMQ has just the default vhost / defined, as we can easily check with the command in step 1.

We then create a new vhost by issuing the command rabbitmqctl add_vhost (step 2). After that, we must issue all the commands related to this new vhost by specifying it with the –p option. If this is omitted, the commands are applied to the default vhost.

The new vhost you have added cannot be used yet. A quick check via listing permissions (step 4) will show that the new vhost has no authorization to perform any action. Then we give the predefined user, guest, all the permissions in the context of the book_orders vhost, as we will see in the next recipe (step 5).

At this point, it is enough to specify the vhost to the connection factory (step 6) in order to let a RabbitMQ client connect to it instead of the default vhost.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset