我们正在使用 StackExchange.Redis 客户端。我们还使用 Sentinel,它会告诉我们 Redis 主节点何时发生变化。由于我们已经创建了连接(作为Lazy<IConnectionMultiplexer>
推荐),我们想知道将连接信息更改为现在指向新的 Redis 主节点的最佳方法是什么?
我们看到几个选项:
- 当通知来自 Sentinel 时,创建一个新的
Lazy<IConnectionMultiplexer>
- 更改现有连接多路复用器上的连接信息
#2甚至可能吗?
我们目前的方式:
private Lazy<IConnectionMultiplexer> CreateRedisConnection()
{
return new Lazy<IConnectionMultiplexer>(
() =>
{
ConnectionMultiplexer multiplexer = null;
//Connect to Sentinel so we can find the redis master
ConnectionMultiplexer sentinelMultiplexer = ConnectionMultiplexer.Connect(SentinelOptions);
string masterNodeAddress = GetMasterNodeAddress(sentinelMultiplexer);
return ConnectionMultiplexer.Connect(masterNode);
}
);
}
然后在收到通知后,我们只需重新调用CreateRedisConnection()
.
但这完全重新创建了连接多路复用器,而不是更轻量级的方法(这可能是不可能的)。