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

1、主从访问

针对net core 使用CSRedis 操作redis的三种连接实例方式_第1张图片

2、哨兵模式

针对net core 使用CSRedis 操作redis的三种连接实例方式_第2张图片

3、集群访问

写法一:写任意一个地址即可,其它节点在运行过程中自动增加,确保每个节点密码一致。如:
        Console.WriteLine("集群测试");
        RedisHelper.Initialization(new CSRedis.CSRedisClient("192.168.20.66:6379,password=123456,defaultDatabase=0"));
        RedisHelper.Set("name1", "ypf1"); //对应的槽位在6384端口上
        RedisHelper.Set("name2", "ypf2"); //对应的槽位在6379端口上
        var data1 = RedisHelper.Get("name1");
        var data2 = RedisHelper.Get("name2");
        Console.WriteLine($"data1={data1}");
        Console.WriteLine($"data2={data2}");
        Console.WriteLine("执行完毕");
写法二
        {
                Console.WriteLine("集群测试");
                var csredis = new CSRedis.CSRedisClient(null,
                            "192.168.137.202:6379,password=123456,defaultDatabase=0",
                            "192.168.137.202:6380,password=123456,defaultDatabase=0",
                            "192.168.137.202:6381,password=123456,defaultDatabase=0",
                            "192.168.137.202:6382,password=123456,defaultDatabase=0",
                            "192.168.137.202:6383,password=123456,defaultDatabase=0",
                            "192.168.137.202:6384,password=123456,defaultDatabase=0");
                RedisHelper.Initialization(csredis);

                RedisHelper.Set("name1", "ypf1");     //对应的槽位在6384端口上
                RedisHelper.Set("name2", "ypf2");     //对应的槽位在6379端口上
                var data1 = RedisHelper.Get("name1");
                var data2 = RedisHelper.Get("name2");
                Console.WriteLine($"data1={data1}");
                Console.WriteLine($"data2={data2}");

                Console.WriteLine("执行完毕");
 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:{

你可能感兴趣的:(redis,数据库,缓存)