我有一个Django应用程序,使用DgangoChannels,Djangochannelrestframework。它与ReactJS前端建立了一个websocket连接。作为通道层,我使用类似的Redis
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("redis", 6379)],
},
},
}
Redis和Django在docker中运行。我的redis docker设置是
redis:
image: "redis:7.0.4-alpine"
command: redis-server
ports:
- "6379:6379"
networks:
- nginx_network
当我在生产服务器上运行我的应用程序时,一切都能工作5-8个小时。但在那之后,如果Django应用程序试图通过ws-if发送消息,则会出现错误
ReadOnlyError at /admin/operations/operation/add/
READONLY You can't write against a read only replica.
Request Method: POST
Request URL: http://62.84.123.168/admin/operations/operation/add/
Django Version: 3.2.12
Exception Type: ReadOnlyError
Exception Value:
READONLY You can't write against a read only replica.
Exception Location: /usr/local/lib/python3.8/site-packages/channels_redis/core.py, line 673, in group_send
Python Executable: /usr/local/bin/python
Python Version: 3.8.13
Python Path:
['/opt/code',
'/usr/local/bin',
'/usr/local/lib/python38.zip',
'/usr/local/lib/python3.8',
'/usr/local/lib/python3.8/lib-dynload',
'/usr/local/lib/python3.8/site-packages']
Server time: Tue, 02 Aug 2022 08:23:18 +0300
我知道它在某种程度上与Redis复制有关,但不知道为什么在一段时间后失败,以及如何修复
我有同样的错误,可能的解决方案是
通过向docker添加命令并禁用副本只读配置来修复此问题,将此添加到您的redis docker撰写
command: redis-server --appendonly yes --replica-read-only no
然后您可以尝试验证复制副本只读禁用usingredis-cli > config get replica-read-only
命令,如果结果为否,则成功禁用。