我目前正在嘗試使用 MassTransit 6.3.2 更新最初是 .NET Core 3.1 的應用程式。現在配置為使用 .NET 6.0 和 MassTransit 7.3.0
我們的應用程式使用 MassTransit 通過 Azure 服務總線發送訊息,將訊息發布到主題,然后讓其他訂閱者收聽這些主題。
削減,它是這樣實作的:
// Program.cs
services.AddMassTransit(config =>
{
config.AddConsumer<AppointmentBookedMessageConsumer>();
config.AddBus(BusControlFactory.ConfigureAzureServiceBus);
});
// BusControlFactory.cs
public static class BusControlFactory
{
public static IBusControl ConfigureAzureServiceBus(IRegistrationContext<IServiceProvider> context)
{
var config = context.Container.GetService<AppConfiguration>();
var azureServiceBus = Bus.Factory.CreateUsingAzureServiceBus(busFactoryConfig =>
{
busFactoryConfig.Host("Endpoint=sb://REDACTED-queues.servicebus.windows.net/;SharedAccessKeyName=MyMessageQueuing;SharedAccessKey=MyKeyGoesHere");
busFactoryConfig.Message<AppointmentBookedMessage>(m => m.SetEntityName("appointment-booked"));
busFactoryConfig.SubscriptionEndpoint<AppointmentBookedMessage>(
"my-subscriber-name",
configurator =>
{
configurator.UseMessageRetry(r => r.Interval(5, TimeSpan.FromSeconds(60)));
configurator.Consumer<AppointmentBookedMessageConsumer>(context.Container);
});
return azureServiceBus;
}
}
}
它現在已更改并升級到最新的 MassTransit 并實施如下:
// Program.cs
services.AddMassTransit(config =>
{
config.AddConsumer<AppointmentBookedMessageConsumer, AppointmentBookedMessageConsumerDefinition>();
config.UsingAzureServiceBus((context, cfg) =>
{
cfg.Host("Endpoint=sb://REDACTED-queues.servicebus.windows.net/;SharedAccessKeyName=MyMessageQueuing;SharedAccessKey=MyKeyGoesHere");
cfg.Message<AppointmentBookedMessage>(m => m.SetEntityName("appointment-booked"));
cfg.ConfigureEndpoints(context);
});
// AppointmentBookedMessageConsumerDefinition.cs
public class AppointmentBookedMessageConsumerDefinition: ConsumerDefinition<AppointmentBookedMessageConsumer>
{
public AppointmentBookedMessageConsumerDefinition()
{
EndpointName = "testharness.subscriber";
}
protected override void ConfigureConsumer(IReceiveEndpointConfigurator endpointConfigurator, IConsumerConfigurator<AppointmentBookedMessageConsumer> consumerConfigurator)
{
endpointConfigurator.UseMessageRetry(r => r.Interval(5, TimeSpan.FromSeconds(60)));
}
}
該問題是否可以被認為是一個,是我不能系結到已存在的預訂。
在上面的示例中,您可以看到EndpointName設定為“testharness.subscriber”。在我升級之前,已經訂閱了“預約”主題。但是,當應用程式運行時,它不會出錯,但不會收到任何訊息。
如果我將其更改EndpointName為“testharness.subscriber2”。另一個訂閱者出現在 Azure 服務總線主題中(通過 Azure 門戶),我開始接收訊息。我看不出名字有什么不同(除了我放置的更改,在這種情況下:“2”后綴)。
我在這里錯過了什么嗎?我還需要做些什么才能使這些系結?我的配置有問題嗎?錯了嗎?雖然我確信我可以通過更密切地管理發布并在他們使用新佇列時洗掉不需要的佇列來解決這個問題 - 這感覺像是錯誤的方法。
uj5u.com熱心網友回復:
使用 Azure 服務總線,ForwardTo訂閱可能有點不透明。
雖然訂閱可能確實在視覺上表明它正在轉發到正確命名的佇列,但可能是在某個時刻洗掉并重新創建了佇列,而沒有洗掉訂閱。這會導致訂閱將建立訊息,因為它無法將它們轉發到不再存在的佇列。
為什么?在內部,訂閱將 維護ForwardTo為一個物件 id,在佇列被洗掉后它指向一個不存在的物件——導致訊息在訂閱中建立。
如果訂閱中有訊息,則可能需要進入門戶并更新該訂閱以指向新佇列(即使它具有相同的名稱),此時訊息應該流向佇列。
如果訂閱中沒有任何訊息(或者它們不重要),您可以洗掉訂閱,當您重新啟動總線時,MassTransit 將重新創建訂閱。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/402597.html
標籤:
