I haven't written it for a long time. Recently, I used rabbitmq in my project. I found some materials for testing and finally succeeded. I will share the steps for installing and configuring rabbitmq.
I. Erlang
Installation Process:
1.double hit otp_win32_r16801.exe (different versions may have different names), select next
2. The program is installed on drive C by default. It is recommended that the program be installed on a non-system disk, such as drive D (if it is installed on drive C, there may be some permission issues). After modifying the installation path, select next:
3. Enter the installer and select install to complete the installation.
Configure the environment variable: add the variable name erlang_home under the system variable. The variable value is c: \ Program Files (x86) \ erl6.1.
Ii. Install rabbitmq
Rabbitmq-server-3.3.5.exe
Install complete add environment variable, double-click path and add:; C: \ Program Files (x86) \ rabbitmq Server \ rabbitmq_server-3.3.5 \ sbin
Start
CMD enter the sbin directory and enter rabbitmq-service to start
Or directly find c: \ Program Files (x86) \ rabbitmq Server \ rabbitmq_server-3.3.5 \ sbin under the rabbitmq installation directory
Open the browser and log on to http: // localhost: 15672. The monitoring page is displayed, indicating that the configuration is successful. username and password: Guest
After the configuration is successful, messages are sent using exchange.
The core components of rabbit include Queue (Message Queue) and exchanges. The main component of exchange is to route information and bind the message queue to exchange, in this mode, the message publisher only needs to publish the information to the corresponding exchange, exchange automatically distributes information to different queue.
In the. NET environment, there are many API options based on rabbitmq, and finally chose easynetq (http://easynetq.com /)
/// <Summary> /// Message Server Connector /// </Summary> public class busbuilder {public static ibus createmessagebus () {// Message Server connection string connstring = "host = 172.17.186.50: 5672; virtualhost = RB; username = admin; Password = 123"; return rabbithuch. createbus (connstring); // return rabbithuch. createbus (connectionstring. connectionstring );}}
/// <Summary> /// send the message /// </Summary> Public static void publish (Message MSG) {/// create the message bus ibus bus = busbuilder. createmessagebus (); try {using (VAR publishchannel = bus. openpublishchannel () // create a message pipeline {publishchannel. publish (MSG, x => X. withtopic (MSG. messagerouter); // send a message through the pipeline} catch (easynetqexception ex) {// handle exceptions in connection to the Message Server // messagehelper. writefuntionexceptionlog ("publish", Ex. message + "|" + ex. stacktrace);} bus. dispose (); // similar to the database connection, use the log to destroy the bus object}
/// <Summary> /// receives the message // </Summary> /// <Param name = "MSG"> </param> Public static void subscribe (Message msg, iprocessmessage ipro) {// create the message bus ibus bus = busbuilder. createmessagebus (); try {bus. subscribe <message> (MSG. messageid, message => ipro. processmsg (Message), x => X. withtopic (MSG. messagerouter ). withargument ("X-ha-Policy", "all");} catch (easynetqexception ex) {// handle a message server connection exception // messagehelper. writefuntionexceptionlog ("subscribe", Ex. message + "|" + ex. stacktrace );}}
Add VM
Add User
When the code is executed, the exchanges is automatically created. The name is based on the project name.
Click the new exchanges to enter the new queue
Finally, call the interface to test sending. The Code is as follows:
Protected void button#click (Object sender, eventargs e) {MQ. message MSG = new MQ. message (); MSG. messageid = "testkey"; MSG. messagebody = "Send test"; MSG. messagetitle = "Hello World"; MSG. messagerouter = "testkey"; MQ. mqhelper. publish (MSG );}
MSG. messagerouter = "testkey" -- this configuration is very important, depending on the routing key in the previous step.
Message pushed!
Source code download: http://pan.baidu.com/s/1sjExnWh
Install and configure rabbitmq and push messages