【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')


image

问题解答

查看Django-redis的官方文档,对 cache backend 中Location的介绍为:

URL 格式举例

  1. redis://[:password]@localhost:6379/0
  2. rediss://[:password]@localhost:6380/0
  3. unix://[:password]@/path/to/socket.sock?db=0

支持三种 URL scheme :

  • redis://: 普通的 TCP 套接字连接
  • rediss://: SSL 包裹的 TCP 套接字连接
  • unix://: Unix 域套接字连接

指定数据库数字的方法:

在仔细对比配置,发现连接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:

image

问题二:如何设置Django_redis的超时时间

image

问题三:如何设置django_redis的keep_alive

image

问题四:如何启用django_redis SSL

image

问题五:启用django-redis的SSL并通过6380端口连接示例

image

参考资料

django-redis 中文文档:https://django-redis-chs.readthedocs.io/zh_CN/latest/index.html

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

分类: 【Azure Redis 缓存】

标签: Python djange-redis, rediss://xxxxx.redis.cache.chinacloudapi.cn:6380/1, redis://xxxxx.redis.cache.chinacloudapi.cn:6379/1, 104, 'Connection reset by peer', ChatGPT for djange-redis

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容