<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);
}
}