我正在創建一個具有通用事件型別和 2 個不同消費者的主題交換,我想知道是否有更好的方法使用泛型來做到這一點?
我好嗎
一般事件:
public interface IOrchestratorTopicType<T> where T : class
{
public Guid? TraceId { get; set; }
public string Event { get; set; }
public string Source { get; set; }
public string Destination { get; set; }
public T Data { get; set; }
}
配置:
services.AddMassTransit(bus =>
{
bus.SetKebabCaseEndpointNameFormatter();
bus.UsingRabbitMq((ctx, busConfigurator) =>
{
busConfigurator.Host(configuration.GetConnectionString("RabbitMq"));
busConfigurator.Publish<IOrchestratorTopicType<object>>(a => a.ExchangeType = ExchangeType.Topic);
busConfigurator.ReceiveEndpoint("create-order", e =>
{
e.ConfigureConsumeTopology = false;
e.Consumer<CreateOrderConsumer>();
e.Bind<IOrchestratorTopicType<object>>(s =>
{
s.RoutingKey = "create.order";
s.ExchangeType = ExchangeType.Topic;
});
});
busConfigurator.ReceiveEndpoint("valid-order", e =>
{
e.ConfigureConsumeTopology = false;
e.Consumer<ValidOrderConsumer>();
e.Bind<IOrchestratorTopicType<object>>(s =>
{
s.RoutingKey = "valid.order";
s.ExchangeType = ExchangeType.Topic;
});
});
});
});
services.AddMassTransitHostedService();
制作人:
await _publisher.Publish<IOrchestratorTopicType<object>>(new
{
TraceId = new Guid(),
Event = "create.order",
Source = "SourceTestSource",
Destination = "DestinationTest",
Data = createOrderModel
}, e => e.TrySetRoutingKey("create.order"));
消費者:
public class ValidOrderConsumer : IConsumer<IOrchestratorTopicType<object>>
{
public Task Consume(ConsumeContext<IOrchestratorTopicType<object>> context)
{
throw new NotImplementedException();
}
}
public class CreateOrderConsumer : IConsumer<IOrchestratorTopicType<object>>
{
public Task Consume(ConsumeContext<IOrchestratorTopicType<object>> context)
{
throw new NotImplementedException();
}
}
我希望生產者發送物件的確切型別,而消費者使用物件的確切型別而不是通用物件
uj5u.com熱心網友回復:
我建議觀看有關 MassTransit 如何使用 RabbitMQ 的視頻。MassTransit 無需使用主題交換,因為它默認使用基于型別的路由。
上面的代碼不是我推薦的,因為 MassTransit 已經默認使用 RabbitMQ 進行基于多型型別的路由。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/381821.html
