RabbitMQ跟Redis做消息队列的区别

区别

https://www.zhihu.com/question/20795043

https://blog.csdn.net/dd18709200301/article/details/79077839

RabbitMQ安装

https://blog.csdn.net/weixin_39735923/article/details/79288578

然后http://localhost:15672 进得去 说明安装成功

RabbitMQ跟Redis做消息队列的区别

有三种模式  fanout , direct topic

fanout 发送到所有的队列

direct 可以设置key 发送到对应的队列

topic 第二种的增强版   key支持模糊匹配

自己创建账号  不要用guest账号(应用程序跟RabbitMQ在同一个电脑上  可以用guest登陆     guest账号给限制了  无法通过外网登陆   )

账号创建完成需要设置账号权限才可以使用

1.生产者

            //创建连接工厂
ConnectionFactory factory = new ConnectionFactory
{
//这是默认帐号密码
UserName = "guest",//用户名
Password = "guest",//密码
HostName = "localhost"//"192.168.157.130"//rabbitmq ip
}; //创建连接
var connection = factory.CreateConnection();
//创建通道
var channel = connection.CreateModel();
//声明一个队列
channel.QueueDeclare("hello", false, false, false, null); Console.WriteLine("\nRabbitMQ连接成功,请输入消息,输入exit退出!"); string input;
do
{
input = Console.ReadLine(); var sendBytes = Encoding.UTF8.GetBytes(input);
//发布消息到hello队列
channel.BasicPublish("", "hello", null, sendBytes); } while (input.Trim().ToLower() != "exit");
channel.Close();
connection.Close();

2.消费者

            //创建连接工厂
ConnectionFactory factory = new ConnectionFactory
{
UserName = "guest",//用户名
Password = "guest",//密码
HostName = "localhost"//"192.168.157.130"//rabbitmq ip
}; //创建连接
var connection = factory.CreateConnection();
//创建通道
var channel = connection.CreateModel(); //事件基本消费者
EventingBasicConsumer consumer = new EventingBasicConsumer(channel); //接收到消息事件
consumer.Received += (ch, ea) =>
{
var message = Encoding.UTF8.GetString(ea.Body);
Console.WriteLine($"收到消息: {message}"); //确认该消息已被消费
channel.BasicAck(ea.DeliveryTag, false);
};
//启动消费者 设置为手动应答消息
//监听hello队列
channel.BasicConsume("hello", false, consumer);
Console.WriteLine("消费者已启动");
Console.ReadKey();
channel.Dispose();
connection.Close();

  

channel.BasicAck(ea.DeliveryTag, false);
执行这句话就表示当前这条记录已经被消费 可以在这句话之前执行调用接口 或者某些数据库操作 成功在执行这句话 不成功该条信息还会放回队列中 上面这种写法是生产者直接把消息直接放到某个队列中 一般我们不会这样做 实际上都是生产者将消息发送到Exchange交换器中 由它将消息路由到一个或多个队列(Queue)中
            string exchangeName = "TestFanoutChange";
string queueName1 = "hello1";
string queueName2 = "hello2";
string routeKey = "";
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory
{
//这是默认帐号密码
UserName = "guest",//用户名
Password = "guest",//密码
HostName = "localhost"//"192.168.157.130"//rabbitmq ip
}; //创建连接
var connection = factory.CreateConnection();
//创建通道
var channel = connection.CreateModel();
//定义一个Direct类型交换机
channel.ExchangeDeclare(exchangeName, ExchangeType.Fanout, false, false, null); //定义队列1
channel.QueueDeclare(queueName1, false, false, false, null);
//定义队列2
channel.QueueDeclare(queueName2, false, false, false, null); //将队列绑定到交换机
channel.QueueBind(queueName1, exchangeName, routeKey, null);
channel.QueueBind(queueName2, exchangeName, routeKey, null); //生成两个队列的消费者
ConsumerGenerator(queueName1);
ConsumerGenerator(queueName2); Console.WriteLine("\nRabbitMQ连接成功,请输入消息,输入exit退出!"); string input;
do
{
input = Console.ReadLine(); var sendBytes = Encoding.UTF8.GetBytes(input);
//发布消息到exchangeName交换机上 绑定到这个交换机上的所有队列都会产生信息
channel.BasicPublish(exchangeName, routeKey, null, sendBytes); } while (input.Trim().ToLower() != "exit");
channel.Close();
connection.Close();
        static void ConsumerGenerator(string queueName)
{
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory
{
UserName = "guest",//用户名
Password = "guest",//密码
HostName = "localhost"//rabbitmq ip
}; //创建连接
var connection = factory.CreateConnection();
//创建通道
var channel = connection.CreateModel(); //事件基本消费者
EventingBasicConsumer consumer = new EventingBasicConsumer(channel); //接收到消息事件
consumer.Received += (ch, ea) =>
{
var message = Encoding.UTF8.GetString(ea.Body); Console.WriteLine($"Queue:{queueName}收到消息: {message}");
//确认该消息已被消费
channel.BasicAck(ea.DeliveryTag, false);
};
//启动消费者 设置为手动应答消息
channel.BasicConsume(queueName, false, consumer);
Console.WriteLine($"Queue:{queueName},消费者已启动");
}
更具体的 见  https://www.cnblogs.com/stulzq/p/7551819.html
上一篇:Java使用foreach遍历集合元素


下一篇:【跟着*学Pandas】Select rows from a DataFrame based on values in a column -pandas 筛选