Tair (Redis OSS-compatible)採用自研的高可用服務HA組件,無需依賴Sentinel(哨兵)。但為了提高執行個體的相容性,減少使用者的代碼改動,Tair (Redis OSS-compatible)提供Sentinel相容模式。開啟該功能後,您可以像串連開源Redis Sentinel一樣串連Tair(以及Redis開源版)執行個體。
Redis Sentinel簡介
Redis Sentinel為開源Redis提供主備執行個體監控、故障警示、自動故障切換等服務,很多使用本地自建Redis資料庫並且對可靠性要求較高的業務情境都用到了Sentinel。為了給這類情境中的Redis資料庫遷移上雲提供方便,阿里雲開發了Sentinel相容模式。開啟Sentinel相容模式後,您可以使用如下的Sentinel相關命令(下述命令中的Master名稱請固定為redis_master):
命令 | 說明 |
SENTINEL sentinels | 查詢指定Master的Sentinel執行個體列表以及這些Sentinel執行個體的狀態。使用方式: SENTINEL sentinels redis_master
|
SENTINEL get-master-addr-by-name | 查詢指定Master的IP地址和連接埠號碼。使用方式: SENTINEL get-master-addr-by-name redis_master
|
關於Sentinel相關命令在各版本中的支援度,請參見Redis開源版命令支援。
串連樣本
開啟Sentinel相容模式後,有兩種方式串連執行個體:若執行個體開啟專用網路免密訪問,您可以通過Sentinel模式免密串連執行個體;若未開啟免密訪問,您需要在串連時配置驗證資訊。
Sentinel免密串連
阿里雲Redis Sentinel相容模式串連代碼配置樣本如下。
Spring Data Redis
本樣本的Spring Data Redis版本為2.4.2。
@Bean
public JedisConnectionFactory connectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master("redis_master")
.sentinel("r-bp10noxlhcoim2****.redis.rds.aliyuncs.com", 6379);
JedisPoolConfig poolConfig = new JedisPoolConfig();
...
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig);
return connectionFactory;
}
參數說明:
master:SENTINEL名稱,請固定為redis_master。
sentinel:執行個體的專用網路串連地址與連接埠號碼,用英文逗號(,)分隔,例如"r-bp10noxlhcoim2****.redis.rds.aliyuncs.com", 6379。
redis-py
本樣本的Python版本為3.9、redis-py版本為4.3.6。
from redis.sentinel import Sentinel
SENTINEL_HOST = "r-bp10noxlhcoim2****.redis.rds.aliyuncs.com"
SENTINEL_PORT = 6379
SENTINEL_MASTER_NAME = "redis_master"
sentinel = Sentinel([(SENTINEL_HOST, SENTINEL_PORT)])
r = sentinel.master_for(SENTINEL_MASTER_NAME, db=0)
r.set('foo', 'bar')
print(r.get('foo'))
參數說明:
Sentinel密碼串連
阿里雲Redis Sentinel相容模式串連代碼配置樣本如下
Java
本樣本以Java用戶端的最低版本為例,用戶端版本要求如下:
Jedis
String masterName = "redis_master";
Set<String> sentinels = new HashSet<>();
sentinels.add("r-bp10noxlhcoim2****.redis.rds.aliyuncs.com:6379");
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
String dbPassword = "testaccount:Rp829dlwa";
String sentinelPassword = "testaccount:Rp829dlwa";
JedisSentinelPool jedisSentinelPool =
new JedisSentinelPool(masterName, sentinels, poolConfig,
2000, 2000, dbPassword,
0, null, 2000, 2000,
sentinelPassword, null);
Lettuce
String masterName = "redis_master";
String sentinelNodes = "r-bp10noxlhcoim2****.redis.rds.aliyuncs.com:6379";
String sentinelPassword = "testaccount:Rp829dlwa";
RedisURI redisURI = RedisURI.create("redis-sentinel://" + sentinelPassword + "@" + sentinelNodes + "#" + masterName);
redisURI.getSentinels().forEach(it -> it.setPassword(sentinelPassword));
RedisClient client = RedisClient.create(redisURI);
RedisCommands<String, String> sync = client.connect().sync();
System.out.println(sync.set("key", "value"));
System.out.println(sync.get("key"));
client.close();
參數說明:
masterName:SENTINEL名稱,請固定為redis_master。
sentinels.add/sentinelNodes:設定為執行個體的專用網路串連地址和連接埠號碼,格式為r-bp10noxlhcoim2****.redis.rds.aliyuncs.com:6379。
dbPassword與sentinelPassword:設定為執行個體帳號的密碼。根據選取帳號的不同,密碼的填寫格式有一定區別。如果忘記密碼,您可以重設密碼。具體操作,請參見修改或重設密碼。
redis-py
本樣本的Python版本為3.9、redis-py版本為4.3.6。
from redis.sentinel import Sentinel
SENTINEL_HOST = "r-bp10noxlhcoim2****.rds.aliyuncs.com"
SENTINEL_PORT = 6379
SENTINEL_MASTER_NAME = "redis_master" # 注意:此名稱不可更改
SENTINEL_REDIS_PWD = "testaccount:Rp829dlwa"
conf = {
'password': SENTINEL_REDIS_PWD,
}
sentinel = Sentinel([(SENTINEL_HOST, SENTINEL_PORT)], sentinel_kwargs=conf)
r = sentinel.master_for(SENTINEL_MASTER_NAME, db=0, **conf)
r.set('foo', 'bar')
print(r.get('foo'))
參數說明:
SENTINEL_HOST與SENTINEL_PORT:執行個體的專用網路串連地址與連接埠號碼。
SENTINEL_MASTER_NAME:SENTINEL名稱,請固定為redis_master。
SENTINEL_REDIS_PWD:執行個體帳號的密碼。
常見問題
Q:原來使用自建Redis Sentinel模式,切換至Redis Sentinel相容模式後,遇到NOAUTH Authentication required錯誤該怎麼處理?
A:您可以啟用#no_loose_sentinel-password-free-access參數(設定為yes),即可在不開啟VPC免密功能的情況下,在VPC串連地址中實現Sentinel免密串連。
若執行個體為Redis 6.0以下,請升級您的用戶端,並修改部分代碼用以添加Sentinel認證密碼,再進行重試。更多資訊,請參見本文中的Sentinel密碼串連。
Q:為什麼報錯Unknown sentinel subcommand 'MASTERS'?
A:舊的小版本不相容部分命令,請升級小版本後重試。