feat: enhance Redis Sentinel configuration to support dynamic sentinel settings
This commit is contained in:
parent
e5d26301c7
commit
a6f0081c94
@ -78,12 +78,26 @@ class Config(dict):
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
if self.get('REDIS_SENTINEL_SENTINELS') is not None:
|
if self.get('REDIS_SENTINEL_SENTINELS') is not None:
|
||||||
del redis_config['default']['LOCATION']
|
sentinels_str = self.get('REDIS_SENTINEL_SENTINELS')
|
||||||
redis_config['default']['OPTIONS']['SENTINEL_SENTINELS'] = self.get('REDIS_SENTINEL_SENTINELS').split(',')
|
sentinels = [
|
||||||
redis_config['default']['OPTIONS']['SENTINEL_MASTER_SET'] = self.get('REDIS_SENTINEL_MASTER_SET')
|
(host.strip(), int(port))
|
||||||
return redis_config
|
for hostport in sentinels_str.split(',')
|
||||||
else:
|
for host, port in [hostport.strip().split(':')]
|
||||||
return redis_config
|
]
|
||||||
|
|
||||||
|
redis_config['default']['LOCATION'] = f'redis://{self.get("REDIS_SENTINEL_MASTER")}/{self.get("REDIS_DB")}'
|
||||||
|
redis_config['default']['OPTIONS'].update({
|
||||||
|
'CLIENT_CLASS': 'django_redis.client.SentinelClient',
|
||||||
|
'SENTINELS': sentinels,
|
||||||
|
'SENTINEL_MASTER': self.get('REDIS_SENTINEL_MASTER'),
|
||||||
|
'PASSWORD': self.get("REDIS_PASSWORD"),
|
||||||
|
'SOCKET_TIMEOUT': 1,
|
||||||
|
})
|
||||||
|
|
||||||
|
# 必须移除和 Sentinel 不兼容的项
|
||||||
|
redis_config['default']['OPTIONS'].pop('CONNECTION_POOL_KWARGS', None)
|
||||||
|
|
||||||
|
return redis_config
|
||||||
|
|
||||||
def get_language_code(self):
|
def get_language_code(self):
|
||||||
return self.get('LANGUAGE_CODE', 'zh-CN')
|
return self.get('LANGUAGE_CODE', 'zh-CN')
|
||||||
|
|||||||
@ -172,3 +172,6 @@ STATIC_URL = 'static/'
|
|||||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||||
|
|
||||||
edition = 'CE'
|
edition = 'CE'
|
||||||
|
|
||||||
|
if os.environ.get('MAXKB_REDIS_SENTINEL_SENTINELS') is not None:
|
||||||
|
DJANGO_REDIS_CONNECTION_FACTORY = "django_redis.pool.SentinelConnectionFactory"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user