.NET6中 RabbitMQ的使用

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="RabbitMQ.Client" Version="6.2.4" />
	  <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" />
	  <PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="6.0.3" />
	  <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.3">
		  <PrivateAssets>all</PrivateAssets>
		  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
	  </PackageReference>
	  <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.3" />
	  <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.3" />
	  <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.3">
		  <PrivateAssets>all</PrivateAssets>
		  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
	  </PackageReference>
	  <PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
	  <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
	  <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
	  <PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
	  <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
	  <PackageReference Include="NModbus4.NetCore" Version="2.0.1" />
	  <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.1" />
  </ItemGroup>

</Project>

需要配置环境变量

erlanghome/bin

在安装目录

C:\Program Files\RabbitMQ Server\rabbitmq_server-3.9.15\sbin

下运行

rabbitmq-plugins enable rabbitmq_management

输入

rabbitmqctl status检查

服务-停止 rabbitmq

再在 目录下运行

rabbitmq-server.bat

再在server打开rabbitmq

浏览器输入

http://localhost:15672/

帐号密码guest

生产者

using RabbitMQ.Client;
using System.Text;

var conFactory = new ConnectionFactory();

conFactory.HostName = "localhost";
//conFactory.Port = 15672;
conFactory.DispatchConsumersAsync = true;
string exchangeName = "exchange1";
var connection = conFactory.CreateConnection();

while (true)
{
    using var channel = connection.CreateModel();
    var prop = channel.CreateBasicProperties();
    prop.DeliveryMode = 2;
    channel.ExchangeDeclare(exchangeName, "direct");
    byte[] bytes = Encoding.UTF8.GetBytes(DateTime.Now.ToString());
    channel.BasicPublish(exchangeName, routingKey: "key1", mandatory: true, basicProperties: prop, body: bytes);
    Console.WriteLine("Ok"+DateTime.UtcNow);
    Thread.Sleep(1000);
}

消费者

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;

var connfactory = new ConnectionFactory();
connfactory.HostName = "localhost";
connfactory.DispatchConsumersAsync = true;
var connection = connfactory.CreateConnection();
string exchangeName = "exchange1";
string queueName = "queue1";
string routingkey = "key1";

using var channel = connection.CreateModel();

channel.ExchangeDeclare(exchangeName, "direct");
channel.QueueDeclare(queueName, durable: true,
        exclusive: false, autoDelete: false, arguments: null);
channel.QueueBind(queue: queueName,
    exchange: exchangeName, routingKey: routingkey);

AsyncEventingBasicConsumer consumer = new AsyncEventingBasicConsumer(channel);
consumer.Received += Consumer_Received;

channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer);
Console.WriteLine("按回车退出");
Console.ReadLine();
async Task Consumer_Received(object sender, BasicDeliverEventArgs args)
{
    try
    {
        var bytes = args.Body.ToArray();
        string msg = Encoding.UTF8.GetString(bytes);
        Console.WriteLine(DateTime.Now + "收到了消息" + msg);
        channel.BasicAck(args.DeliveryTag, multiple: false);
        await Task.Delay(800);
    }
    catch (Exception ex)
    {
        channel.BasicReject(args.DeliveryTag, true);
        Console.WriteLine("处理收到的消息出错" + ex);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值