EasyNetQ-An open source library for using RabbitMQ's. NET API

EasyNetQ-An open source library for using RabbitMQ's. NET API

The goal of EasyNetQ is to provide a library for using RabbitMQ in. NETas simply as possible.

最后更新 7/26/2022 6:32 AM
黑哥聊dotNet
预计阅读 6 分钟
分类
.NET
标签
.NET C# open source

Part 1 Introduction

EasyNetQ 是用于RabbitMQ的简单易用 .NET API 。

如果您只想尽快启动并运行,请转到快速入门指南。

EasyNetQ 的目标是提供一个库,用于在 .NET 中使用 RabbitMQ 尽可能简单。为了做到这一点,它通过强制执行一些简单的约定来以灵活性换取简单性。这些包括:

  • 消息应该由 .NET 类型表示。
  • 消息应按其 .NET 类型进行路由。

这意味着消息是由 .NET 类定义的。您要发送的每种不同的消息类型都由一个类表示。该类必须是公共的,必须具有默认构造函数和公共读/写属性。您通常不会在消息中实现任何功能,而是将其视为简单的数据容器或数据传输对象 (DTO)。这是一个简单的消息:

public class MyMessage
{
    public string Text { get; set; }
}

EasyNetQ 按消息类型路由消息。 当您发布消息时,EasyNetQ 会检查其类型并根据类型名称、命名空间和程序集为其提供路由键(routing key)。 在消费端,订阅者订阅一个类型。 订阅一个类型后,该类型的消息会被路由到订阅者。

默认情况下,EasyNetQ 使用 Newtonsoft.Json 库将 .NET 类型序列化为 JSON。 这样做的好处是消息是人类可读的,因此您可以使用 RabbitMQ 管理应用程序等工具来调试消息问题。

Part2 API Design

EasyNetQ 是在 RabbitMQ.Client 库之上提供服务的组件集合。它们执行序列化、错误处理、线程编组、连接管理等操作。它们由 mini-IoC 容器组成。您可以很容易地用您自己的实现替换任何组件。因此,如果您想要 XML 序列化而不是内置 JSON,只需编写 ISerializer 的实现并将其注册到容器中。

这些组件以 IAdvancedBus API 为前端。这看起来很像 AMQP 规范,实际上您可以从这个 API 运行大多数 AMQP 方法。此 API 对您隐藏的唯一 AMQP 概念是通道。这是因为通道是一个令人困惑的低级概念,一开始就不应该成为 AMQP 规范的一部分。老实说,“高级”对于这个 API 来说并不是一个很好的名字,Iamqp会好得多。

位于高级 API 之上的是一组消息传递模式:发布/订阅请求/响应发送/接收。这是 EasyNetQ 的“主见”部分。这是我们对如何实施这些模式的看法。灵活性很小;要么你接受我们的做事方式,要么你不使用它。目的是您,用户,不必花费脑力去重新发明相同的模式;您不必在每次只想发布和订阅消息时都做出选择。它旨在实现 EasyNetQ 的核心目标,即尽可能轻松地使用 RabbitMQ

这些模式位于 IBus API 后面。再一次,这是一个糟糕的名字,它与消息总线的概念几乎没有关系。更好的名称是 IPackagedMessagePatterns

IBus 旨在为 80% 的用户、80% 的时间工作。这并不详尽。如果您要实现的模式不是由 IBus 提供的,那么您应该使用 IAdvancedBus。这样做没有问题,EasyNetQ 就是这样设计的。

Part3 Why do I need EasyNetQ?

RabbitMQ 不是已经有 .NET 客户端了吗?

是的,它确实。您可以在此处下载 .NET AMQP 客户端库。

那么为什么我需要 EasyNetQRabbitMQ .NET 客户端实现了 AMQP 协议的客户端(而 RabbitMQ 实现了服务器端)。AMQP 旨在作为消息传递的 HTTP。它被设计成跨平台和语言无关的。它还旨在灵活地支持基于 Exchange/Binding/Queue 模型的各种消息传递模式。

拥有这种灵活性很棒,但随着灵活性而来的是复杂性。这意味着您需要编写大量代码才能实现 RabbitMQ 客户端。通常,此代码将包括:

  • Implement messaging patterns such as publish/subscribe or request/response. Although, to be fair, the. NET client does provide some support here.
  • Implement routing strategies. How will you design switch queue bindings and route messages between producers and consumers? - Implement message serialization/deserialization. How will you convert the binary representation of messages in AMQP into something that your programming language can understand?
  • Implement consumer threads for subscriptions. You need to have a dedicated consumer loop waiting for the messages you subscribe to. What would you do with multiple or temporary subscribers, such as subscribers waiting for a response to a request?
  • Enable subscriber reconnection. If the connection is lost or the RabbitMQ server bounces, how do you detect it and ensure that all subscriptions are rebuilt?
  • Understand and implement service quality settings. What settings do you need to make to ensure that you have a reliable client?
  • Implement error handling strategies. What should you do if your client receives a malformed message or throws an unexpected exception?
  • Achieve reliable messaging for publisher confirmation.

EasyNetQ aims to encapsulate all these issues in a simple and easy-to-use library that sits on top of existing AMQP clients. It is the result of several years of experience in using RabbitMQ in high-volume commercial environments.

Part4 Simple Use

Received RabbitMQ agent

var bus = RabbitHutch.CreateBus("host=localhost");

released a message

await bus.PubSub.PublishAsync(message);

Post a 5-second delay message

await bus.Scheduler.FuturePublishAsync(message, TimeSpan.FromSeconds(5));

subscribe message

await bus.PubSub.SubscribeAsync<MyMessage>(
    "my_subscription_id", msg => Console.WriteLine(msg.Text)
);

RPC server

await bus.Rpc.RespondAsync<TestRequestMessage, TestResponseMessage>(request =>
    new TestResponseMessage{ Text = request.Text + " all done!" }
);

Part5 Address

  • Original link: github.com/EasyNetQ/EasyNetQ/wiki/Introduction
  • Warehouse link: github.com/EasyNetQ/EasyNetQ

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读
同分类 / 同标签 2/7/2026

Summary of experience in using AOT

From the very beginning of project creation, you should develop a good habit of conducting AOT release testing in a timely manner whenever new features are added or newer syntax is used.

继续阅读