Install and configure the RabbitMQ message queue and push messages.
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
What scenarios does rabbitmq apply?
Rabbitmq
Edit
MQ is called Message Queue. MQ is a communication method for applications. Applications write and retrieve data (messages) for applications in the inbound and outbound queues to communicate with each other without dedicated connections. Message transmission refers to the communication between programs by sending data in messages, rather than by directly calling each other. Direct calls are usually used for such remote process calls. Queuing means that applications communicate through queues. The use of the queue removes the need to receive and send applications simultaneously. Among them, more mature MQ products include ibm websphere mq.
Use Cases
In the project, some operations that do not require immediate return and time-consuming are extracted for asynchronous processing. This asynchronous processing method greatly saves the server's request response time, this improves the system throughput.
How to push messages on the public platform
The text message of the public platform is sent as follows: 1. Click the material management item under the left-side topic management column, select "Single Text message" or "multi text message" in the intermediate editing area to go to the editing page. (A single text message is applicable to one message and one topic, and multiple text messages are applicable to multiple topics of one message) 2. on the editing page, add the corresponding title, cover image, abstract (no items for multi-text messages), and message body, and save the settings. You can preview the edited results if you want to see the edited results, enter your personal ID and send the message to your mobile phone to check the effect. 3. After Entering the Group Sending function and setting the group object, gender, and region, select the final "text message" on the inner option below. then, select the text message that was edited and saved in the clip management, and click the Green Group button below to send the message. If you do not understand, please join me or ask .....