ABP框架分层架构下无法连接Redis服务器的问题求助
问题描述
使用最新版ABP Framework分层架构、EF及Blazor Server客户端时,遇到Redis连接错误:It was not possible to connect to the redis server(s)。
- 调试
AuthServerModule时,直接通过var redis = ConnectionMultiplexer.Connect可正常连接Redis服务器并获取数据库 - Redis服务器在RedisInsight中连接、测试均正常
- 错误日志如下:
2023-04-12 17:24:15.606 +03:00 [INF] Content root path: C:MyProjectsa_Samples\Denemeler14\Adalim14\src\Adalim14.AuthServer
2023-04-12 17:24:15.668 +03:00 [INF] Request starting HTTP/2 GET https://localhost:44311/ - -
2023-04-12 17:24:22.411 +03:00 [ERR] It was not possible to connect to the redis server(s). UnableToConnect on merry-ladybug-33525.upstash.io:6379/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 5s ago, last-write: 5s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 0s ago, v: 2.2.4.27433
StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s). UnableToConnect on merry-ladybug-33525.upstash.io:6379/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 5s ago, last-write: 5s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 0s ago, v: 2.2.4.27433
at StackExchange.Redis.ConnectionMultiplexer.ConnectImplAsync(ConfigurationOptions configuration, TextWriter log) in //src/StackExchange.Redis/ConnectionMultiplexer.cs:line 861
at Microsoft.Extensions.Caching.StackExchangeRedis.RedisCache.ConnectAsync(CancellationToken token)
at Microsoft.Extensions.Caching.StackExchangeRedis.RedisCache.GetAndRefreshAsync(String key, Boolean getData, CancellationToken token)
at Microsoft.Extensions.Caching.StackExchangeRedis.RedisCache.GetAsync(String key, CancellationToken token)
at Microsoft.Extensions.Caching.Distributed.DistributedCacheExtensions.GetStringAsync(IDistributedCache cache, String key, CancellationToken token)
at Volo.Abp.PermissionManagement.StaticPermissionSaver.SaveAsync()
at Volo.Abp.PermissionManagement.StaticPermissionSaver.SaveAsync()
at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous(IInvocation invocation, IInvocationProceedInfo proceedInfo)
at Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapter.ProceedAsync()
at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed)
at Volo.Abp.PermissionManagement.AbpPermissionManagementDomainModule.<>c__DisplayClass8_0.<b__1>d.MoveNext()
解决建议
检查ABP配置的Redis连接字符串完整性
你使用的是Upstash Redis,需确保连接字符串包含用户名、密码、SSL协议等关键参数。直接调用ConnectionMultiplexer.Connect可能用了完整配置,但appsettings.json中的Redis:Configuration可能缺失参数。Upstash标准连接字符串格式为:rediss://<用户名>:<密码>@merry-ladybug-33525.upstash.io:6379(带SSL)。
确认配置文件内容:"Redis": { "Configuration": "rediss://<你的用户名>:<你的密码>@merry-ladybug-33525.upstash.io:6379", "InstanceName": "Adalim14" }验证ABP的Redis缓存注入配置
检查AuthServerModule或相关模块的ConfigureServices方法,确保Redis缓存配置正确:Configure<AbpDistributedCacheOptions>(options => { options.KeyPrefix = "Adalim14:"; });同时确认未手动替换
IDistributedCache实现,导致使用错误的Redis配置。检查StackExchange.Redis版本兼容性
日志显示使用的StackExchange.Redis版本为2.2.4.27433,ABP最新版可能对该库有版本要求。尝试升级StackExchange.Redis到最新稳定版,或确认与ABP依赖的版本匹配。添加Redis连接超时与重试参数
在连接字符串中补充超时和重试配置,避免网络波动导致连接失败:rediss://<用户名>:<密码>@merry-ladybug-33525.upstash.io:6379?connectTimeout=10000&syncTimeout=10000&retry=3确认应用程序网络权限
虽然RedisInsight能连接,但应用程序所在环境可能有不同网络策略(如防火墙、代理)。用命令行测试网络连通性:# Linux/macOS telnet merry-ladybug-33525.upstash.io 6379# Windows Test-NetConnection merry-ladybug-33525.upstash.io -Port 6379
内容的提问来源于stack exchange,提问作者Serdar Güner

