【发布时间】:2019-02-14 08:07:43
【问题描述】:
我们在 .NET Core 2.0 Azure 应用服务中偶然收到以下错误日志:
StackExchange.Redis.RedisConnectionException: No connection is available to service this operation: GET post:307948:media;
It was not possible to connect to the redis server(s).
To create a disconnected multiplexer, disable AbortOnConnectFail.
ConnectTimeout; IOCP: (Busy=0,Free=1000,Min=4,Max=1000),
WORKER: (Busy=20,Free=662,Min=4,Max=682),
Local-CPU: n/a ---> System.AggregateException: One or more errors occurred.
(It was not possible to connect to the redis server(s).
To create a disconnected multiplexer, disable AbortOnConnectFail. ConnectTimeout)
(UnableToConnect on api-prod.redis.cache.windows.net:6380/Interactive,
Initializing, last: NONE,
origin: BeginConnectAsync, outstanding: 0, last-read: 12s ago, last-write: 12s ago,
unanswered-write: 290293s ago, keep-alive: 60s,
state: Connecting, mgr: 10 of 10 available,
last-heartbeat: never, global: 0s ago, v: 2.0.513.63329)
我找到的所有答案和博客文章,告诉我们将属性 AbortOnConnectFail 设置为 false。我们已经将此属性设置为 false,但仍然会发生这些错误。这是我们的实现:
public class CacheService : ICacheService
{
private static IRedisConfiguration _redisConfiguration;
private readonly IDateService _dateService;
private static readonly Lazy<ConnectionMultiplexer> LazyConnection =
new Lazy<ConnectionMultiplexer>(() =>
{
var configurationOptions = CreateConfigurationOptions();
return ConnectionMultiplexer.Connect(configurationOptions);
});
public CacheService(IRedisConfiguration redisConfiguration, IDateService dateService)
{
_redisConfiguration = redisConfiguration;
_dateService = dateService;
}
private static ConfigurationOptions CreateConfigurationOptions()
{
var deltaBackOffInMilliseconds = TimeSpan.FromSeconds(5).Milliseconds;
var maxDeltaBackOffInMilliseconds = TimeSpan.FromSeconds(20).Milliseconds;
var config = new ConfigurationOptions();
//https://docs.microsoft.com/en-us/azure/architecture/best-practices/retry-service-specific#azure-redis-cache-retry-guidelines
config.AbortOnConnectFail = false;
config.ConnectRetry = 3;
config.ConnectTimeout = 2000;
config.ReconnectRetryPolicy = new ExponentialRetry(deltaBackOffInMilliseconds, maxDeltaBackOffInMilliseconds);
config.EndPoints.Add(_redisConfiguration.HostName);
config.Password = _redisConfiguration.Password;
config.Ssl = _redisConfiguration.Ssl;
config.DefaultDatabase = _redisConfiguration.DatabaseId;
return config;
}
public static ConnectionMultiplexer Connection => LazyConnection.Value;
public static IDatabase Cache => Connection.GetDatabase();
}
在 Azure 应用服务上,我检查了 TCP 连接,没有显示任何警告。 传出连接数有所增加,但低于 1000。
在运行两个 API 实例的情况下,我们有大约 80 个连接,几乎保持不变。一天没有增加,所以它接缝连接管理似乎是正确的。
关于如何解决这个问题的任何提示?也许是因为更多的配置调整。
Azure Redis:P1 高级层 StackExchange.Redis 版本:2.0.513 在 .NET Core 2.0 上运行的 .NET Core API
非常感谢。
【问题讨论】:
-
我遇到了同样的问题。你能找到解决方案吗?
-
@RobertVuković 没有。还没有。
标签: azure redis asp.net-core-2.0 stackexchange.redis azure-redis-cache