Redis Master/Slave 实践
本次我们将模拟 Master(1) + Slave(4) 的场景,并通过ASP.NET WEB API进行数据的提交及查询,监控 Redis Master/Slave 数据分发情况,只大致概述,不会按照step by step的方式一一列举.
API List:
[POST]:http://localhost:53964/api/persons
Accept:application/json ,Content-Type:application/json
{
"Id": 2,
"Name": "Leo.J.Liu"
}
[GET]:http://localhost:53964/api/persons/1
Accept:application/json ,Content-Type:application/json
{
"Id": 2,
"Name": "Leo.J.Liu"
}
AutoMapper 自动转换Request DTO 与 DomainEntity
private readonly IPersonService personService; public PersonsController(IPersonService personService)
{
this.personService = personService;
}
public HttpResponseMessage GetPerson(int id)
{
var person = personService.GetPersonById(id);
if (person == null)
{
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No person with ID = {0}", id)),
ReasonPhrase = "Person ID Not Found"
};
throw new HttpResponseException(resp);
};
return Request.CreateResponse(HttpStatusCode.OK, person);
}
public HttpResponseMessage AddPerson([FromBody] PersonRequestDto personDto)
{
Person person = Mapper.Map<PersonRequestDto,Person>(personDto);
var persons = personService.AddPerson(person);
return Request.CreateResponse(HttpStatusCode.OK, persons);
}
Application_Start 中完成AutoMapper注册
public class AutoMapperConfig
{
public static void RegisterMappings()
{
Mapper.Initialize(c =>
{
c.CreateMap<PersonRequestDto,Person>().ForMember(s=>s.UserAge,d=>d.MapFrom(e=>e.Age));
});
}
}
采用StackExchange.Redis 作为Redis的Client,其中(6379为Master,提供写操作),(6380~6382为Slave,提供查询操作)
public class RedisService<T> where T : new()
{
public static ConfigurationOptions QueryConfig = new ConfigurationOptions
{
EndPoints =
{
{ "localhost", 6380 },
{ "localhost", 6381 },
{ "localhost", 6382 }
},
}; public static ConfigurationOptions SaveConfig = new ConfigurationOptions
{
EndPoints =
{
{ "localhost", 6379 }
},
}; public static T Get(string type,string key)
{
ConnectionMultiplexer redis =
ConnectionMultiplexer.Connect(QueryConfig); IDatabase db = redis.GetDatabase(); string value = db.StringGet(string.Format("{0}:{1}",type,key)); return JsonConvert.DeserializeObject<T>(value);
} public static bool Save(string type, string key, T reqDto)
{
ConnectionMultiplexer redis =
ConnectionMultiplexer.Connect(SaveConfig); IDatabase db = redis.GetDatabase(); string json = JsonConvert.SerializeObject(reqDto); return db.StringSet(string.Format("{0}:{1}", type, key), json);
}
}
SimpleInjector 作为Ioc Container
public static class SimpleInjectorWebApiInitializer
{
public static void Initialize()
{
var container = new Container(); InitializeContainer(container); container.RegisterWebApiControllers(GlobalConfiguration.Configuration); container.Verify(); GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
} private static void InitializeContainer(Container container)
{
container.Register<IPersonService, PersonService>();
container.Register<IRepository<Person>, PersonRepository>();
}
}
public class PersonRepository : IRepository<Person>
{ public List<Person> GetAll()
{
return RedisService<List<Person>>.Get("persons",string.Empty);
} public Person GetById(int id)
{
return RedisService<Person>.Get("persons",id.ToString());
} public bool Add(Person reqDto)
{
return RedisService<Person>.Save("persons", reqDto.Id.ToString(), reqDto);
} public bool Update(Person reqDto)
{
throw new NotImplementedException();
} public bool Remove(Person reqDto)
{
throw new NotImplementedException();
}
}
Redis 配置介绍:
Step1: 下载Redis
Step2: 分别创建如下图所示目录 data_1~data_4,redis_1.config~redis_4.config
data_1,redis_1.config 为Master 存储目录及配置文件
data_2~data_4,redis_2.config~ redis_4.config为Slave 存储目录及配置文件

redis_2.config~ redis_4.config配置说明:
port:6380~6381
dir:./data_2/~./data_4/
slaveof localhost 6379


Redis Desktop Manager 监控:

Redis Master/Slave 实践的更多相关文章
- Redis master/slave,sentinel,Cluster简单总结
现在互联网项目中大量使用了redis,本文著主要分析下redis 单点,master/slave,sentinel模式.cluster的一些特点. 一.单节点模式 单节点实例还是比较简单的,平时做个测 ...
- redis 学习笔记(3)-master/slave(主/从模式)
类似mysql的master-slave模式一样,redis的master-slave可以提升系统的可用性,master节点写入cache后,会自动同步到slave上. 环境: master node ...
- Redis主从复制(Master/Slave)
Redis主从复制(Master/Slave) 修改配置文件 拷贝多个redis.conf文件分别配置如下参数: 开启daemonize yes pidfile port logfile dbfile ...
- redis的主从机制 master&slave
转载自:https://www.cnblogs.com/qwangxiao/p/9733480.html 一:master&slave的解释? master&slave就是主从复制,主 ...
- Redis源码学习-Master&Slave的命令交互
0. 写在前面 Version Redis2.2.2 Redis中可以支持主从结构,本文主要从master和slave的心跳机制出发(PING),分析redis的命令行交互. 在Redis中,serv ...
- redis高可用 - Master&Slave
Master&Slave也就是我们所说的主从复制,即主机数据更新后根据配置和策略,自动同步到备机的机制.其中Master以写为主,Slave以读为主. Master&Slave的作用主 ...
- Redis的master/slave复制
摘自:Redis的master/slave复制 Redis的master/slave数据复制方式可以是一主一从或者是一主多从的方式,Redis在master是非阻塞模式,也就是说在slave执行数据同 ...
- Redis主从复制(Master/Slave) 与哨兵模式
Redis主从复制是什么? 行话:也就是我们所说的主从复制,主机数据更新后根据配置和策略, 自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主 Redis主从复制 ...
- Redis实现主从复制(Master&Slave)
由于前段时间公司项目比较赶,一直抽不出时间写博客,今天偷空写一篇吧.前面给大家讲解了单机版redis的基本操作,现在继续给大家讲解一下Redis的进阶部分,主从复制和读写分离. 一.Master&am ...
随机推荐
- php字符串截取问题
希望将一个字符串限长显示,如果该字符串超过一定长数,就截取前n个字符,后加省略号. 但是在英文和汉字混合的情况下会出现如下问题: 如果有这样一个字符串 $str="这是一个字符串" ...
- Microsoft Visual C++ Runtime error解决方法
1: 当出现下图时提示Microsoft Visual C++ Runtime error 2:此时不要关闭该对话框,然后打开任务管理器(Ctrl+Shift+Esc)如下图: 找到Microsoft ...
- cursorfilter
android.widget.CursorAdapter它首先实现了两个接口Filterable,CursorFilter.CursorFilterClient.其中Filterable接口定义了ge ...
- Laravel 5 基础(十一)- 子视图和表单复用
我们需要处理编辑文章的问题.当然我们可以手工添加新的路由,就像这样: Route::get('/articles/{id}/edit', 'ArticleController@edit'); 让我们在 ...
- 低功耗蓝牙(BLE)透传模块 ——RF-BM-S01(BQB认证)
本文来源深圳信驰达科技www.szrfstar.com,技术交流群336720020. 低功耗蓝牙(BLE)透传模块 ——RF-BM-S01(BQB认证) 深圳市信驰达科技有限公司 2013年3月18 ...
- 对java面试文章的技术漫谈的C#技术理解
.NET人技术太菜的话,要好好学习啊,所以看到Java届的面试对话文章,不经意想用C#的知识想做一些回应(其实也没有什么了不起的). 楼下知识文章扩展一览,外加自己接触到的扩展.水太深! static ...
- Python核心编程--学习笔记--8--条件与循环
本章讲述if.while.for以及与他们搭配的else.elif.break.continue.pass等语句. 1 if语句 语法:三部分——关键字if.条件表达式.代码块.(记住冒号) if c ...
- 几条sql语句
1.行.列转换 --行转列 ),科目 ),分数 int) ) ) ) ) ) ) ) ) ) --方法1 select 姓名, end) as 语文, end) as 数学, end) as 物理 f ...
- 【C#】使用C#将类序列化为XML
直接上代码: public static class XmlSerializer { public static void SaveToXml(string filePath, object sour ...
- 应聘复习基础笔记1:网络编程之TCP与UDP的优缺点,TCP三次握手、四次挥手、传输窗口控制、存在问题
重要性:必考 一.TCP与UDP的优缺点 ①TCP---传输控制协议,提供的是面向连接.可靠的字节流服务.当客户和服务器彼此交换数据前,必须先在双方之间建立一个TCP连接,之后才能传输数据.TCP提供 ...