django框架使用之redis缓存cache问题解决

相关版本:

  • ubuntu 18.04
  • django 1.11.5
  • python 2.7
  • python包:redis 2.10.6, django-redis 4.10.0

背景描述:

本人负责公司的django服务,最近公司为适配不同架构服务器,转arm64机器调试。
django作为web服务的backend server,处理FE机制是通常的先认证,再请求其他业务接口,认证的相关session会保存一份在redis缓存中,如果有业务请求,先从缓存cache中查找session信息,如果有则可以进一步访问并正常响应,反之则报认证error。

问题描述:

在本地测试django服务时,一切正常。转arm机器测试,docker容器中测试,redis连接使用arm机器上的其他redis容器服务,checkin操作正常,但进一步请求即报认证error。反复多次,一直报这个问题。

分析:

1.相关python扩展包一致,应该不是包的问题
2.由于业务接口需要先认证,进一步请求时从cache中获取session,如果成功,服务应该没有问题,所以认为应该是redis缓存cache有问题。

测试cache

进入django项目文件内,进入django shell,使用命令:

python manage.py shell

本地测试结果如下,没有问题:

Python 2.7.17 (default, Sep 30 2020, 13:38:04) 
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.cache import cache
>>> cache.set("django_test", "redis")
True
>>> print cache.get("django_test")
redis
>>>

arm机器上测试如下,确实有问题,set后没有返回True,get时没有返回value("redis")。

>>> from django.core.cache import cache
>>> cache.set("django_test", "redis")
>>> cache.get("django_test")
>>> 

从cache的测试结果来看,应该是arm机器上cache有问题,接下来进一步分析。

>>> client = redis.Redis(host="172.22.113.249",port=6379)
>>> client.set("name","test")
----------------报错------------
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/redis/client.py", line 1171, in set
    return self.execute_command('SET', *pieces)
  File "/usr/local/lib/python2.7/dist-packages/redis/client.py", line 668, in execute_command
    return self.parse_response(connection, command_name, **options)
  File "/usr/local/lib/python2.7/dist-packages/redis/client.py", line 680, in parse_response
    response = connection.read_response()
  File "/usr/local/lib/python2.7/dist-packages/redis/connection.py", line 629, in read_response
    raise response
ResponseError: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

从报错信息来看,

  • solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no'

于是,键入:redis-cli,并且修改config:

127.0.0.1:6379> config get protected-mode
1) "protected-mode"
2) "yes"
127.0.0.1:6379> config set  protected-mode no
OK
127.0.0.1:6379> config get protected-mode
1) "protected-mode"
2) "no"

当然,也可以修改/etc/redis/redis.conf,在network中 更改protected-modeprotected-mode no,此法要重启redis-server。

再次测试,可以连通使用:

启动django服务。测试接口,发现接口正常通信。

原文地址:https://www.cnblogs.com/davis12/p/14441881.html