侧边栏

针对net core 使用CSRedis 操作redis的三种连接实例方式

1、主从访问

 

 

2、哨兵模式

 

 

3、集群访问

 ConfigurationOptions sentinelOptions = new ConfigurationOptions();
sentinelOptions.EndPoints.Add("180.76.*.*", 26379);
sentinelOptions.EndPoints.Add("180.76.*.*", 26380);
sentinelOptions.EndPoints.Add("180.76.*.*", 26381);
sentinelOptions.TieBreaker = "";
sentinelOptions.CommandMap = CommandMap.Sentinel;
sentinelOptions.AbortOnConnectFail = false;
// Connect!
ConnectionMultiplexer sentinelConnection = ConnectionMultiplexer.Connect(sentinelOptions);

// Get a connection to the master
ConfigurationOptions redisServiceOptions = new ConfigurationOptions();
redisServiceOptions.ServiceName = "mymaster1"; //master名称
redisServiceOptions.Password = "redis_pwd"; //master访问密码
redisServiceOptions.AbortOnConnectFail = true;
ConnectionMultiplexer masterConnection = sentinelConnection.GetSentinelMasterConnection(redisServiceOptions);

var db = masterConnection.GetDatabase();
var value= db.StringGet("testKey");
Console.WriteLine($"[Use StackExchange-Redis] The remote redis-sentinel test key value:{
posted @ 2022-04-11 17:34  我有我的骄傲  阅读(532)  评论(0)    收藏  举报