【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')
问题描述
使用Python连接Azure Redis服务,因为在代码中使用的是Django-redis组件,所以通过如下的配置连接到Azure Redis服务:
CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://xxxxxxxxx.redis.cache.chinacloudapi.cn:6380/1", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } } }
但是当部署到AKS中后,发现一直报错 [ERROR][testdjangeredis.py:109]Error while reading from xxxxxxxxx.redis.cache.chinacloudapi.cn:6380 : (104, 'Connection reset by peer')
问题解答
查看Django-redis的官方文档,对 cache backend 中Location的介绍为:
URL 格式举例
- redis://[:password]@localhost:6379/0
- rediss://[:password]@localhost:6380/0
- unix://[:password]@/path/to/socket.sock?db=0
支持三种 URL scheme :
- redis://: 普通的 TCP 套接字连接
- rediss://: SSL 包裹的 TCP 套接字连接
- unix://: Unix 域套接字连接
指定数据库数字的方法:
- db 查询参数, 例如: redis://localhost?db=0
- 如果使用 redis:// scheme, 可以直接将数字写在路径中, 例如: redis://localhost/0
在仔细对比配置,发现连接Azure Redis的时候使用SSL 6380端口,而Django-Redis的配置中 scheme 还继续使用的 redis://,而不是rediss://,所以导致 Connection reset。
为了解决以上问题,直接修改Location设置为:rediss://xxxxxxxxx.redis.cache.chinacloudapi.cn:6380/1 即可!
CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "rediss://xxxxxxxxx.redis.cache.chinacloudapi.cn:6380/1", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", } } }
附录一:在机器人ChatGPT中寻求 django_redis 配置答案
问题一:如何配置django_redis:
问题二:如何设置Django_redis的超时时间
问题三:如何设置django_redis的keep_alive
问题四:如何启用django_redis SSL
问题五:启用django-redis的SSL并通过6380端口连接示例
参考资料
django-redis 中文文档:https://django-redis-chs.readthedocs.io/zh_CN/latest/index.html
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2022-02-03 【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known