gpt4 book ai didi

azure - 与 Azure 服务总线一起使用时获取 MassTransit.RequestTimeoutException

转载 作者:行者123 更新时间:2023-12-03 06:21:11 27 4
gpt4 key购买 nike

我是公共(public)交通新手,打算通过 Azure 服务总线应用持久 future 和路由滑动模式。具有 Controller 的 Web 应用程序定义如下:

app.MapPost("/orders/submit", async (SubmitOrderDto submitOrderDto, IRequestClient<SubmitOrder> client, ILogger<Program> logger, CancellationToken cancellationToken) =>
{
try
{
Response response = await client.GetResponse<OrderCompleted, OrderFaulted>(new
{
submitOrderDto.OrderId
}, cancellationToken);
return response switch
{
(_, OrderCompleted completed) => Results.Ok(new
{
completed.OrderId,
completed.Status,
completed.Created,
completed.Completed,
}),
(_, OrderFaulted faulted) => Results.BadRequest(new
{
faulted.OrderId,
faulted.Description,
faulted.Created,
faulted.Faulted
}),
_ => Results.BadRequest()
};
}
catch (Exception ex)
{
return Results.Accepted(value:new
{
submitOrderDto.OrderId,
ex.Message
});
}
})
.Produces<OrderCompleted>()
.Produces<OrderFaulted>(StatusCodes.Status400BadRequest)
.Produces(StatusCodes.Status202Accepted)
.ProducesValidationProblem(StatusCodes.Status409Conflict)
.WithName("SubmitOrder").WithTags("OrderServiceAPI");

有 future 、规划和事件类(class)。我的预期如果有提交请求后,事件类将对其进行验证,然后将错误返回给客户端。如果通过验证,则可以继续该过程。

但是我遇到了如下的公共(public)交通异常:

Timeout waiting for response, RequestId: 40040000-4446-107b-e828-08db2e658cb0at MassTransit.Clients.RequestClient1.GetResponseInternal[T1,T2](SendRequestCallback request, CancellationToken cancellationToken, RequestTimeout timeout, RequestPipeConfiguratorCallback1 callback) in /_/src/MassTransit/Clients/RequestClient.cs:line 212

如果我改用 RabbitMq,请求就会按我的预期得到验证。但是当与 Azure Service Bus 一起使用时,总是出现超时异常,如上所述在程序中,我已如下注入(inject) Masstransit 并使用 Masstransit v8 包

var builder = WebApplication.CreateBuilder(args);
builder.Services.TryAddScoped<IItineraryPlanner<SubmitOrder>, OrderItineraryPlannerAzure>();

builder.Services.AddMassTransit(x =>
{

// Azure
x.SetKebabCaseEndpointNameFormatter();
x.AddServiceBusMessageScheduler();
x.AddRequestClient<SubmitOrder>();

//-------------
x.ApplyCustomMassTransitConfiguration();
x.AddDelayedMessageScheduler();
x.AddActivitiesFromNamespaceContaining<OrderActivity>();
x.AddFuturesFromNamespaceContaining<OrderFuture>();

x.AddSagaRepository<FutureState>()
.InMemoryRepository();

//x.UsingRabbitMq((context, cfg) =>
//{
// cfg.AutoStart = true;
// cfg.ApplyCustomBusConfiguration();

// cfg.UseDelayedMessageScheduler();
// cfg.ConfigureEndpoints(context);
//});

x.UsingAzureServiceBus((context, cfg) =>
{
using var scope = context.CreateScope();
cfg.Host("Endpoint=sb://{connectionString}");

cfg.UseTimeout(timeoutConfigurator =>
{
timeoutConfigurator.Timeout = TimeSpan.FromMinutes(10);
});
cfg.PrefetchCount = 100;
cfg.LockDuration = TimeSpan.FromMinutes(5);
cfg.MaxConcurrentCalls = 100;
cfg.MaxDeliveryCount = 5;
cfg.DefaultMessageTimeToLive = TimeSpan.FromDays(7);
cfg.AutoStart = true;
cfg.RequiresSession = true;

cfg.UseServiceBusMessageScheduler();
// deploy topology configuration when service starting
cfg.DeployTopologyOnly = true;
cfg.UseDelayedMessageScheduler();
cfg.ConfigureEndpoints(context);
});

我想我需要为请求客户端创建消费者,但我不知道如何在 future 模式中定义它。或者我错过了什么步骤?

最佳答案

嗯,您确实过度指定了 Azure 服务总线的总线配置,这样做的配置会破坏一些东西。

删除:

cfg.RequiresSession = true;
cfg.DeployTopologyOnly = true;
cfg.UseDelayedMessageScheduler();

此外,删除:

x.AddDelayedMessageScheduler();

因为您已经添加了服务总线消息调度程序。

总线配置所需的一切:

x.UsingAzureServiceBus((context, cfg) =>
{
cfg.Host("Endpoint=sb://{connectionString}");

cfg.UseServiceBusMessageScheduler();

cfg.UseTimeout(timeoutConfigurator =>
{
timeoutConfigurator.Timeout = TimeSpan.FromMinutes(10);
});

cfg.PrefetchCount = 100;
cfg.ConcurrentMessageLimit = 100;
cfg.AutoStart = true;

cfg.ConfigureEndpoints(context);
});

如果您想在接收端点上配置任何属性,则需要添加回调而不是配置总线端点属性。

x.AddConfigureEndpointsCallback((context,name,cfg) =>
{
if (cfg is IServiceBusReceiveEndpointConfigurator sb)
{
sb.LockDuration = TimeSpan.FromMinutes(5);
sb.MaxDeliveryCount = 5;
sb.DefaultMessageTimeToLive = TimeSpan.FromDays(7);
}
});

关于azure - 与 Azure 服务总线一起使用时获取 MassTransit.RequestTimeoutException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75872694/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com