Skip to content

This is a EventBus library for .net core . it's support localhost event and distributed event

License

Notifications You must be signed in to change notification settings

MapleWithoutWords/Cike.EventBus

Repository files navigation

Cike.EventBus

介绍

Cike.EventBus 是一个.net core中支持事件总线的一个组件,并且它还支持事件中间件拦截。目前仅支持本地事件,后续会扩展分布式事件。

软件架构

  • 本项目依赖于.net6

安装教程

dotnet add package Cike.EventBus

使用说明

功能说明
  1. Cike.EventBus 提供了两种事件总线接口它们分别是:

    • IDistributedEventBus 分布式事件总线 (分布式事件总线,目前默认还是调用本地时间总线,后续会对接分布式队列)

    • ILocalEventBus 本地事件总线

    订阅事件处理:允许多次订阅

    • IDistributedEventHandler<TEventData> 分布式事件处理器
    • ILocalEventHandler<TEventData> :本地事件处理器
  2. EventMiddleware:事件中间件,允许添加多个中间件,并按照添加顺序执行

    Cike.EventBus中的事件中间件,允许自定义中间件拦截事件命令,并且可以支持中断事件执行。可以用于一些事件日志的记录,校验等

  3. 事件编排:Cike.EventBus允许设置事件处理的执行顺序,比如说有多个事件处理Hanlder,那么可以配置执行的先后顺序

继承自IDistributedEventHandler或ILocalEventHandler时,必须实现ExecSeqNo属性,并赋值。

1. 事件总线使用
  1. Program.cs 中添加以下代码
builder.Services.AddCikeEventBus(opt =>
{
    //从某个程序集加载事件处理器
    opt.AddHandlerForAsemmbly(Assembly.Load("xxxx"));
});
  1. 定义一个事件对象类:UserDeletedEventArgs
public class UserDeletedEventArgs
{
    public int UserId { get; set; }
}
  1. 在业务代码中注入 IDistributedEventBus 对象(注:目前IDistributedEventBus还是走的本地事件),并在业务方法中发布事件
public class UserAppService : IAutoApiService
{
    private readonly IDistributedEventBus _distributedEventBus;

    public UserAppService(IDistributedEventBus distributedEventBus)
    {
        _distributedEventBus = distributedEventBus;
    }

    public async Task DeleteAsync(int userId)
    {
        Console.WriteLine($"用户id={userId}的用户被删除了");
        await _distributedEventBus.PublishAsync(new UserDeletedEventArgs
        {
            UserId = userId
        });

    }
}
  1. 定义一个事件处理函数:UserDeleted1Handler
public class UserDeleted1Handler : IDistributedEventHandler<UserDeletedEventArgs>
{
    /// <summary>
    /// 事件编排:执行序号
    /// </summary>
    public int ExecSeqNo { get; set; }
    private readonly ILogger _logger;

    public UserDeleted1Handler(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<UserDeleted1Handler>();
    }

    public async Task HandlerAsync(UserDeletedEventArgs eventData)
    {
        Console.WriteLine("UserDeleted1Handler===事件处理程序");
    }
}
2. 事件中间件使用
  1. 定义一个事件中间件 LoggerEventMiddleware
public class LoggerEventMiddleware : IEventMiddleware
{
    private readonly ILogger _logger;
    public LoggerEventMiddleware(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<LoggerEventMiddleware>();
    }
    public async Task InvokeAsync(EventMiddlewareContext context, EventMiddlewareDelegate next)
    {
        _logger.LogDebug($"事件中间件,记录日志:事件id=【{context.EventId}】、事件类型=【{context.EventType}】、事件处理器个数=【{context.EventHandlerFactories.Count}】");
        
        //一定要加next委托执行,不然将中断事件执行。
        await next(context);
    }
}
  1. 在添加服务配置的时候添加中间件
builder.Services.AddCikeEventBus(opt =>
{
    //从某个程序集加载事件处理器
    opt.AddHandlerForAsemmbly(Assembly.Load("xxxx"));
    //添加事件拦截中间件
    opt.UseEventMiddleware<LoggerEventMiddleware>();
});

About

This is a EventBus library for .net core . it's support localhost event and distributed event

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published