[Switch] RabbitMQ series (1): RabbitMQ installation and entry under Windows

https://blog.csdn.net/hzw19920329/article/details/53156015

 

1. Installing RabbitMQ under Windows requires the following steps

   (1): Download erlang, the reason is that the RabbitMQ server code is written in the concurrent language erlang, download address: http://www.erlang.org/downloads, double-click the .exe file to install it, and create it after the installation is complete An environment variable named ERLANG_HOME, whose value points to the erlang installation directory, at the same time add %ERLANG_HOME%\bin to Path, and finally open the command line and enter erl, if the erlang version information appears, it means that the erlang language environment is installed successfully;

                      

 

   (2): Download RabbitMQ, download address: http://www.rabbitmq.com/, and double-click .exe to install it (it needs to be noted here that the default installation directory is C:/Program Files/.... , there are spaces in this directory, we need to change the installation directory, it seems that spaces are not allowed in the RabbitMQ installation directory, I have stepped on this pit before);

   (3): Install RabbitMQ-Plugins, which is equivalent to a management interface, which is convenient for us to view the working conditions of each message queue and exchange of RabbitMQ in the browser interface. The installation method is: open the command line cd and enter the sbin directory of rabbitmq (my directory). Yes: E:\software\rabbitmq\rabbitmq_server-3.6.5\sbin), enter: rabbitmq-plugins enable rabbitmq_management command, you will find a prompt that the plugins are installed successfully after a while, the default is to install 6 plugins, if you are installing The following error occurred during the plugin process:        


   The solution is: first enter: rabbitmq-service stop on the command line, then enter rabbitmq-service remove, then enter rabbitmq-service install, then enter rabbitmq-service start, and finally re-enter rabbitmq-plugins enable rabbitmq_management to try, I am solved in this way;

   (4): After the plugin is installed, enter http://localhost:15672 in the browser for verification, you will see the following interface, enter the username: guest, password: guest, you can enter the management interface, of course, the username and password you can change


2. After installing RabbitMQ, let's briefly understand the concepts involved in RabbitMQ

    producer: message producer

    consumer: message consumer

     virtual host: virtual host. In RabbitMQ, users can only set some permissions at the virtual host level, such as which queues I can access, which requests I can process, etc.;

     broker: The message forwarder, which is the function of our RabbitMQ server, so what rules are the messages forwarded according to? The following concepts need to be used;

     exchange: switch, it deals directly with the producer, which is similar to the function of a router, mainly for forwarding operations, so which exchange does the producer use for routing? This depends on the routing key (routing key), each message has this key, we can also set it ourselves, it is actually a string;

     Queue: message queue, used to store messages. It receives messages routed by exchange. We can perform persistent operations on the content of the queue. Then, does the queue receive messages routed by exchange? At this time, the binding key will be used. The binding key will bind the queue to the exchange. As for the binding method, RabbitMQ provides a variety of methods. You can check the RabbitMQ blog series of Hongyang God. ( click to view );

     The above are some of the concepts involved in RabbitMQ. A diagram to represent the relationship between these concepts is:


3. RabbitMQ is simple to use

   Producer side steps:

    (1): Create a ConnectionFactory and set some parameters, such as hostname, portNumber, etc.

    (2): Use ConnectionFactory to create a Connection connection

    (3): Use Connection to create a Channel channel

    (4): Create queue and bind to Channel

    (5): Create a message and send it to the queue

     Note that in our current example, the exchange switch is not used. RabbitMQ will create an exchange with an empty string name by default. If we do not create our own exchange, the default is to use this exchange;

     Producer side code:

 

[java] view plain copy
  1. public class Sender {  
  2.     private final static String QUEUE_NAME = "MyQueue";  
  3.       
  4.     public static void main(String[] args) {  
  5.         send();  
  6.     }  
  7.       
  8.     public static void send()  
  9.     {  
  10.         ConnectionFactory factory = null;  
  11.         Connection connection = null;  
  12.         Channel channel = null;  
  13.         try {  
  14.             factory = new ConnectionFactory();  
  15.             factory.setHost("localhost");  
  16.             connection = factory.newConnection();  
  17.             channel = connection.createChannel();  
  18.             channel.queueDeclare(QUEUE_NAME, false, false, false, null);  
  19.             String message = "my first message .....";  
  20.             channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));  
  21.             System.out.println( "Message has been sent..."+message);  
  22.         } catch (IOException e) {  
  23.             e.printStackTrace ();  
  24.         } catch (TimeoutException e) {  
  25.             e.printStackTrace ();  
  26.         }finally{  
  27.             try {  
  28.                 //close the resource  
  29.                 channel.close();  
  30.                 connection.close();  
  31.             } catch (IOException e) {  
  32.                 e.printStackTrace ();  
  33.             } catch (TimeoutException e) {  
  34.                 e.printStackTrace ();  
  35.             }  
  36.         }  
  37.     }  
  38. }  

     Consumer (consumer) side steps:

     (1): Create a ConnectionFactory and set some parameters, such as hostname, portNumber, etc.

     (2): Use ConnectionFactory to create a Connection connection

     (3): Use Connection to create a Channel channel

     (4): Bind the queue to the Channel, note that the queue name here should be consistent with the queue created by the previous producer

     (5): Create a consumer Consumer to receive messages, and bind the consumer to the queue at the same time

     Consumer side code:

 

[java] view plain copy
  1. public class Receiver {  
  2.     private final static String QUEUE_NAME = "MyQueue";  
  3.       
  4.     public static void main(String[] args) {  
  5.         receive();  
  6.     }  
  7.       
  8.     public static void receive()  
  9.     {  
  10.         ConnectionFactory factory = null;  
  11.         Connection connection = null;  
  12.         Channel channel = null;  
  13.           
  14.         try {  
  15.             factory = new ConnectionFactory();  
  16.             factory.setHost("localhost");  
  17.             connection = factory.newConnection();  
  18.             channel = connection.createChannel();  
  19.             channel.queueDeclare(QUEUE_NAME, false, false, false, null);  
  20.             Consumer consumer = new DefaultConsumer(channel){  
  21.                 @Override  
  22.                 public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,  
  23.                         byte[] body) throws IOException {  
  24.                     System.out.println("11111111111");  
  25.                     String message = new String(body, "UTF-8");  
  26.                     System.out.println( "Received a message..."+message);  
  27.                 }};  
  28.             channel.basicConsume(QUEUE_NAME, true,consumer);  
  29.         } catch (IOException e) {  
  30.             e.printStackTrace ();  
  31.         } catch (TimeoutException e) {  
  32.             e.printStackTrace ();  
  33.         }finally{  
  34.             try {  
  35.                 //close the resource  
  36.                 channel.close();  
  37.                 connection.close();  
  38.             } catch (IOException e) {  
  39.                 e.printStackTrace ();  
  40.             } catch (TimeoutException e) {  
  41.                 e.printStackTrace ();  
  42.             }  
  43.         }  
  44.     }  
  45. }  

   Well, this article is here first. In the next article, I will briefly introduce more in-depth things. In the future, I will also encapsulate the RabbitMQ native API, which is convenient for our own development;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326846497&siteId=291194637