Redis学习笔记~conf自主集群模式

时间:2023-12-09 19:40:25

回到目录

Redis自主提供了集群模式,当然也只是比较简单的读写分离模式,或者叫主从模式,它在各个redis服务端自己做数据同步机制,当然就是将主服务端的信息同步到各个slave服务器上,在客户端集成的驱动中,需要指定主节点和从节点的信息,服务端根据各自的conf的文件来实现的,下面我主要说一下两大客户端驱动是实现集群的方式方法。

服务端配置

服务端各个从服务器配置如何(6789为主节点的端口,6380为自己节点的端口)

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeMAAABrCAIAAADLgX1uAAAMGUlEQVR4nO2d3cGrIAyGnYuBmIdpXMZhPBdWCRB+bGu/6Hmeq9YihICvgJJOKwAA2Gb6awMAAKADSg0AYB2UGgDAOig1AIB1UGoAAOug1AAA1kGpAQCsg1IDAFgHpQYAsE5TqWfvwhKcnxtpluCmDReWLxv3NJbgpqYzTTL7abJpturP2CFFjzzpebtVThmyU3XIifw/c8XHfX63Ptq+W3Uc2Q8Uxu7n3qApu7SUehPpjlIfKb2/XKp7dw0r3MVOFc34G9XnW76/tsrf6yHdjD4vKs/gh907uPzuMvtdeGe/K/DsXShtW4J7nRtT3pjvKHVwfpb+Wldxo3Pe++LuFw+Wt/zgpsmFI+nRHPLG2XO9Vnox/F+CmybnXP+gaqdeUMVOfXgyZlKa0ruTs5xqng23pcYG530+kDkzViuM15t43MkVf8azp2nae6OaUi9IGNTo9jXjtfnllsqFefvJz3Unn0CzU62R6hBppshP2JZcyKkCnDG+1+d7PXluDv40/ZU3EXn2jYYaNapKnbRxu0V296SumUQPihKcH1yCk/fD12c5q5v90fOGb+Za6Wk+Uv/zW7R2ULdTr2bDzvSHUybtKePPKify1NHH1KMOqdhUGq818Sknv37Kba35PjmsFpS5q+Pksn/qnt9Tu+0uHm+eb0uHame9OZSi5nmOCV/ezCROJv9wTJ2lHu7J6xK899r6xSJue/WykjH1/ddmW2Pq2buw5M1WEhuu3RG1g0t2Q4idPrmNn+vfarL0/nwkEYdjTcuDup01ewaV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s7FoeKtkLRGxQNeALg97HwBALAOSg0AYB2UGgDAOig1AIB1UGoAAOug1AAA1kGpAQCsg1IDAFgHpQYAsA5KDQBgHZQaAMA6KDUAgHVQagAA66DUAADW+QcHOasiWfK/5gAAAABJRU5ErkJggg==" alt="" />

客户端配置

ServiceStack.Redis驱动

        /// <summary>
/// 创建链接池管理对象
/// </summary>
private static void CreateManager()
{
//redis写服务器集群
string[] writeServerList = ConfigManager.Config.Redis.WriteServerList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
//redis读服务器集群
string[] readServerList = ConfigManager.Config.Redis.ReadServerList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
RedisClientManagerConfig s = new RedisClientManagerConfig();
s.MaxWritePoolSize = ConfigManager.Config.Redis.MaxWritePoolSize;
s.MaxReadPoolSize = ConfigManager.Config.Redis.MaxReadPoolSize;
s.AutoStart = ConfigManager.Config.Redis.AutoStart;
prcm = new PooledRedisClientManager(writeServerList, readServerList, s);
}

StackExchange.Redis驱动

    /// <summary>
/// redis队列管理者,简单的队列添加内容,以及实时消费等功能,与Lind.DDD.CachingQueue不同它将会连接到本地的redis服务器 ,
/// 默认6379端口,多个连接通过逗号分割 。 其他选项在名称的后面包含了一个 “= ”。 例如
/// var conn = ConnectionMultiplexer.Connect("redis0:6380,redis1:6380,allowAdmin=true");
/// </summary>
public class RedisQueueManager
{
/// <summary>
/// redis连接对象
/// </summary>
static ConnectionMultiplexer conn = ConnectionMultiplexer.Connect(ConfigConstants.ConfigManager.Config.Redis.Host);
/// <summary>
/// 缓存数据库
/// </summary>
static IDatabase cache = conn.GetDatabase();

也可以通过ConfigurationOptions参数来进行配置

ConfigurationOptions config = new ConfigurationOptions
{
EndPoints =
{
{ "redis0", },
{ "redis1", }
},
CommandMap = CommandMap.Create(new HashSet<string>
{
"INFO", "CONFIG", "CLUSTER",
"PING", "ECHO", "CLIENT"
}, available: false),
KeepAlive = ,
DefaultVersion = new Version(, , ),
Password = "changeme"
};

当然,它上面代码也可以直接使用下面的字符串来代替,参数化比较灵活

redis0:,redis1:,keepAlive=,version=2.8.,$CLIENT=,$CLUSTER=,$CONFIG=,$ECHO=,$INFO=,$PING=

好了,对于redis自主集群模式就介绍到这里,下次我们来说说Redis Cluster这个集群,它也是大势所趋!

感谢各位的阅读!

回到目录