redis集群:
```python
# 创建集群, 启动6个redis实例, 集群至少要3个节点, 每个节点必须是主从结构
# 配置如下: 复制6份, 修改端口号
port 7000 # 端口
bind 10.211.55.15 # 这里换成自己的ip
daemonize yes # 后台运行, 不占用终端
pidfile 7000.pid # pid保存文件名
cluster-enabled yes # 集群模式
cluster-config-file 7000_node.conf # 集群配置保存文件名
cluster-node-timeout 15000 # 节点超时时间(毫秒)
appendonly yes # 所有的写操作都追加到日志中
```
```python
# 启动6个redis节点
redis-server 7000.conf
redis-server 7001.conf
redis-server 7002.conf
redis-server 7003.conf
redis-server 7004.conf
redis-server 7005.conf
```
```python
# 复制 集群管理工具 到系统路径, 方便调用工具
sudo cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/local/bin/
# 使用集群配置工具, 把6个节点配置成集群模式, --replicas 1 表示每个主节点有一个从节点
redis-trib.rb create --replicas 1 10.211.55.15:7000 10.211.55.15:7001 10.211.55.15:7002 10.211.55.15:7003 10.211.55.15:7004 10.211.55.15:7005
# 使用命令行工具测试集群
redis-cli -c -h 10.211.55.15 -p 7000 # -c 表示集群模式
```
```python
# 安装依赖包
django-cluster-redis== 1.0.5
django-redis== 4.10.0
redis== 3.0.1 # 注意这个版本
redis-py-cluster== 2.0.0
```
```python
# 配置cache
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': [
"redis://10.211.55.15:7000",
"redis://10.211.55.15:7001",
"redis://10.211.55.15:7002",
],
'OPTIONS': {
'REDIS_CLIENT_CLASS': 'rediscluster.RedisCluster',
'CONNECTION_POOL_CLASS': 'rediscluster.connection.ClusterConnectionPool',
"CONNECTION_POOL_KWARGS": {"decode_responses": True}
}
}
}
# 测试django-redis 集群模式, 集群模式不支持切换库编号
from django_redis import get_redis_connection
redis_client = get_redis_connection()
redis_client.set('name', '祖安人')
redis_client.get('name')
```