我的设置:
一切都很好但我想知道读取操作是否有可能始终尝试使用本地可用的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();
}
}
}
}
答案 0 :(得分:1)
在这种情况下。
假设你有这个案例,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);