【Redis】集群NetCore实战
环境准备
1. redis集群(windows集群搭建)
启动redis集群,给每个节点加上title
start "redis - 6379" /min redis-server.exe redis.6379.conf start "redis - 6380" /min redis-server.exe redis.6380.conf start "redis - 6381" /min redis-server.exe redis.6381.conf start "redis - 6382" /min redis-server.exe redis.6382.conf start "redis - 6383" /min redis-server.exe redis.6383.conf start "redis - 6384" /min redis-server.exe redis.6384.conf
2. 安装stackexchange.redis包
dotnet add package stackexchange.redis
连接redis
var configstring = "127.0.0.1:6379"; var options = configurationoptions.parse(configstring); options.reconnectretrypolicy = new exponentialretry(5000); var client = connectionmultiplexer.connect(options);
这里只需要提供一个节点就能访问整个集群
connectionmultiplexer 连接redis可以使用configurationoptions,或者直接字符串初始化(里面实现也是调用configurationoptions.parse转换)
这里要说一下是他的重连重试机制,在第一次创建连接的时候,stackexchange.redis会创建一个心跳检测
internal static idisposable create(connectionmultiplexer connection) { var token = new timertoken(connection); var timer = new timer(heartbeat, token, millisecondsperheartbeat, millisecondsperheartbeat); token.settimer(timer); return timer; }
使用system.threading.timer每秒钟检查一次。
stackexchange.redis 实现两种重试策略,当然我们也可以实现自己的重现策略,实现ireconnectretrypolicy接口
- exponentialretry(指数)
根据指定时间,根据重试次数不断增大随机数范围
- linearretry(线性)
根据指定时间固定时间间隔重试
常用数据结构操作
字符串(string)操作
static void redisstringoperation(connectionmultiplexer client) { var db = client.getdatabase(); //单个key操作 db.stringset("key", "wilson"); console.writeline($"get key : {db.stringget("key")}"); db.stringset("nums", 1); db.stringincrement("nums"); console.writeline($"get nums : {db.stringget("nums")}"); db.stringdecrement("nums"); console.writeline($"get nums : {db.stringget("nums")}"); //多个key操作 db.stringset(new keyvaluepair<rediskey, redisvalue>[] { new keyvaluepair<rediskey, redisvalue>("{user}name","wilson"), new keyvaluepair<rediskey, redisvalue>("{user}age",30) }); foreach (var value in db.stringget(new rediskey[] { "{user}name", "{user}age" })) { console.writeline($"{value}"); } }
哈希(hash)操作
static void redishashoperation(connectionmultiplexer client) { var db = client.getdatabase(); db.hashset("person", "name", "wilson"); console.writeline(db.hashget("person", "name")); db.hashset("person", new hashentry[] { new hashentry("name","wilson"), new hashentry("sex",1) }); console.writeline(string.join("\n", db.hashget("person", new redisvalue[] { "name", "sex" }))); console.writeline(string.join("\n", db.hashgetall("person"))); }
列表(list) 操作
static void redislistoperation(connectionmultiplexer client) { var db = client.getdatabase(); db.listleftpush("list", 100); db.listleftpush("list", 200); db.listleftpush("list", 300); db.listrightpush("list", 400); db.listrightpush("list", 500); console.writeline(db.listleftpop("list")); console.writeline(db.listrightpop("list")); console.writeline(string.join("\t", db.listrange("list", 0, 2))); console.writeline(db.listlength("list")); }
集合(set)
static void redissetoperation(connectionmultiplexer client) { var db = client.getdatabase(); db.setadd("user", "wilson"); db.setadd("user", "wilson"); db.setadd("user", "alice"); console.writeline(db.setlength("user")); console.writeline(db.setpop("user")); console.writeline(string.join("\n", db.setmembers("user"))); }
有序集合(sorted set)
static void redissortedsetoperation(connectionmultiplexer client) { var db = client.getdatabase(); db.keydelete("user"); db.sortedsetadd("user", "wilson", 90); db.sortedsetadd("user", "alice", 85); db.sortedsetadd("user", "trenary", 12); db.sortedsetadd("user", "nixon", 30); console.writeline(db.sortedsetlength("user")); console.writeline(db.sortedsetremove("user", "wilson")); console.writeline(string.join("\n", db.sortedsetrangebyrank("user", 0, 2))); }
转发请标明出处:https://www.cnblogs.com/wilsonpan/p/12677478.html
示例代码: https://github.com/wilsonpan/net.demos/tree/master/demo.rediscluster