This repository has been archived by the owner on Aug 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Home
欧俊 edited this page Nov 10, 2018
·
18 revisions
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");
}
}