Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.
欧俊 edited this page Nov 10, 2018 · 18 revisions

RabbitMQ.EventBus.AspNetCore

NuGet version

使用方法

在asp.net core中

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    services.AddRabbitMQEventBus("amqp://guest:guest@192.168.0.252:5672/", eventBusOptionAction: eventBusOption =>
    {
        eventBusOption.ClientProvidedAssembly("RabbitMQEventBusTest");
        eventBusOption.EnableRetryOnFailure(true, 5000, TimeSpan.FromSeconds(30));
    }); 
    services.AddButterfly(butterfly =>
    {

        butterfly.CollectorUrl = "http://192.168.0.252:6401";
        butterfly.Service = "RabbitMQEventBusTest";
    });
    return services.BuildServiceProvider();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceTracer tracer)
{
    //自动订阅
    app.RabbitMQEventBusAutoSubscribe();
    app.RabbitMQEventBusModule(moduleOptions =>
    {
        moduleOptions.AddButterfly(tracer);
    });
}

订阅处理方法

[EventBus(Exchange = "dev.ex.temp.topic", RoutingKey = "send.message")]
public class Person : IEvent
{
    public string Name { set; get; }
}
public class PersonHandler : IEventHandler<Person >
{
    public Task Handle(Person message)
    {
        Console.WriteLine($"收到消息:{message.Name}");
        return Task.CompletedTask;
    }
}

发送消息

[Route("api/v1/[controller]")]
public class IndexController : Controller
{
   private readonly IRabbitMQEventBus _eventbus;
   public IndexController(IRabbitMQEventBus eventbus)
   {
      _eventbus= eventbus?? throw new ArgumentNullException(nameof(eventbus));
   }
   [HttpPost]
   public async Task<IActionResult> Send()
   {
      _eventbus.Publish( new { Name = "Hello RabbitMQ" },  "dev.ex", "send.message");
   }
}
Clone this wiki locally