【发布时间】:2017-11-01 06:52:07
【问题描述】:
我的设置:
- 4 个 Windows 服务器
- 每个服务器上都有一个 Redis 节点和一个 Sentinel 进程
- 在每台服务器上部署相同的 Web 应用程序
- Web 应用程序通过 StackExchange.Redis 驱动程序连接到 redis 服务器
一切都很好,但我想知道读取操作是否有可能总是尝试使用首先在本地可用的 redis 节点。这将大大提高性能,因为所有读取操作的跳数都会减少。
据我所知,可以通过 Command Flags 属性将特定命令的从属优先于主控。 但是有没有办法确定特定端点的优先级?
PS:
使用的 DLL:StackExchange.Redis.StrongName@1.2.0.0
Redis 服务器版本:3.2.100
编辑:
这是我的连接代码。我没有使用推荐的 Lazy getter 的原因是我想在其中一个节点发生故障时连接/重新连接,这与我的解决方案非常有效。
internal class RedisConnector
{
private readonly ConfigurationOptions _currentConfiguration;
internal ConnectionMultiplexer Connection;
internal RedisCacheStore Store;
internal RedisConnector(ConfigurationOptions configuration)
{
_currentConfiguration = configuration;
Connect();
}
internal IDatabase Database
=> Connection.GetDatabase(RedisCacheConfiguration.Instance.Connection.DatabaseId);
internal IServer Server => Connection.GetServer(Database.IdentifyEndpoint());
private void Connect()
{
Connection = ConnectionMultiplexer.Connect(_currentConfiguration);
if (Connection == null || !Connection.IsConnected)
throw new CacheNotAvailableException();
Connection.ConnectionFailed += OnConnectionFailed;
Connection.ConnectionRestored += OnConnectionRestored;
Store = new RedisCacheStore(Database);
}
private void Reconnect()
{
if (Connection != null && !Connection.IsConnected)
Connection.Dispose();
Connect();
}
private void OnConnectionFailed(object sender, ConnectionFailedEventArgs args)
{
lock (_currentConfiguration)
{
if (_currentConfiguration.EndPoints.Contains(args.EndPoint))
{
_currentConfiguration.EndPoints.Remove(args.EndPoint);
Reconnect();
}
}
}
private void OnConnectionRestored(object sender, ConnectionFailedEventArgs args)
{
lock (_currentConfiguration)
{
if (!_currentConfiguration.EndPoints.Contains(args.EndPoint))
{
_currentConfiguration.EndPoints.Add(args.EndPoint);
Reconnect();
}
}
}
}
【问题讨论】:
-
你好,优先级是什么意思?
-
它是否优先考虑具有诸如尝试调用本地redis而不是调用另一个redis之类的规则的redis节点?
-
ConnectionMultiplexer 管理我的四个端点。据我所知,ConnectionMultiplexer 会查看可用的端点,并在我发出特定命令(例如 SET 或 GET)时选择一个。现在我希望 ConnectionMultiplexer 使用一个特定的端点而不是其他端点(如果它可用)。我知道我可以自己迭代端点,但我认为这就是 ConnectionMultiplexer 的用途。所以我想知道是否有办法保持该抽象完整,但也许可以通过配置 ConnectionMultiplexer 端点来做我想做的事。
-
现在明白了,这是我的答案。
标签: c# redis stackexchange.redis