【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')

Azure,Django,Python,Connection · 浏览次数 : 184

小编点评

**问题描述:** 使用 Python 连接 Azure Redis 服务时遇到以下错误: ``` [ERROR][testdjangeredis.py:109]Error while reading from xxxxxxxxx.redis.cache.chinacloudapi.cn:6380 : (104, 'Connection reset by peer') ``` **问题解决:** 1. **修改 `CACHES` 配置:** ```python CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "rediss://xxxxxxxxx.redis.cache.chinacloudapi.cn:6380/1", "OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"}, } } ``` 2. **在机器人 ChatGPT 中寻求 Django_redis 配置答案问题:** 在机器人 ChatGPT 中寻求 Django_redis 配置答案问题一,您可以参考以下链接: * 在 Django_redis 中文文档中搜索 "django_redis 配置问题" **附录:** * 在使用 SSL 连接时,`LOCATION` 的格式应为 `rediss://[:password]@localhost:6380/0`。 * 确保您已经安装了 Azure Redis 服务并配置了 SSL 证书。 * 确保您的代码正确加载了 `django_redis` 模块。

正文

问题描述

使用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 格式举例

  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 域套接字连接

指定数据库数字的方法:

  • 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

 

与【Azure Cache for Redis】Python Django-Redis连接Azure Redis服务遇上(104, 'Connection reset by peer')相似的内容:

【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", "

【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题

问题描述 Azure Cache for Redis 服务在传输和存储数据时是如何加密呢? 问题回答 一:关于Azure cache for Redis服务在数据传输过程中是如何加密的? 为了确保在Azure cache for Redis和客户端应用程序之间传输的数据安全,需要启用TLS加密。Az

【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)

在前一篇文章中,我们是把.NET 8应用读取SSL证书(X509)示例部署在App Service Windows环境中,那么如果部署在Linux环境,以及Linux Container中呢? 根据前文中的第一种方法,直接在把证书文件包含在源文件中,通过相对路径读取证书文件的方式,经测试,可以正常工

【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)

在使用App Service服务部署业务应用,因为有些第三方的接口需要调用者携带TLS/SSL证书(X509 Certificate),在官方文档中介绍了两种方式在代码中使用证书: 1) 直接使用证书文件路径加载证书 new X509Certificate2 2) 从系统的证书库中通过指纹加载...

Azure Service Principals ----- Azure 上最好保守的秘密的服务

一,引言 Azure Service Principals 是 Azure Active Directory (AAD) 中的一种标识,代表应用程序,服务,自动化流程。Service Principals 支持各种 Azure 服务和资源之家的安全通信,为应用程序提供了一种进行身份验证并于 Azur

【Azure Developer】如何通过Azure Portal快速获取到对应操作的API并转换为Python代码

问题描述 对于Azure资源进行配置操作,门户上可以正常操作。但是想通过Python代码实现,这样可以批量处理。那么在没有SDK的情况下,是否有快速办法呢? 问题解答 当然可以,Azure Portal上操作的所有资源都是通过REST API来实现的,所以只要找到正确的API,就可以通过浏览器中抓取

【Azure APIM】列举几种在APIM 策略中的主动生产的错误语句

问题描述 在为APIM服务配置了诊断日志(Diagnostic Setting),把日志收集在Log A Workspace中,需要验证日志中是否能查看到请求的错误信息。 所以想人为的来制造一些错误。经过网络搜索,参考Policy的文档介绍后,完成了以下3种错误 第一种:使用 return-resp

【Azure Developer】.Net 简单示例 "文字动图显示" Typing to SVG

问题描述 看见一个有趣的页面,可以把输入的文字信息,直接输出SVG图片,还可以实现动图模式。 示例URL: https://readme-typing-svg.demolab.com/?font=Fira+Code&pause=1000&color=F7F7F7&background=233911F

Azure DevOps Server 入门实践与安装部署

一,引言 最近一段时间,公司希望在自己的服务器上安装本地版的 Azure DevOps Service(Azure DevOps Server),用于项目内的测试,学习。本着学习的目的,我也就开始学习在测试服务器上安装 Azure DevOps Server 2022 Express 以及测试的 D

Azure DevOps Server 设置项目管理用户,用户组

一,引言 Azure DevOps Server 搭建完成后,关于如何进行项目管理,项目成员管理等,我们接着上一篇文章,继续讲解 Azure DevOps Server 的用户,用户组。首先,我们需要明白 Azure DevOps Server 有哪些登录方式 1)Azure DevOps Serv