我正在使用RabbitMQ总线和微服务。因为我有2个微服务。我从1服务发布,并尝试在2服务中获取该已发布消息,但我没有得到。这是我的代码。 Startup.cs-发布者,因为我使用的是.net core,而不是放置整个文件,而是放置总线配置
var builder = new ContainerBuilder();
builder.Register(c =>
{
return Bus.Factory.CreateUsingRabbitMq(rmq =>
{
var host = rmq.Host(new Uri("rabbitmq://localhost"), "/", h =>
{
h.Username("guest");
h.Password("guest");
});
rmq.ExchangeType = ExchangeType.Fanout;
});
}).
As<IBusControl>()
.As<IBus>()
.As<IPublishEndpoint>()
.SingleInstance();
builder.Populate(services);
ApplicationContainer = builder.Build();
var busControl = ApplicationContainer.Resolve<IBusControl>();
busControl.Start();
return new AutofacServiceProvider(ApplicationContainer);
控制器发布
_bus.Publish<OrderCompletedEvent>(new { BuyerId = Order.CustomerId.ToString() }).Wait();
消费者启动
var builder = new ContainerBuilder();
// register a specific consumer
builder.RegisterType<OrderCompletedEventConsumer>();
builder.Register(context =>
{
var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
var host = cfg.Host(new Uri("rabbitmq://localhost/"), "/", h =>
{
h.Username("guest");
h.Password("guest");
});
cfg.ReceiveEndpoint(host, "rabbitmqpocCart" + Guid.NewGuid().ToString(), e =>
{
e.LoadFrom(context);
e.Consumer(() => new OrderCompletedEventConsumer());
});
});
return busControl;
})
.SingleInstance()
.As<IBusControl>()
.As<IBus>();
builder.Populate(services);
ApplicationContainer = builder.Build();
var busControl1 = ApplicationContainer.Resolve<IBusControl>();
busControl1.Start();
return new AutofacServiceProvider(ApplicationContainer);
消费者服务
public class OrderCompletedEventConsumer : IConsumer<OrderCompletedEvent>
{
public OrderCompletedEventConsumer()
{
}
public Task Consume(ConsumeContext<OrderCompletedEvent> context)
{
Debug.WriteLine(context.Message.BuyerId);
await Test();
}
public async Task<bool> Test()
{
return true;
}
}
我已将调试放入“使用方法”中,但没有成功。我错过了什么吗?请帮我。 我还注意到,这两种服务都在应用程序启动时开始运行。正确吗?
请给我一些很好的示例链接或文档链接。
谢谢