There is a vhost concept in RABBITMQ, Vhost is equivalent to a mini version of the RABBITMQ server, on a RABBITMQ server can create multiple vhost, they have their own rights control mechanism, We can let different users have access to different vhost. More simply, it's like running multiple virtual machines on an operating system.
We can use Rabbitmqctl list_vhosts to view the existing vhost on the current RABBITMQ server. At present, there is only one vhost on our server called "/", this is also rabbitmq default vhost.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/F1/wKioL1cN38DCLHiVAAAOk2UnRJY220.png "title=" 1.PNG " alt= "Wkiol1cn38dclhivaaaok2unrjy220.png"/>
Next we use Rabbitmqctl add_vhost to add a vhost.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/F1/wKioL1cN4OKhF-zMAAAiuwgSnoo675.png "title=" 2.PNG " alt= "Wkiol1cn4okhf-zmaaaiuwgsnoo675.png"/>
After adding a new vhost, we are unable to access this vhost, we need to rabbitmqctl set_permissions to the user to access the Vhost permissions.
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/F1/wKioL1cN5gqRuTytAAAz_RlUH_Y417.png "title=" 3.PNG " alt= "Wkiol1cn5gqrutytaaaz_rluh_y417.png"/>
Then we can access the newly created vhost, and we'll change the previous code slightly:
package com.jaeger.vhost;import java.io.ioexception;import Java.util.concurrent.timeoutexception;import org.junit.test;import com.rabbitmq.client.amqp;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.connectionfactory;import com.rabbitmq.client.consumer;import Com.rabbitmq.client.defaultconsumer;import com.rabbitmq.client.envelope;public class producer {private static final String MY_EXCHANGE_NAME = "Myexchange";p rivate static final string my_routing_key = "Myroutingkey";p rivate static final String MY_QUEUE_NAME = "Myqueue";p rivate static final string direct = "Direct";p rivate static final string host = "172.19.64.28";p rivate static final String USER = "Jaeger";p Rivate static final string password = "Root";p rivate static final string vhost = "Jaeger_vhost";p rivate static final int port = 5672; @Testpublic void createexchangeandqueue () throws ioexception, timeoutexception {connectionfactory connectionfactory = new connectionfactory (); Connectionfactory.sethost (HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport (PORT);// Specifies the VHOST name Connectionfactory.setvirtualhost (VHOST) that needs to be accessed; Connection connection = connectionfactory.newconnection (); Channel channel = connection.createchannel (); Channel.exchangedeclare (MY_EXCHANGE_NAME, DIRECT); Channel.queuedeclare (my_queue_name, false, false, false, null); Channel.queueBind ( My_queue_name, my_exchange_name, my_routing_key); Channel.close (); Connection.close ();} @Testpublic void Produce () throws ioexception, timeoutexception {connectionfactory connectionfactory = new connectionfactory (); Connectionfactory.sethost (HOST); Connectionfactory.setusername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport (PORT);// Specifies the VHOST name Connectionfactory.setvirtualhost (VHOST) that needs to be accessed; Connection connection = connectionfactory.newconnection (); Channel channel = connection.createchannel (); string message = "hello world!"; Channel.basicpublish (My_exchange_name, my_routing_key, null, message.getbytes ("Utf-8")); System.out.println ("sent " + message + "'"); Channel.close (); Connection.close ();} @Testpublic void consume () throws IOException, TimeoutException, Interruptedexception {connectionfactory connectionfactory = new connectionfactory (); Connectionfactory.sethost (HOST); connectionfactory.setUsername (USER); Connectionfactory.setpassword (PASSWORD); Connectionfactory.setport (PORT);// Specifies the VHOST name Connectionfactory.setvirtualhost (VHOST) that needs to be accessed; Connection connection = connectionfactory.newconnection (); Channel channel = connection.createchannel (); Consumer consumer = new defaultconsumer (channel) {@Overridepublic void Handledelivery (STRING CONSUMERTAG, ENVELOPE ENVELOPE, AMQP. Basicproperties properties,byte[] body) throws IOException {String message = new string (body, "UTF-8"); System.out.println ("received " + message + "'");}; Channel.basicconsume (My_queue_name, true, consumer); Thread.Sleep (1000);}}
First run the Createexchangeandqueue method and create a new exchange and queue under Jaeger_vhost:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7E/F3/wKioL1cN9wvRgsHNAAD_TUwdD4g680.png "style=" float: none; "title=" 4.PNG "alt=" Wkiol1cn9wvrgshnaad_tuwdd4g680.png "/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7E/F3/wKioL1cN9wux17Z-AABq00tebuU386.png "style=" float: none; "title=" 5.PNG "alt=" Wkiol1cn9wux17z-aabq00tebuu386.png "/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/F6/wKiom1cN9lXCGP7CAABlUxD6V9w969.png "style=" float: none; "title=" 6.PNG "alt=" Wkiom1cn9lxcgp7caabluxd6v9w969.png "/>
Then run the produce method to add a piece of data to the Jaeger_vhost queue:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/F3/wKioL1cN902jgLtPAABnYxBxM7o589.png "title=" 7.PNG " alt= "Wkiol1cn902jgltpaabnyxbxm7o589.png"/> Last run consume to consume the message in the queue:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/F3/wKioL1cN927jWyFuAABnglKnYE0698.png "title=" 8.PNG " alt= "Wkiol1cn927jwyfuaabnglknye0698.png"/>
This article is from the "Bronze Gong" blog, please be sure to keep this source http://jaeger.blog.51cto.com/11064196/1763455
RABBITMQ Introduction (Fri)--vhost