Redis跟MySQL一样,拥有非常强大的主从复制功能,而且还支持一个master可以拥有多个slave,而一个slave又可以拥有多个slave,从而形成强大的多级服务器集群架构。
         
  redis的主从复制是异步进行的,它不会影响master的运行,所以不会降低redis的处理性能。主从架构中,可以考虑关闭Master的数据持久化功能,只让Slave进行持久化,这样可以提高主服务器的处理性能。同时Slave为只读模式,这样可以避免Slave缓存的数据被误修改。

  1.配置

    实际生产中,主从架构是在几个不同服务器上安装相应的Redis服务。为了测试方便,我这边的主从备份的配置,都是在我Windows 本机上测试。

    1. 安装两个Redis 实例,Master和Slave。将Master端口设置为6379,Slave 端口设置为6380 。bind 都设置为:127.0.0.1。

    

    2. 在Slave 实例 ,增加:slaveof 127.0.0.1 6379 配置。如下图所示:

    

    配置完成之后,启动这两个实例,如果输出如下内容,说明主从复制的架构已经配置成功了。

    

    注意:在同一台电脑上测试,Master和Slave的端口不要一样,否则是不能同时启动两个实例的。

  2.测试

    在命令行,分别连接上Master服务器和Slave 服务器。然后在Master 写入缓存,然后在Slave 中读取。如下图所示:

     
 

 3.C#中调用

    主从架构的Redis的读写其实和单台Redis 的读写差不多,只是部分配置和读取区分了主从,如果不清楚C#中如何使用redis,请参考我这篇文章 《Redis总结(二)C#中如何使用redis》。

    需要注意的是:ServiceStack.Redis 中GetClient()方法,只能拿到Master redis中获取连接,而拿不到slave 的readonly连接。这样 slave起到了冗余备份的作用,读的功能没有发挥出来,如果并发请求太多的话,则Redis的性能会有影响。

    所以,我们需要的写入和读取的时候做一个区分,写入的时候,调用client.GetClient()
来获取writeHosts的Master的redis
链接。读取,则调用client.GetReadOnlyClient()来获取的readonlyHost的 Slave的redis链接。

    或者可以直接使用client.GetCacheClient() 来获取一个连接,他会在写的时候调用GetClient获取连接,读的时候调用GetReadOnlyClient获取连接,这样可以做到读写分离,从而利用redis的主从复制功能。

    1. 配置文件 app.config

    <!-- redis Start   -->
<add key="SessionExpireMinutes" value="180" />
<add key="redis_server_master_session" value="127.0.0.1:6379" />
<add key="redis_server_slave_session" value="127.0.0.1:6380" />
<add key="redis_max_read_pool" value="300" />
<add key="redis_max_write_pool" value="100" />
<!--redis end-->

    2. Redis操作的公用类RedisCacheHelper

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using ServiceStack.Common.Extensions;
using ServiceStack.Redis;
using ServiceStack.Logging; namespace Weiz.Redis.Common
{
public class RedisCacheHelper
{
private static readonly PooledRedisClientManager pool = null;
private static readonly string[] writeHosts = null;
private static readonly string[] readHosts = null;
public static int RedisMaxReadPool = int.Parse(ConfigurationManager.AppSettings["redis_max_read_pool"]);
public static int RedisMaxWritePool = int.Parse(ConfigurationManager.AppSettings["redis_max_write_pool"]);
static RedisCacheHelper()
{
var redisMasterHost = ConfigurationManager.AppSettings["redis_server_master_session"];
var redisSlaveHost = ConfigurationManager.AppSettings["redis_server_slave_session"]; if (!string.IsNullOrEmpty(redisMasterHost))
{
writeHosts = redisMasterHost.Split(',');
readHosts = redisSlaveHost.Split(','); if (readHosts.Length > )
{
pool = new PooledRedisClientManager(writeHosts, readHosts,
new RedisClientManagerConfig()
{
MaxWritePoolSize = RedisMaxWritePool,
MaxReadPoolSize = RedisMaxReadPool, AutoStart = true
});
}
}
}
public static void Add<T>(string key, T value, DateTime expiry)
{
if (value == null)
{
return;
} if (expiry <= DateTime.Now)
{
Remove(key); return;
} try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = ;
r.Set(key, value, expiry - DateTime.Now);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "存储", key);
} } public static void Add<T>(string key, T value, TimeSpan slidingExpiration)
{
if (value == null)
{
return;
} if (slidingExpiration.TotalSeconds <= )
{
Remove(key); return;
} try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = ;
r.Set(key, value, slidingExpiration);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "存储", key);
} } public static T Get<T>(string key)
{
if (string.IsNullOrEmpty(key))
{
return default(T);
} T obj = default(T); try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = ;
obj = r.Get<T>(key);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "获取", key);
} return obj;
} public static void Remove(string key)
{
try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = ;
r.Remove(key);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "删除", key);
} } public static bool Exists(string key)
{
try
{
if (pool != null)
{
using (var r = pool.GetClient())
{
if (r != null)
{
r.SendTimeout = ;
return r.ContainsKey(key);
}
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "是否存在", key);
} return false;
} public static IDictionary<string, T> GetAll<T>(IEnumerable<string> keys) where T : class
{
if (keys == null)
{
return null;
} keys = keys.Where(k => !string.IsNullOrWhiteSpace(k)); if (keys.Count() == )
{
T obj = Get<T>(keys.Single()); if (obj != null)
{
return new Dictionary<string, T>() { { keys.Single(), obj } };
} return null;
} if (!keys.Any())
{
return null;
} IDictionary<string, T> dict = null; if (pool != null)
{
keys.Select(s => new
{
Index = Math.Abs(s.GetHashCode()) % readHosts.Length,
KeyName = s
})
.GroupBy(p => p.Index)
.Select(g =>
{
try
{
using (var r = pool.GetClient(g.Key))
{
if (r != null)
{
r.SendTimeout = ;
return r.GetAll<T>(g.Select(p => p.KeyName));
}
}
}
catch (Exception ex)
{
string msg = string.Format("{0}:{1}发生异常!{2}", "cache", "获取", keys.Aggregate((a, b) => a + "," + b));
}
return null;
})
.Where(x => x != null)
.ForEach(d =>
{
d.ForEach(x =>
{
if (dict == null || !dict.Keys.Contains(x.Key))
{
if (dict == null)
{
dict = new Dictionary<string, T>();
}
dict.Add(x);
}
});
});
} IEnumerable<Tuple<string, T>> result = null; if (dict != null)
{
result = dict.Select(d => new Tuple<string, T>(d.Key, d.Value));
}
else
{
result = keys.Select(key => new Tuple<string, T>(key, Get<T>(key)));
} return result
.Select(d => new Tuple<string[], T>(d.Item1.Split('_'), d.Item2))
.Where(d => d.Item1.Length >= )
.ToDictionary(x => x.Item1[], x => x.Item2);
}
}
}

转自:https://www.cnblogs.com/zhangweizhong/p/4980639.html

【Redis】- 主从复制的更多相关文章

  1. [原]Redis主从复制各种环境下测试

    Redis 主从复制各种环境下测试 测试环境: Linux ubuntu 3.11.0-12-generic 2GB Mem 1 core of Intel(R) Core(TM) i5-3470 C ...

  2. NoSQL初探之人人都爱Redis:(4)Redis主从复制架构初步探索

    一.主从复制架构简介 通过前面几篇的介绍中,我们都是在单机上使用Redis进行相关的实践操作,从本篇起,我们将初步探索一下Redis的集群,而集群中最经典的架构便是主从复制架构.那么,我们首先来了解一 ...

  3. 【转】 NoSQL初探之人人都爱Redis:(4)Redis主从复制架构初步探索

    一.主从复制架构简介 通过前面几篇的介绍中,我们都是在单机上使用Redis进行相关的实践操作,从本篇起,我们将初步探索一下Redis的集群,而集群中最经典的架构便是主从复制架构.那么,我们首先来了解一 ...

  4. redis+Keepalived实现Redis主从复制

    redis+Keepalived实现Redis主从复制: 环境:CentOs6.5Master: 10.10.10.203Slave:   10.10.10.204Virtural IP Addres ...

  5. 深入剖析 redis 主从复制

    主从概述 redis 支持 master-slave(主从)模式,redis server 可以设置为另一个 redis server 的主机(从机),从机定期从主机拿数据.特殊的,一个 从机同样可以 ...

  6. 谈谈redis主从复制的重点

    Redis主从复制的配置十分简单,它可以使从服务器是主服务器的完全拷贝.下面是关于Redis主从复制的几点重要内容: Redis使用异步复制.但从Redis 2.8开始,从服务器会周期性的应答从复制流 ...

  7. 配置Redis主从复制

    [构建高性能数据库缓存之redis主从复制][http://database.51cto.com/art/201407/444555.htm] 一.什么是redis主从复制? 主从复制,当用户往Mas ...

  8. Redis主从复制及状态监测

    参考链接:http://www.cnblogs.com/morvenhuang/p/4184262.html #配置redis主从复制: #安装redis- master slave #修改slave ...

  9. Redis主从复制(Master/Slave)

    Redis主从复制(Master/Slave) 修改配置文件 拷贝多个redis.conf文件分别配置如下参数: 开启daemonize yes pidfile port logfile dbfile ...

  10. [转载] 深入剖析 redis 主从复制

    转载自http://www.cnblogs.com/daoluanxiaozi/p/3724299.html 主从概述 redis 支持 master-slave(主从)模式,redis server ...

随机推荐

  1. STM32(9)——通用定时器作为输入捕捉

    通用定时器作为输入捕获的使用.我们将用 TIM5 的通道 1 (PA0)来做输入捕获,捕获 PA0 上高电平的脉宽(用 WK_UP 按键输入高电平),通过串口打印高电平脉宽时间 输入捕获简介 输入捕获 ...

  2. python网络编程之进程

    一.什么是进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实 ...

  3. linux 网络编程 2---(TCP编程)

    流程 服务器:server 创建套接字 socket( ) 填充服务器网络信息结构体 sockaddr_in 将套接字与服务器网络信息结构体绑定 bind( ) 将套接字设置为被动监听状态 liste ...

  4. 食物链_KEY

    食物链 (eat.pas/c/cpp) [ 问题描述] 动物王国中有三类动物 A,B,C, 这三类动物的食物链构成了有趣的环形. A 吃 B, B 吃C, C 吃 A.现有 N 个动物, 以 1-N ...

  5. 成都Uber优步司机奖励政策(3月10日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. 【LG3527】[POI2011]MET-Meteors

    [LG3527][POI2011]MET-Meteors 题面 洛谷 题解 整体二分. 每次二分\(mid\),如果到时间\(mid\)以收集过\(P_i\)就存入子序列\(L\),否则存入子序列\( ...

  7. dubbo之注册管理中心

    一.在dubbo的框架中注册中心是必要的一个环节,这个也是分布式部署的一个必要环节.在dubbo的架构基本图中可以看出,基本上所有的服务都是通过注册中心进行注册,然后在通过注册中心,暴露出接口来. 二 ...

  8. SpringBoot学习:整合shiro(rememberMe记住我后自动登录session失效解决办法)

    项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 定义一个拦截器,判断用户是通过记住我登录时,查询数据库后台自动登录,同时把用户放入ses ...

  9. Android 9 适配怎么做? “QQ音乐”优化实录

    WeTest 导读 2018年8月7日,Google对外发布最新 Android 9.0 正式版系统,并宣布系统版本Android P 被正式命名为代号“Pie”,最新系统已经正式推送包括谷歌Pixe ...

  10. C# 简单工厂

    如下: public static IList<T> Create<T>(Type type) { if (type == typeof(List<T>)) { r ...