• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Brighter and RabbitMQ: How to setup and use Brighter with RabbitMQ

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
Introduction to RabbitMQ


RabbitMQ is one of the most widely used message brokers, implementing the Advanced Message Queuing Protocol (AMQP) to ensure reliable message delivery. Built on the Erlang programming language, it offers high scalability and fault tolerance, making it ideal for distributed systems. RabbitMQ acts as an intermediary that accepts messages from producers and delivers them to consumers, decoupling services in a microservices architecture.

Requirement

Brighter Recap


Before continuing about AWS SNS/SQS configuration, let's recap what we already know about Brighter.

Request (Command/Event)


Define messages using IRequest:


public class Greeting() : Event(Guid.NewGuid())
{
public string Name { get; set; } = string.Empty;
}
  • Commands: Single-recipient operations (e.g., SendEmail).
  • Events: Broadcast notifications (e.g., OrderShipped).
Message Mapper


Translates between Brighter messages and your app objects:


public class GreetingMapper : IAmAMessageMapper<Greeting>
{
public Message MapToMessage(Greeting request)
{
var header = new MessageHeader();
header.Id = request.Id;
header.TimeStamp = DateTime.UtcNow;
header.Topic = "greeting.topic"; // The target topic to be publish
header.MessageType = MessageType.MT_EVENT;

var body = new MessageBody(JsonSerializer.Serialize(request));
return new Message(header, body);
}

public Greeting MapToRequest(Message message)
{
return JsonSerializer.Deserialize<Greeting>(message.Body.Bytes)!;
}
}
Request Handler


Processes incoming messages:


public class GreetingHandler(ILogger<GreetingHandler> logger) : RequestHandler<Greeting>
{
public override Greeting Handle(Greeting command)
{
logger.LogInformation("Hello {Name}", command.Name);
return base.Handle(command);
}
}
Configuring Brighter with RabbitMQ

1. Connection Setup


Define RabbitMQ connection details:


var connection = new RmqMessagingGatewayConnection
{
AmpqUri = new AmqpUriSpecification(new Uri("amqp://guest:guest@localhost:5672")),
Exchange = new Exchange("paramore.brighter.exchange"),
};
2. RabbitMQ Subscription


Subscribe to a queue/topic:


.AddServiceActivator(opt =>
{
opt.Subscriptions = [
new RmqSubscription<Greeting>(
new SubscriptionName("kafka.greeting.subscription"),
new ChannelName("greeting.queue"),
new RoutingKey("greeting.topic"),
makeChannels: OnMissingChannel.Create
),
];

opt.ChannelFactory = new ChannelFactory(
new RmqMessageConsumerFactory(connection)
);
})
3. RabbitMQ Producer Configuration


Publish events to a topic:


.UseExternalBus(new RmqProducerRegistryFactory(connection, new[]
{
new RmqPublication
{
Topic = new RoutingKey("greeting.topic"),
MakeChannels = OnMissingChannel.Create
}
}).Create());
Best Practices

  • Dead Letter Queues (DLQs): Configure DLQs in RabbitMQ to handle poisoned messages. Use deadLetterChannelName and deadLetterRoutingKey in your queue declarations.
  • Monitoring: Use RabbitMQ’s management plugin to track queue health and performance.
  • Error Handling: Implement retries and circuit breakers to handle transient failures (Brighter has retry and circuit breakers via attributes, I'll show it in other article).
Conclusion


Integrating Brighter with RabbitMQ enables robust, scalable messaging in .NET applications. By leveraging RabbitMQ’s AMQP protocol and Brighter’s abstraction layer, you can:

  • Decouple services with publish-subscribe patterns.
  • Ensure message ordering and reliability.
  • Simplify error handling with DLQs and retries.

For production use, validate RabbitMQ configurations against Brighter’s latest documentation, as features like message prioritization or delayed exchanges may evolve.

Reference



Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу