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 ...
随机推荐
- Mysql常用数据类型详细说明及实例说明(学习笔记一)
1.Mysql 在windows下 Net start mysql[启动] Net stop mysql[停止] Quit[退出mysql命令行] \c[取消输入的命令] Select version ...
- 修改后的SQL分页存储过程,利用2分法,支持排序
/****** Object: StoredProcedure [dbo].[sys_Page_v3] Script Date: 08/13/2014 09:32:28 ******/ SET ANS ...
- delphi常用函数过程
数据类型转化 1.1. 数值和字符串转化 Procedure Str(X [: Width [ : Decimals ]]; var S); 将数值X按照一定格式转化成字符串S.Wid ...
- 【Django】Apache上运行多个Django项目
运行单个项目的步骤参考:这里 1 安装环境 操作系统:Ubuntu 12.04 LTS 32 位(安装在VMware虚拟机中) python 版本: Python 2.7.3 Django版本 > ...
- apache 403错
<Directory />Options FollowSymLinksAllowOverride NoneOrder deny,allowAllow from all</Direct ...
- C#模糊查询绑定datagridview
private CollectionViewSource wgdData = new CollectionViewSource(); private DataTable Ds_wgd { get { ...
- meteor icons & splash配置
ionic resources http://ionicframework.com/docs/cli/icon-splashscreen.html 用一条指令生成ionic生需Icons & ...
- .Net开源数据库设计工具Mr.E For Linq (EF 6.1) 教程(二)级联删除和触发器
1.建立级联删除 Mr.E的级联删除并非数据库自带那个级联删除,而是Mr.E自带的,所以它能触发你C#里面编写的触发器. 首先,建立级联删除关系,如下图有两个表,UserInfo和UserDocume ...
- Android编程: 界面组成、事件监听器
学习知识:界面组成.事件监听器 ====界面组成==== 1.用户界面的基本组件叫做View,都是继承android.view.View类,Android里面预定义很多基本的界面组件,比如 Butto ...
- Labview实现单边带信号调制(SSB)[滤波法]
Labview实现单边带信号调制(SSB)[滤波法] 首先用信号仿真器得到一个被调制信号m(t),以及载波信号,该实验选择正弦信号作为载波信号. 根据调制器模型 得到一个结果信号. 其中,H(w)的选 ...