我的设置:
> 4台Windows服务器
>每台服务器上的Redis节点和Sentinel进程
>在每个服务器上部署相同的Web应用程序
> Web应用程序通过StackExchange.Redis驱动程序连接到redis服务器
一切都很好,但我想知道读操作是否有可能总是尝试使用本地可用的redis节点.这将大大提高性能,因为所有读取操作的跳数都会减少.
据我所知,通过Command Flags属性,可以优先考虑奴隶而不是特定命令.但有没有办法确定具体终端的优先顺序?
PS:
使用的DLL:StackExchange.Redis[email protected]
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(); } } } }
解决方法
在这种情况下.
假设你有这个案例,3VM有3个应用程序& 3个redis实例.
APP在VM1中运行的最佳选择是在VM1中使用Redis.
APP在VM2中运行的最佳选择是在VM2中使用Redis.
APP在VM3中运行的最佳选择是在VM3中使用Redis.
您可以实现一些规则,如下所示:
private static Lazy<Configurationoptions> configOptions = new Lazy<Configurationoptions>(() => { var configOptions = new Configurationoptions(); configOptions.EndPoints.Add("x.x.x.1:6379"); configOptions.EndPoints.Add("x.x.x.2:6379"); configOptions.EndPoints.Add("x.x.x.3:6379"); configOptions.ClientName = "LeakyRedisConnection"; configOptions.ConnectTimeout = 100000; configOptions.SyncTimeout = 100000; return configOptions; }); private static string getIP() { var host = Dns.GetHostEntry(Dns.GetHostName()); foreach (var ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { return ip.ToString(); } } throw new Exception("ip not found!"); } private static Lazy<Configurationoptions> getoptionsForIp(string myip) { var configOptions = new Configurationoptions(); configOptions.EndPoints.Add(myip); configOptions.ClientName = "LeakyRedisConnectionDirectVM"; configOptions.ConnectTimeout = 100000; configOptions.SyncTimeout = 100000; return configOptions; }); private static ConnectionMultiplexer conn; private static ConnectionMultiplexer LeakyConn { get { if (conn == null || !conn.IsConnected){ string myIP = getIP(); conn = ConnectionMultiplexer.Connect(getoptionsForIp(myIP).Value); if(conn == null || !conn.IsConnected){ conn = ConnectionMultiplexer.Connect(configOptions.Value); } } return conn; } }
如何使用此代码:
var db = LeakyConn.GetDatabase(); db.StringSet(key,i); db.StringGet(key);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。