0
点赞
收藏
分享

微信扫一扫

Redis学习笔记~Redis主从服务器,读写分离

M4Y 2022-08-25 阅读 120

​​回到目录​​

Redis这个Nosql的存储系统一般会被部署到linux系统中,我们可以把它当成是一个数据服务器,对于并发理大时,我们会使用多台服务器充当Redis服务器,这时,各个Redis之间也是分布式的,而Redis与WWW之间也是一种分布式,对于各个redis之间的分布式不需要我们去干预,它是由我们的redis客户端去负责链接的,你当时链到哪台服务器,完全由客户端去控制,redis这种模式我们通常称为“主从模式”,即一个主服务器,主要负责写入数据,多台从服务器,负责数据的读取,而它们之前的数据同步,也是redis自已为我们实现的,我们不需要去干预它,这种模式通常会称为“多级服务器集群架构”,它大大改善了程序的性能!

下面我们分别开启主redis和从redis,如图

Redis学习笔记~Redis主从服务器,读写分离_服务器

Redis学习笔记~Redis主从服务器,读写分离_服务器_02

对于配置从服务器,我们主要设置port,bind和slaveof这三个参数就可以了,port是端口,bind是从服务器的ip地址,slaveof是主服务器的地址和端口,代码如下

port 6380
bind 127.0.0.1
slaveof 127.0.0.1 6379

实例:在主服务器写入一个字符串,在从服务器读取字符串

首先对redisConfig进行相关配置,我加了一些说明

/// <summary>
/// redis主要信息的配置参数
/// </summary>
public sealed class RedisConfigInfo : ConfigurationSection
{
public static RedisConfigInfo GetConfig()
{
RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");
return section;
}

public static RedisConfigInfo GetConfig(string sectionName)
{
RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");
if (section == null)
throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
return section;
}
/// <summary>
/// 负责写入的Redis链接地址,一般为一个服务器,我们称为主服务器
/// </summary>
[ConfigurationProperty("WriteServerList", IsRequired = false)]
public string WriteServerList
{
get
{
return (string)base["WriteServerList"];
}
set
{
base["WriteServerList"] = value;
}
}


/// <summary>
/// 负责读的Redis链接地址,它一般由多个服务器组件,一般称为从服务器(slave),各个服务器之间用逗号分开
/// </summary>
[ConfigurationProperty("ReadServerList", IsRequired = false)]
public string ReadServerList
{
get
{
return (string)base["ReadServerList"];
}
set
{
base["ReadServerList"] = value;
}
}


/// <summary>
/// 最大写链接数
/// </summary>
[ConfigurationProperty("MaxWritePoolSize", IsRequired = false, DefaultValue = 5)]
public int MaxWritePoolSize
{
get
{
int _maxWritePoolSize = (int)base["MaxWritePoolSize"];
return _maxWritePoolSize > 0 ? _maxWritePoolSize : 5;
}
set
{
base["MaxWritePoolSize"] = value;
}
}


/// <summary>
/// 最大读链接数
/// </summary>
[ConfigurationProperty("MaxReadPoolSize", IsRequired = false, DefaultValue = 5)]
public int MaxReadPoolSize
{
get
{
int _maxReadPoolSize = (int)base["MaxReadPoolSize"];
return _maxReadPoolSize > 0 ? _maxReadPoolSize : 5;
}
set
{
base["MaxReadPoolSize"] = value;
}
}


/// <summary>
/// 自动重启
/// </summary>
[ConfigurationProperty("AutoStart", IsRequired = false, DefaultValue = true)]
public bool AutoStart
{
get
{
return (bool)base["AutoStart"];
}
set
{
base["AutoStart"] = value;
}
}



/// <summary>
/// 本地缓存到期时间(超时时间),单位:秒
/// </summary>
[ConfigurationProperty("LocalCacheTime", IsRequired = false, DefaultValue = 36000)]
public int LocalCacheTime
{
get
{
return (int)base["LocalCacheTime"];
}
set
{
base["LocalCacheTime"] = value;
}
}


/// <summary>
/// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
/// </summary>
[ConfigurationProperty("RecordeLog", IsRequired = false, DefaultValue = false)]
public bool RecordeLog
{
get
{
return (bool)base["RecordeLog"];
}
set
{
base["RecordeLog"] = value;
}
}

}

而配置文件中,我们可以把redis读服务器和写服务器进行配置,多个服务器使用逗号分开(redis本身就是支持读写分离的)

"192.168.2.71:6379" ReadServerList="192.168.2.71:6379,192.168.2.71:6380" MaxWritePoolSize="60" MaxReadPoolSize="60" AutoStart="true" LocalCacheTime="180" RecordeLog="false">
</RedisConfig>

下面我们向主服务器加个对象

using (var test = redisClient.GetTypedClient<string>())
{
test.Lists["Test"].Add("信息被添加");

}

当没有调用save方法时,对象只存储在内存中,数据不会被同步到从服务器,而调用了save方法后,数据才会被同步到各个从服务器中

下面我们添加了这个save方法之后,在从服务器上就会有信息同步了

using (var redisClient = RedisManager.GetClient())
{
using (var test = redisClient.GetTypedClient<string>())
{

test.Lists["bobo"].Add("info");
test.Save();
}
}

如图所示

 

Redis学习笔记~Redis主从服务器,读写分离_redis_03

Redis学习笔记~Redis主从服务器,读写分离_数据_04

对于装有防火墙的服务器来说,当然要把对应的端口开放一下,否则客户端也是不能链接上的,呵呵

Redis学习笔记~Redis主从服务器,读写分离_服务器_05

设置好之事,我们可以在命令行上测试一下从服务器的数据,如图

Redis学习笔记~Redis主从服务器,读写分离_数据_06

在WEB端进行测试

using (var redisClient = RedisManager.GetClient())
{
using (var test = redisClient.GetTypedClient<string>())
{

test.Lists["bobo"].ToList().ForEach(i =>
{
Response.Write(redisClient.Port + " " + i);
Response.Write("<br>");
});

}
}

分别进行刷新之后的结果如图

Redis学习笔记~Redis主从服务器,读写分离_redis_07

Redis学习笔记~Redis主从服务器,读写分离_redis_08

最后:一处写入,多处读取,它会从我们的所有服务器上去读取,这样大大改善了程序的相应能力,分布式将在未来对于企业来说,将会是重中之重!

​​回到目录​​

作者:仓储大叔,张占岭,
荣誉:微软MVP



举报

相关推荐

0 条评论