Skip to content

EventBus is a .Net Standard library to achieve eventually consistent in distributed architectures system like SOA,MicroService. It is lightweight,easy to use and efficiently.

License

Notifications You must be signed in to change notification settings

FeiniuBus/EventBus

Repository files navigation

EventBus

Branch Pipeline
master Build statusBuild Status
release Build statusBuild status
milestone/1.0.0 Build statusBuild Status

OverView

EventBus is a .Net Standard library to achieve eventually consistent in distributed architectures system like SOA,MicroService. It is lightweight,easy to use and efficiently.

Getting Started

Step 1 : Configure EventBus Options

*** Add following code after services.AddDbContext in StartUp.cs ***

services.AddEventBus(options =>
{
  // using EntityFramework
  options.UseEntityframework<**Your DbContext Type**>();
  
  // OR using Ado.NET 
  options.UseMySQL(**Database connection string**)
  
  // using RabbitMQ
  options.UseRabbitMQ(rabbit =>
  {
    rabbit.HostName = "**Your hostname of RabbitMQ**";
    rabbit.UserName = "**Your username if needed**";
    rabbit.Password = "**Your password if needed**";
  });
 });

*** Add following codes in Configure scope of StartUp.cs ***

app.UseEventBus();

Step 2 : Publish Event

  • Inject IEventPublisher in constructor like .ctor(IEventPublisher eventPublisher)
  • Begin a transaction
    • using EntityFramework
using(var transaction = dbContext.Database.BeginTransaction)
{
  //TODO:Businesses codes
  
  //Publish Event
  await _eventPublisher.PrepareAsync(/*RouteKey*/, /*Content Object*/, /*MetaData Object*/);
  
  //Commit transaction
  transaction.Commit();
  
  //Confirm Published Event.The event message won't publish untill invoked **IEventPublisher.ConfirmAsync()**
  //And you can decide when the event message be confirmed all by your self.
  await _eventPublisher.ConfirmAsync();
  
  //Or you can just rollback these messages when exception was thrown.
  await _eventPublisher.RollbackAsync();
}
  • using Ado.NET
 IDbConnection dbConnection; /*Open your database connection.*/
IDbTransaction dbTransaction = dbConnection.BeginTransaction();

//TODO:Businesses codes

//Publish Event
await _eventPublisher.PrepareAsync(/*RouteKey*/, /*Content Object*/, /*MetaData Object*/,dbConnection,dbTransaction);

//Commit transaction
dbTransaction.Commit();
dbConnection.Close();

//Confirm Published Event.The event message won't publish untill invoked **IEventPublisher.ConfirmAsync()**
//And you can decide when the event message should be confirmed all by your self.
await _eventPublisher.ConfirmAsync();

//Or you can just rollback these messages when exception was thrown.
await _eventPublisher.RollbackAsync();

step 3 : Dead letter callback handler

  • Declare a callback handler class implemented IFailureHandler
  • Register callback handle in AddEventBus scope
options.UseFailureHandle(failure =>
{
 failure.RegisterFailureCallback(/*RouteKey*/, /*Type of your deadletter callback handler*/);
});

Step 4 : Consumer callback handler

  • Declare a callback handler class implemented ISubscribeCallbackHandler
  • Register callback handle in StartUp.cs
services.AddSub(options =>
{
  options.ConsumerClientCount = 1;
  options.DefaultGroup = "/*Default Group Name*/";
  // Use default group
  options.RegisterCallback(/*RouteKey*/, /*Type of your callback handler*/);
  // Use specialized group
  options.RegisterCallback(/*RouteKey*/,/*Group Name*/ /*Type of your callback handler*/);
 });

About

EventBus is a .Net Standard library to achieve eventually consistent in distributed architectures system like SOA,MicroService. It is lightweight,easy to use and efficiently.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages