簡體   English   中英

從 Web 瀏覽器調用時,Azure 應用服務無法連接到 Azure Redis

[英]Azure App Service cannot connect to Azure Redis when calling from Web Browser

我有一個使用 Asp.net 核心並托管在 Azure 應用服務上的 REST API 服務應用程序。 我正在添加 Azure Redis 作為緩存機制。

我使用本地 redis 在本地機器上測試了該應用程序,它運行良好。 我將 Web 服務部署到 Azure 應用服務,並對其進行了測試。

當我嘗試使用 Postman 測試服務時,它工作正常,並且正在填充緩存並從緩存中讀取。

但是當我運行前端應用程序時,它是一個調用后端服務的 JavaScript 單頁應用程序。

我不是從前端查詢 Redis。 但來自后端 .net 應用程序。

對服務的調用失敗並出現此錯誤

Timeout performing EVAL, inst: 1, 
clientName: <.....>, serverEndpoint: Unspecified/my-redis.redis.cache.windows.net:6380, 
keyHashSlot: 15126 (Please take a look at this article for some common client-side issues that can cause timeouts: http://stackexchange.github.io/StackExchange.Redis/Timeouts)

正如我所說,我正在使用相同的參數從 Postman 調用同一個 EndPoint(這是我的應用程序服務),並且它正在工作。 但是從瀏覽器中,它不起作用

這是我的配置和代碼:

在 Startup.cs

public void ConfigureServices(IServiceCollection services)
{
  ...
   services.AddDistributedRedisCache(options => {
                options.Configuration = Configuration.GetConnectionString("AzureCache");
                options.InstanceName = "master";
                });
}

並在控制器(ProductController)中

using Microsoft.Extensions.Caching.Distributed;
public class ProductController: ControllerBase
{
   IDistributedCache _cashe;
   IDataRepository _repo;
   public ProductController (IDistributedCache cache, IDataRepository repo)
   {
       _cache = cache;
       _repo = repo;
   }

   [HttpGet]
   public Async Task<IActionResult> GetProductions([FormBody] DataRequest request)
   {
      string data = _cache.GetString(request.AsKey());
      if (data == null)
      {
           data = _repo.getData(request);
           _cache.SetString(request.AsKey());
      }
      return Ok(data);
   }
}

}

客戶端代碼如下:

const request = axios.post('https://mydata.myserver.azure.com', reqBody, headers);
request.then(res => {
    .... process the data
});

PS:錯誤提到了網上的一篇文章。 我讀了這篇文章,什么都沒有跳出來。 除了一個在 250k 到 300k 之間的服務之外,我所有的服務都小於 5k,而且我的所有呼叫都失敗了。

錯誤本身描述了“請查看這篇文章了解一些常見的客戶端問題”。 您能否分享您正在撥打電話的客戶端代碼?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM