1.加入依赖
RabbitMQ.Client
2.编写生产者
using RabbitMQ.Client;
namespace rabbitmq.config
{
public class MqHelper
{
private static ConnectionFactory? _factory;
public static ConnectionFactory GetFactory() {
if (_factory == null) {
ConnectionFactory factory = new ConnectionFactory();
factory.Uri = new Uri("amqp://guest:[email protected]:5672/");
_factory = factory;
}
return _factory;
}
}
}
namespace rabbitmq.config
{
public class QueueConfig
{
public static string QueueName = "mq_test_queue";
public static string ExchangeName = "mq_test_queue_exchange";
public static string RoutingKey = "mq_test_queue";
}
}
using RabbitMQ.Client;
namespace rabbitmq.config
{
public class SendMsg
{
public static void send() {
IConnection conn = MqHelper.GetFactory().CreateConnection();
IModel channel = conn.CreateModel();
channel.ExchangeDeclare(QueueConfig.ExchangeName, ExchangeType.Direct);
channel.QueueDeclare(QueueConfig.QueueName, false, false, false, null);
channel.QueueBind(QueueConfig.QueueName, QueueConfig.ExchangeName, QueueConfig.RoutingKey, null);
for (int i = 0; i < 10; i++) {
byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("Hello, world!-"+i);
channel.BasicPublish(QueueConfig.ExchangeName, QueueConfig.RoutingKey, null, messageBodyBytes);
Thread.Sleep(1000);
}
}
}
}
using Microsoft.AspNetCore.Mvc;
using rabbitmq.config;
namespace rabbitmq.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class MyController : ControllerBase
{
[HttpGet(Name = "my")]
public object Get()
{
//MqListener.OpenThread();
SendMsg.send();
return "success";
}
}
}
3.编写消费端
using RabbitMQ.Client;
namespace rabbitmq.config
{
public class MqHelper
{
private static ConnectionFactory? _factory;
public static ConnectionFactory GetFactory() {
if (_factory == null) {
ConnectionFactory factory = new ConnectionFactory();
factory.Uri = new Uri("amqp://guest:[email protected]:5672/");
_factory = factory;
}
return _factory;
}
}
}
namespace rabbitmq.config
{
public class QueueConfig
{
public static string QueueName = "mq_test_queue";
public static string ExchangeName = "mq_test_queue_exchange";
public static string RoutingKey = "mq_test_queue";
}
}
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
namespace rabbitmq.config
{
public class MqListener : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
IConnection conn = MqHelper.GetFactory().CreateConnection();
IModel channel = conn.CreateModel();
channel.ExchangeDeclare(QueueConfig.ExchangeName, ExchangeType.Direct);
channel.QueueDeclare(QueueConfig.QueueName, false, false, false, null);
channel.QueueBind(QueueConfig.QueueName, QueueConfig.ExchangeName, QueueConfig.RoutingKey, null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (ch, ea) =>
{
var body = ea.Body.ToArray();
string str = System.Text.Encoding.UTF8.GetString(body);
Console.WriteLine("接受到的mq消息:" + str);
channel.BasicAck(ea.DeliveryTag, false);
//Thread.Sleep(1000);
};
string consumerTag = channel.BasicConsume(QueueConfig.QueueName, false, consumer);
}
}
}
using rabbitmq.config;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHostedService<MqListener>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
4.启动2个项目