标签:管理 exe 增加用户 blank date ada data 发送消息 microsoft
随着公司加大力度信息化建设,PCB企业各种各样的系统软件越来越多,整个公司订单流状态监控变得越来越不可控,是时候需采用新的方式来收集各系统状态节点状态了,以下记录RabbitMQ安装使用:
RabbitMQ作为一个消息代理,主要和消息打交道,负责接收并转发消息。RabbitMQ提供了可靠的消息机制、跟踪机制和灵活的消息路由,支持消息集群和分布式部署。适用于排队算法、秒杀活动、消息分发、异步处理、数据同步、处理耗时任务、CQRS等应用场景
1.安装Erlang运行环境
Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装Rabbit MQ的前提是安装Erlang
下载并安装 Erlang OTP For Windows (vR16B03)
2.安装RabbitMQ
下载并安装 Rabbit MQ Server Windows Installer (v3.2.3)
3.安装管理平台插件
rabbitmq-plugins enable rabbitmq_management
4.重启RabbitMQ服务
net stop RabbitMQ && net start RabbitMQ
5.安装RabbitMQ界面
安装插件与重启服务
MQ启动
MQ服务:
1.RabbitMQ网址:
默认账号和密码:guest guest
2.主界面
3.增加用户
4.配置用户权限
1.发送消息
/// <summary> /// 连接配置 /// </summary> private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory() { HostName = "127.0.1.1", UserName = "pcbren", Password = "abc+2015", Port = 5672 }; /// <summary> /// 路由名称 /// </summary> const string ExchangeName = "pcbren.exchange"; /// <summary> ///队列名称 /// </summary> const string QueueName = "pcbren.queue"; static void Main(string[] args) { using (IConnection conn = rabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null); channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null); channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName); var props = channel.CreateBasicProperties(); props.Persistent = true; string vadata = Console.ReadLine(); while (vadata != "exit") { var msgBody = Encoding.UTF8.GetBytes(vadata); channel.BasicPublish(exchange: ExchangeName, routingKey: QueueName, basicProperties: props, body: msgBody); Console.WriteLine(string.Format("发送时间:{0},发送完成", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); vadata = Console.ReadLine(); } } } }
2.接收消息
/// <summary> /// 连接配置 /// </summary> private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory() { HostName = "127.0.1.1", UserName = "pcbren", Password = "abc+2015", Port = 5672 }; /// <summary> /// 路由名称 /// </summary> const string ExchangeName = "pcbren.exchange"; /// <summary> ///队列名称 /// </summary> const string QueueName = "pcbren.queue"; static void Main(string[] args) { using (IConnection conn = rabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { channel.QueueDeclare(QueueName, durable: true, autoDelete: false, exclusive: false, arguments: null); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, MQ) => { var msgBody = Encoding.UTF8.GetString(MQ.Body); Console.WriteLine(string.Format("接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody)); channel.BasicAck(deliveryTag: MQ.DeliveryTag, multiple: false); //消息确认后,MQ服务端才将此消息清除 }; channel.BasicConsume(QueueName, false, consumer: consumer); Console.ReadKey(); } } }
使用消列队列的好处就是它能够并行的处理队列。当一个消息接受端处理不过来时,我们只需要增加多个消息接收处理者。下图展示两个消息接收端,等待消息接收,再启动一个消息发送端进行消息发送。
从下图中可知,循环发送6条信息依次的分派给2个客户端,每个客户端平均接收到3条消息
默认情况下:RabbitMQ这种分发消息的方式叫做循环(round-robin)。
标签:管理 exe 增加用户 blank date ada data 发送消息 microsoft
原文地址:https://www.cnblogs.com/pcbren/p/10503836.html