标签:登陆 ima 用户名 oca orm 监听 password bit private
上篇我们说到erlang的安装,现在有了基础前提,就可以继续安装RabbitMQ了!
这里我选用的RabbitMQ版本是:
PS:这个RabbitMQ版本是要对应前面erlang版本,所以前面我们安装的版本是21.2,因为需要提供21.X版本的erlang才能安装 rabbitmq-server-3.7.10
1.安装RabbitMQ
下载地址:http://www.rabbitmq.com/install-windows.html
download完,执行exe文件,安装到自己选用的目录,并配置环境变量
rabbitmq的基本操作:
2.配置rabbitmq网页管理插件
以管理员运行命令提示启用插件:
rabbitmq-plugins enable rabbitmq_management
打开浏览器页面:http://localhost:15672 可以看到
默认登陆为:guest/guest
3.开启rabbitMQ远程访问
rabbitmqctl add_user XRom XRom123
rabbitmqctl set_permissions -p "/" XRom ".*" ".*" ".*"
rabbitmqctl set_user_tags XRom administrator
然后就可以远程访问了,可以用新增的用户登录RabbitMQ
4.Producer与Exchange
新建.net core 控制台项目,并引入NuGet包
接下来可以用代码看实现效果:
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
using System.Threading;
namespace RabbitMQConsole
{
class Program
{
/// <summary>
/// 创建只读连接对象
/// </summary>
private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory()
{
HostName = "",//这里写自己电脑hostname,可以通过命令提示符,直接输入hostname查询
Port = 5672,
UserName = "XRom",
Password = "XRom123",
VirtualHost = "/"
};
static void Main(string[] args)
{
var exchange = "change2";
var route = "route2";
var queue = "queue2";
using (IConnection conn = rabbitMqFactory.CreateConnection())
{
using (IModel channel = conn.CreateModel())
{
channel.ExchangeDeclare(exchange, type: "direct", durable: true, autoDelete: false);//创建change2
channel.QueueDeclare(queue, durable: true, exclusive: false, autoDelete: false);//创建queue2
channel.QueueBind(queue, exchange, route);//将queue2绑定到change2
#region 发送消息
var props = channel.CreateBasicProperties();
props.Persistent = true; //持久化
channel.BasicPublish(exchange, route, true, props, Encoding.UTF8.GetBytes("hello rabbitmq!!"));
#endregion
#region 消费消息
//while (true)
//{
// var message = channel.BasicGet(queue, true); //第二个参数说明自动释放消息,如为false需手动释放消息
// if (message != null)
// {
// var msgBody = Encoding.UTF8.GetString(message.Body);
// Console.WriteLine(string.Format("***接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody));
// }
// System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
//}
#endregion
#region 让失败的消息回到队列中
//while (true)
//{
// var message = channel.BasicGet(queue, false);
// if (message != null)
// {
// var msgBody = Encoding.UTF8.GetString(message.Body);
// Console.WriteLine(string.Format("***接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody));
// Console.WriteLine(message.DeliveryTag); //当前消息被处理的次序数
// if (1 == 1)
// channel.BasicReject(message.DeliveryTag, true);
// }
// System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
//}
#endregion
#region 监听消息
//channel.BasicQos(prefetchSize: 0, prefetchCount: 20, global: false); //一次接受10条消息,否则rabbit会把所有的消息一次性推到client,会增大client的负荷
//EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
//consumer.Received += (model, ea) =>
//{
// Byte[] body = ea.Body;
// String message = Encoding.UTF8.GetString(body);
// Console.WriteLine(message + Thread.CurrentThread.ManagedThreadId);
// channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
//};
//channel.BasicConsume(queue: queue, autoAck: false, consumer: consumer);
//Console.ReadLine();
#endregion
}
}
}
}
}
标签:登陆 ima 用户名 oca orm 监听 password bit private
原文地址:https://www.cnblogs.com/ButterflyEffect/p/10245898.html