azure - 如何解决从 Azure 部署的应用程序调用时的外部 API 延迟?

标签 azure http asp.net-web-api2 azure-application-insights

信息: 我有以下 2 个方法,它是 Web API(不是核心 API)的一部分,并且部署在 Azure 中

方法一:

public async Task<bool> ProcessEmployee(list<employee> EmployeeList)
                    var tasks = new List<Task<EmployeeResponseModel>>();

                    HttpClient localHttpClient = new HttpClient(); 
                    localHttpClient.Timeout = TimeSpan.FromSeconds(100);
                    

                    foreach (var employee in EmployeeList) // **having 1000 calls** 
                    { 
                     tasks.Add(GetAddressResponse(employee.URL,localHttpClient)); 
                    } 
                    var responses = await Task.WhenAll(tasks);
          }

方法2:

    private async Task<EmployeeResponseModel> GetAddressResponse(url, HttpClient client)
{
        var response = new EmployeeResponseModel();
        try
        {

            
            using (HttpResponseMessage apiResponse = await client.GetAsync(**url**))
            {

                if (apiResponse.IsSuccessStatusCode)
                {
                    var res= await apiResponse.Content.ReadAsStringAsync();
                    response = JsonConvert.DeserializeObject<EmployeeResponseModel>(res);
                   
                }
            }

            return response;
        }
        catch (Exception ex)
        {
        }
        return response;
    }

如果我从 Azure 进行监控 -> 诊断并解决问题 -> Web 应用程序缓慢,所有外部 API 调用都会显示延迟问题

enter image description here enter image description here

但是,如果我从 Postman 调用相同的外部 API,速度会非常快,并且延迟也会更短

enter image description here

  1. 方法 1 和方法 2 是一个 Web API 的一部分,并且部署在 Azure AppService 上。

  2. getAddress是外部API,已部署在其他环境中,没有太多信息

  3. 如果我们从 1) 调用外部 API,即“getAddress”,我们将面临超过 5 秒的高延迟。

  4. 如果我们从 Postman 调用外部 API,即“getAddress”,我们会在 303 毫秒内收到响应。

最佳答案

我猜这是由服务计划的位置造成的。

如果服务计划的位置距离您的位置较远,可能会导致延迟。但也不能排除其他的可能性,所以我的建议是先在localhost调试一下,排除代码的可能性。

关于azure - 如何解决从 Azure 部署的应用程序调用时的外部 API 延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67061111/

相关文章:

Azure Multi-Tenancy 应用程序注册: What happens when I add an "Admin Consent" required permission

http - 为什么 http 使用 CRLF 作为行分隔符?

http - 如何通过Postman在go lang中处理GET操作(CRUD)?

http - 实现 CORS 时未获得响应

.net - HttpClient.ReadAsAsync<T> 在使用 [Serializable] 时返回空对象

在 azure 中使用 webjobs 的 Python 脚本

azure - 连接到新的 Azure 缓存(DataCache、DataCacheFactory 和连接池)

asp.net-mvc - 在 Azure 中使用 DataAnnotations 进行验证显示错误的文本

c# - 如何通过IHttpActionResult获取内容

c# - parallel.foreach 和 httpclient - 奇怪的行为