![]() |
VOOZH | about |
dotnet add package TJC.Cyclops.EventBus --version 2026.6.11.2
NuGet\Install-Package TJC.Cyclops.EventBus -Version 2026.6.11.2
<PackageReference Include="TJC.Cyclops.EventBus" Version="2026.6.11.2" />
<PackageVersion Include="TJC.Cyclops.EventBus" Version="2026.6.11.2" />Directory.Packages.props
<PackageReference Include="TJC.Cyclops.EventBus" />Project file
paket add TJC.Cyclops.EventBus --version 2026.6.11.2
#r "nuget: TJC.Cyclops.EventBus, 2026.6.11.2"
#:package TJC.Cyclops.EventBus@2026.6.11.2
#addin nuget:?package=TJC.Cyclops.EventBus&version=2026.6.11.2Install as a Cake Addin
#tool nuget:?package=TJC.Cyclops.EventBus&version=2026.6.11.2Install as a Cake Tool
📢 Cyclops世界的"快递中转站" 📢
想象一下,你的应用程序就像一个繁忙的写字楼:订单模块创建了订单,需要通知库存模块扣减库存、通知邮件模块发送确认邮件、通知日志模块记录操作...如果每个模块都要直接找对方"喊话",系统很快就会变成一团乱麻。
Cyclops.EventBus 就是这座写字楼的"智能快递系统"——谁有事要通知,只需把"包裹"(事件)扔进快递站,快递员会自动把包裹送到所有关心这件事的人手中。发送者不用知道谁在接收,接收者不用知道谁发送的,大家各忙各的,互不干扰。
Channels 实现,高性能、低依赖Cyclops.Common/EventBus/ # "快递单模板"(接口定义)
├── IEventBus.cs # 快递站门面:发件、订阅、退订
├── IEventData.cs # 包裹标签:事件名称、发生时间、来源
├── IEventHandler.cs # 收件人签名:怎么处理包裹、处理失败怎么办
└── IEventChannel.cs # 传送带:把包裹从发货口运到收货口
Cyclops.EventBus/ # "快递站实体建筑"
├── Core/
│ ├── EventBus.cs # 快递总站:调度一切
│ ├── EventChannel.cs # 自动传送带:基于 Channel 的高效传输
│ ├── EventPersistence.cs # 备份仓库:可选的事件存档
│ └── SubscriptionInfo.cs # 订阅登记簿:谁关心什么事件
├── Models/
│ ├── BaseEventData.cs # 标准包裹箱:事件数据的默认实现
│ └── EventBusOptions.cs # 站点配置:队列容量、是否存档等
└── Extensions/
└── EventBusServiceExtensions.cs # 开业指南:DI 注册扩展方法
using Cyclops.EventBus.Extensions;
// 在 ConfigureServices 中
services.AddEventBus(options =>
{
options.MaxQueueCapacity = 10000; // 传送带最多堆多少包裹
options.EnablePersistence = true; // 要不要留个底单存档
});
// 注册你的"收件人"
services.AddEventHandler<OrderCreatedEvent, OrderCreatedEventHandler>();
// 或者一键扫描整个程序集,自动注册所有收件人
services.AddEventHandlers();
using Cyclops.EventBus.Models;
// 订单创建事件——这就是你的包裹箱
public class OrderCreatedEvent : BaseEventData
{
public long OrderId { get; set; }
public string CustomerName { get; set; }
public decimal TotalAmount { get; set; }
public OrderCreatedEvent() : base("OrderCreated") { }
}
using Cyclops.Common.EventBus;
public class OrderCreatedEventHandler : IEventHandler<OrderCreatedEvent>
{
private readonly IEmailService _emailService;
private readonly IInventoryService _inventoryService;
public OrderCreatedEventHandler(IEmailService emailService, IInventoryService inventoryService)
{
_emailService = emailService;
_inventoryService = inventoryService;
}
public async Task HandleAsync(OrderCreatedEvent eventData)
{
// 收到包裹后,发邮件通知客户
await _emailService.SendOrderConfirmationAsync(eventData.CustomerName, eventData.OrderId);
// 通知仓库扣减库存
await _inventoryService.DeductStockAsync(eventData.OrderId);
}
public async Task OnErrorAsync(Exception ex)
{
// 如果处理包裹时出了岔子,比如邮件服务器挂了
// 这里可以记录日志、发送告警,甚至把包裹转交给人工处理
Console.WriteLine($"处理订单 {eventData?.OrderId} 时出错了:{ex.Message}");
}
}
using Cyclops.Common.EventBus;
public class OrderService
{
private readonly IEventBus _eventBus;
public OrderService(IEventBus eventBus)
{
_eventBus = eventBus;
}
public async Task CreateOrderAsync(string customerName, decimal amount)
{
// 1. 把订单存进数据库(主业务逻辑)
var orderId = await SaveOrderToDatabaseAsync(customerName, amount);
// 2. 把"订单已创建"的消息丢进快递站
// 这一步不会阻塞,马上返回,邮件和库存处理在后台慢慢跑
await _eventBus.PublishAsync(new OrderCreatedEvent
{
OrderId = orderId,
CustomerName = customerName,
TotalAmount = amount
});
}
}
// 系统启动时只想执行一次的初始化任务
_eventBus.SubscribeOnce<SystemStartupEvent, SystemInitHandler>();
int subscriberCount = _eventBus.GetSubscriberCount<OrderCreatedEvent>();
Console.WriteLine($"有 {subscriberCount} 个模块在关注订单创建事件");
// 某个模块下线了,不再接收这类事件
_eventBus.Unsubscribe<OrderCreatedEvent>();
public class EventBusOptions
{
/// 传送带最大容量,默认能堆 10000 个包裹
/// 超过这个数新包裹会排队等待(不会丢失)
public int MaxQueueCapacity { get; set; } = 10000;
/// 是否开启存档,默认开启
/// 开启后每个事件都会留个底单,方便事后查账
public bool EnablePersistence { get; set; } = true;
/// 轮询间隔(毫秒),默认 100ms
/// 传送带多久去"仓库"看一次有没有新包裹
public int Sensitivities { get; set; } = 100;
/// 最大并发处理数,默认 4
/// 同时能有几个人在拆包裹
public int MaxDegreeOfParallelism { get; set; } = 4;
}
| 场景 | 用还是不用 | 原因 |
|---|---|---|
| 用户注册后,需要同时发邮件、发短信、加积分 | ✅ 用 | 这三个操作互不影响,用 EventBus 并行处理更快 |
| 订单支付后,需要扣库存、生成物流单、通知仓库 | ✅ 用 | 下游系统可能有多个,用 EventBus 解耦 |
| 简单的 CRUD,一次数据库操作就完事 | ❌ 不用 | 杀鸡焉用牛刀,直接写就行 |
| 强一致性要求(比如转账扣款) | ❌ 不用 | EventBus 是异步的,不适合需要即时确认的场景 |
OrderCreated(订单已创建)、PaymentReceived(款项已收到),表示"已经发生了的事实"OnErrorAsync,至少把错误记下来,不然出了问题找不到原因欢迎提交 Issue 或 Pull Request!
MIT License
Cyclops.EventBus —— 让业务模块之间像发微信一样简单沟通,告别"你找我、我找它"的混乱局面!✨
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 1 NuGet packages that depend on TJC.Cyclops.EventBus:
| Package | Downloads |
|---|---|
|
TJC.Cyclops.ApprovalFlow
企服版框架中审批流开发套件 |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2026.6.11.2 | 181 | 6/11/2026 |
| 2026.6.11.1 | 199 | 6/11/2026 |
| 2026.6.9.4 | 179 | 6/9/2026 |
| 2026.6.9.3 | 195 | 6/9/2026 |
| 2026.6.9.2 | 192 | 6/9/2026 |
| 2026.6.9.1 | 188 | 6/9/2026 |
| 2026.6.8.3 | 213 | 6/8/2026 |
| 2026.6.8.2 | 166 | 6/8/2026 |
| 2026.6.8.1 | 174 | 6/8/2026 |
| 2026.6.5.1 | 170 | 6/5/2026 |
Cyclops框架中的事件总线,让业务模块之间像发微信一样简单沟通,告别"你找我、我找它"的混乱局面