RabbitMQ for windows
一、搭建环境
Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装RabbitMQ之前要先安装Erlang。
erlang:http://www.erlang.org/download.html
rabbitmq:http://www.rabbitmq.com/download.html
我目前使用的:http://pan.baidu.com/s/1eS8Dhse
默认安装的Rabbit MQ 监听端口是:5672
二、配置
1. 安装完以后erlang需要手动设置ERLANG_HOME 的系统变量。
输入:set ERLANG_HOME=C:\Program Files\erl9.0
2,打开cmd定位到rabbitmq的安装路径:C:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.10\sbin
上述命令回车后接着输入rabbitmqctl status,回车后出现下面一坨的即说明安装没有问题:
出现问题: Error:unable to connect to node rabbit@Hunter:nodedown
也不知道怎么回事,重新安装了下rabbitmq-server-3.6.10
3,rabbitmq-plugins enable rabbitmq_management(安装 RabbitMQWeb的管理插件。此时,已经可以通过 http://127.0.0.1:15672/ 地址来访问web管理界面了,默认的账户和密码均是 guest。但实际使用时可能需要重新一个新的管理账户)
4,rabbitmqctl.bat add_user zhangdi 123456(创建管理用户,这一步还不能登录)
5,rabbitmqctl.bat set_user_tags zhangdi administrator(设置管理员,可以登录了)
6,rabbitmqctl.bat set_permissions -p / zhangdi ".*" ".*" ".*"(授予管理员权限)
7,其他命令
a. 查询用户: rabbitmqctl.bat list_users
b. 查询vhosts: rabbitmqctl.bat list_vhosts
c. 启动RabbitMQ服务: net stop RabbitMQ && net start RabbitMQ
8,centos配置
https://ken.io/note/centos7-rabbitmq-install-setup#H3-7
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#添加用户 sudo rabbitmqctl add_user admin pwd #设置用户角色 sudo rabbitmqctl set_user_tags admin administrator #tag(administrator,monitoring,policymaker,management) #设置用户权限(接受来自所有Host的所有操作) sudo rabbitmqctl set_permissions -p "/" admin '.*' '.*' '.*' #查看用户权限 sudo rabbitmqctl list_user_permissions admin
新版配置:
下载地址:https://pan.baidu.com/s/18-rLh0e3mSY0xX4YfDqi3g
参考地址:https://blog.csdn.net/hzw19920329/article/details/53156015
1,下载安装erlang
①添加系统变量:ERLANG_HOME=C:\Program Files\erl9.3(安装路径)
②添加系统变量path:C:\Program Files\erl9.3\bin
③测试安装是否成功:打开cmd 输入erl,如果出现erlang的版本信息就表示erlang语言环境安装成功
2,下载安装RabbitMQ
注意:安装目录不能存在空格,最好安装到c盘(我安装rabbitmq到D盘出错)
①cmd进入C:\RabbitMQServer\rabbitmq_server-3.7.4\sbin目录 输入:rabbitmq-plugins enable rabbitmq_management安装管理界面
②安装完成进入http://localhost:15672,默认管理员和密码都是guest
三、C#使用RabbitMQ(使用EasyNetQ)
1,MQ.Common
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using EasyNetQ; namespace MQ.Common { /// <summary> /// 消息服务器连接器 /// </summary> public class BusBuilder { public static IBus CreateMessageBus() { // 消息服务器连接字符串 // var connectionString = ConfigurationManager.ConnectionStrings["RabbitMQ"]; //string connString = "host=192.168.98.107:5672;virtualHost=OrderQueue;username=zhangdi;password=123456"; string connString = "host=127.0.0.1:5672;virtualHost=text;username=zhangdi;password=123456"; if(string.IsNullOrEmpty(connString)) throw new Exception("messageserver connection string is missing or empty"); return RabbitHutch.CreateBus(connString); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MQ.Common { public interface IProcessMessage { void ProcessMsg(Message msg); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using EasyNetQ; namespace MQ.Common { public class Message { public string MessageID { get; set; } public string MessageTitle { get; set; } public string MessageBody { get; set; } public string MessageRouter { get; set; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using EasyNetQ; namespace MQ.Common { public class MQHelper { /// <summary> /// 发送消息 /// </summary> /// <param name="msg"></param> public static void Publish(Message msg) { // 创建消息bus IBus bus = BusBuilder.CreateMessageBus(); try { bus.Publish(msg, x => x.WithTopic(msg.MessageRouter)); } catch (EasyNetQException ex) { //处理连接消息服务器异常 } bus.Dispose();//与数据库connection类似,使用后记得销毁bus对象 } public static void Subscibe(Message msg, IProcessMessage ipro) { // 创建消息bus IBus bus = BusBuilder.CreateMessageBus(); try { bus.Subscribe<Message>(msg.MessageRouter, message => ipro.ProcessMsg(message), x => x.WithTopic(msg.MessageRouter)); } catch (EasyNetQException ex) { //处理连接消息服务器异常 } } } }
2,MQ.Consumer
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MQ.Common; namespace MQ.Consumer { public class OrderProcessMessage : IProcessMessage { public void ProcessMsg(Message msg) { Console.WriteLine(msg.MessageBody); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MQ.Common; namespace MQ.Consumer { class Program { static void Main(string[] args) { OrderProcessMessage order = new OrderProcessMessage(); Message msg = new Message(); msg.MessageID = "1"; msg.MessageRouter = "pcm.notice.zhangsan"; MQHelper.Subscibe(msg, order); Console.ReadLine(); } } }
3,MQ.Producer
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MQ.Common; namespace MQ.Producer { class Program { static void Main(string[] args) { Message msg = new Message(); msg.MessageID = "1"; msg.MessageBody = DateTime.Now.ToString(); msg.MessageTitle = "1"; msg.MessageRouter = "pcm.notice.zhangsan"; MQHelper.Publish(msg); Console.ReadLine(); } } }
当启动Consumer(消费者)时,会自动在RabbitMQ服务器上创建相关的exchange和queue。
注意:这个程序应该先启动Consumer(消费者)
案例下载: http://pan.baidu.com/s/1c1LY9gc
参考文档:
http://blog.csdn.net/seven_coder/article/details/50946562
http://www.cnblogs.com/zhangweizhong/p/5687457.html