.Net Core中使用RabbitMQ

(1)、引入依赖

RabbitMQ.Client

(2)、编写发布者代码

 1             var connectionFactory = new ConnectionFactory() { HostName="192.168.205.128",UserName="guest",Password="guest"};  //创建连接工厂
 2             var connection = connectionFactory.CreateConnection();  //创建connection
 3             var channel = connection.CreateModel(); //创建channel
 4             //声明交换机
 5             //String exchange,   交换机名称
 6             //String type,  交换机类型
 7             //Boolean durable, 是否持久化
 8             //Boolean autoDelete,   是否自动删除
 9             //IDictionary< String, Object > arguments   交换机参数
10             channel.ExchangeDeclare("netExchange", ExchangeType.Direct, true, false, null);
11             //声明队列
12             //String queue, 队列名称
13             //Boolean durable,  是否持久化
14             //Boolean exclusive,    是否专有的(排外)
15             //Boolean autoDelete,   是否自动删除
16             //IDictionary<String, Object> arguments 队列参数
17             channel.QueueDeclare("netQueue", true, false, false, null);
18             //将队列绑定到交换机上
19             //String queue,   队列名称  
20             //String exchange,  交换机名称
21             //String routingKey,    routingKey
22             //IDictionary< String, Object > arguments   绑定参数
23             channel.QueueBind("netQueue", "netExchange", "netMessage", null);
24             //发布消息
25             //String exchange,  交换机名称
26             //String routingKey,    routingKey
27             //IBasicProperties basicProperties,     发布属性
28             //Byte[] body   消息内容
29             channel.BasicPublish("netExchange", "netMessage", null, Encoding.UTF8.GetBytes("来自.net的问候"));
30             Console.ReadKey();

(3)、编写消费者代码

 1             var connectionFactory = new ConnectionFactory() { HostName = "192.168.205.128", UserName = "guest", Password = "guest" };  //创建连接工厂
 2             var connection = connectionFactory.CreateConnection();  //创建connection
 3             var channel = connection.CreateModel(); //创建channel
 4             //声明交换机
 5             //String exchange,   交换机名称
 6             //String type,  交换机类型
 7             //Boolean durable, 是否持久化
 8             //Boolean autoDelete,   是否自动删除
 9             //IDictionary< String, Object > arguments   交换机参数
10             channel.ExchangeDeclare("netExchange", ExchangeType.Direct, true, false, null);
11             //声明队列
12             //String queue, 队列名称
13             //Boolean durable,  是否持久化
14             //Boolean exclusive,    是否专有的(排外)
15             //Boolean autoDelete,   是否自动删除
16             //IDictionary<String, Object> arguments 队列参数
17             channel.QueueDeclare("netQueue", true, false, false, null);
18             //将队列绑定到交换机上
19             //String queue,   队列名称  
20             //String exchange,  交换机名称
21             //String routingKey,    routingKey
22             //IDictionary< String, Object > arguments   绑定参数
23             channel.QueueBind("netQueue", "netExchange", "netMessage", null);
24             //1.直接获取消息
25             //var result = channel.BasicGet("netQueue", true);
26             //Console.WriteLine(Encoding.UTF8.GetString(result.Body));
27             //2.使用事件机制获取消息
28             EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
29             consumer.Received += (sender, e) =>
30             {
31                 Console.WriteLine(Encoding.UTF8.GetString(e.Body));
32             };
33             channel.BasicConsume("netQueue", true, consumer);
34             Console.ReadKey();

 

转载于:https://www.cnblogs.com/fanqisoft/p/10388793.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值