没有连接 active/available 来为这个操作提供服务:HMGET /api/v1/students;导师无法连接-helper.redis.cache.windows.net:6380

No connection is active/available to service this operation: HMGET /api/v1/students; UnableToConnect on tutor-helper.redis.cache.windows.net:6380

我正在尝试连接到 Azure Redis 缓存。我正在使用 .net 6 并使用 StackExchangeRedis 来缓存响应,它在本地主机上工作但在部署中不起作用,请帮助我.... 我也在很多论坛找到了答案,但没有用

Error when deploy

我的 Redis 代码配置

application.json

"RedisCacheSetting": {
    "Enabled": true,
    "ConnectionString": "tutor-helper.redis.cache.windows.net:6380,password=*****PuCERKKnrcbff0y3hWpZ***m1BaY40=,ssl=True,abortConnect=False"
  },

缓存属性

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public class CachedAttribute : Attribute, IAsyncActionFilter
    {
        private readonly int _timeToLiveSeconds;

        public CachedAttribute(int timeToLiveSeconds)
        {
            _timeToLiveSeconds = timeToLiveSeconds;
        }

        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var cacheSetting = context.HttpContext.RequestServices.GetRequiredService<RedisCacheSetting>();

            if (!cacheSetting.Enabled)
            {
                await next();
                return;
            }
            var cacheService = context.HttpContext.RequestServices.GetRequiredService<IResponseCacheService>();
            var cacheKey = GenerateKeyFromRequest(context.HttpContext.Request);

            var cacheResponse = await cacheService.GetCacheResponseAsync(cacheKey);

            if (!string.IsNullOrEmpty(cacheResponse))
            {
                var rs = new ContentResult
                {
                    Content = cacheResponse,
                    ContentType = "application/json",
                    StatusCode = 200,
                };

                context.Result = rs;

                return;
            }

            var executedContext = await next();

            if (executedContext.Result is ObjectResult okObjectResult)
            {
                await cacheService.CacheResponseAsync(cacheKey, okObjectResult.Value, TimeSpan.FromSeconds(_timeToLiveSeconds));
            }
        }

        private static string GenerateKeyFromRequest(HttpRequest request)
        {
            var keyBuilder = new StringBuilder();
            keyBuilder.Append($"{request.Path}");

            foreach (var (key, value) in request.Query.OrderBy(x => x.Key))
            {
                keyBuilder.Append($"|{key}-{value}");
            }

            return keyBuilder.ToString();
        }
    }

配置在startup.cs

 public static void SetUpCache(this IServiceCollection services, IConfiguration configuration)
        {
            var redisCache = new RedisCacheSetting();
            configuration.GetSection(nameof(RedisCacheSetting)).Bind(redisCache);
            services.AddSingleton(redisCache);

            //services.Configure<RedisCacheSetting>(configuration.GetSection("RedisCacheSetting"));

            //services.AddDistributedRedisCache(op => op.Configuration = redisCache.ConnectionString);
            services.AddStackExchangeRedisCache(op => op.Configuration = redisCache.ConnectionString);
            services.AddSingleton<IResponseCacheService, ResponseCacheService>();
        }

这是到端点的简单 tcping 的输出:

tcping tutor-helper.redis.cache.windows.net 6380
Probing 52.163.188.36:6380/tcp - No response - time=2021.946ms
Probing 52.163.188.36:6380/tcp - No response - time=2013.493ms
Probing 52.163.188.36:6380/tcp - No response - time=2014.854ms
Probing 52.163.188.36:6380/tcp - No response - time=2006.678ms

因此,DNS 名称解析为真实 IP 地址,但端点不接受连接。

如果我猜的话,我会说您在阻止连接的 redis 实例上设置了防火墙规则?