本次我们将模拟 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 实践的更多相关文章

  1. Redis master/slave,sentinel,Cluster简单总结

    现在互联网项目中大量使用了redis,本文著主要分析下redis 单点,master/slave,sentinel模式.cluster的一些特点. 一.单节点模式 单节点实例还是比较简单的,平时做个测 ...

  2. redis 学习笔记(3)-master/slave(主/从模式)

    类似mysql的master-slave模式一样,redis的master-slave可以提升系统的可用性,master节点写入cache后,会自动同步到slave上. 环境: master node ...

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

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

  4. redis的主从机制 master&slave

    转载自:https://www.cnblogs.com/qwangxiao/p/9733480.html 一:master&slave的解释? master&slave就是主从复制,主 ...

  5. Redis源码学习-Master&Slave的命令交互

    0. 写在前面 Version Redis2.2.2 Redis中可以支持主从结构,本文主要从master和slave的心跳机制出发(PING),分析redis的命令行交互. 在Redis中,serv ...

  6. redis高可用 - Master&Slave

    Master&Slave也就是我们所说的主从复制,即主机数据更新后根据配置和策略,自动同步到备机的机制.其中Master以写为主,Slave以读为主. Master&Slave的作用主 ...

  7. Redis的master/slave复制

    摘自:Redis的master/slave复制 Redis的master/slave数据复制方式可以是一主一从或者是一主多从的方式,Redis在master是非阻塞模式,也就是说在slave执行数据同 ...

  8. Redis主从复制(Master/Slave) 与哨兵模式

    Redis主从复制是什么? 行话:也就是我们所说的主从复制,主机数据更新后根据配置和策略, 自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主 Redis主从复制 ...

  9. Redis实现主从复制(Master&Slave)

    由于前段时间公司项目比较赶,一直抽不出时间写博客,今天偷空写一篇吧.前面给大家讲解了单机版redis的基本操作,现在继续给大家讲解一下Redis的进阶部分,主从复制和读写分离. 一.Master&am ...

随机推荐

  1. thinkphp foreach循环生成二维数组的方法

    先做个问题记录,另外下面是做的过程中遇到的一个没想明白的现象 foreach($result as $key => $val ){ $wzList[$key]['lik']=$val[0]; $ ...

  2. GetType() 和typeof() 的区别

    GetType() 非强类型,支持跨程序集发射,用来支持动态引用, A obja=new A(); Type t=obja.GetType() typeof() 强类型,静态的 Type t=type ...

  3. Python 2.7的安装(64位win10)

    Python 2.7.12 下载地址:https://www.python.org/downloads/release/python-2712/ 安装路径D:\Program Files\Python ...

  4. 4.html5中超链接

    html中超链接都是通过<a>标签实现的,html5也不例外,这里就来探讨一下<a>标签. <a>元素属于文本元素,有一些私有属性或者叫局部属性.那么,相对应的还有 ...

  5. vagrant

    puppet chef ansible salt docker https://github.com/ansible/ansiblehttps://github.com/saltstack/salth ...

  6. MySQL 出现 Access denied for user 'root'@'localhost' (using password: YES) 错误

    登录某台服务器的mysql时候总报错: mysql2/client.rb:58:in `connect': Access denied for user 'root'@'localhost' (usi ...

  7. Linux软链接与硬链接

    1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过索引节点 ...

  8. Virtual Box + CentOS Minimal + Apache搭建Web服务器

    本文并不介绍关于Virtual Box, CentOS, Apache的安装, 主要针对安装后相关的配置, 使宿主机(Host)可以访问客户机(Guest: CentOS in Virtual Box ...

  9. 获取 UIWebView中用户所点击的图片URL

    在使用 UIWebView 的时候 (通常是阅读类的 App),会有点击图片放大的需求,那么可以通过设置 UIWebViewDelegate 来过滤请求,取出图片的 URL 这个方法的前提是 img ...

  10. [原创]基于html5新标签canvas写的一个小画板

    最近刚学了canvas,写个小应用练习下 源代码 <!DOCTYPE> <html> <head> <meta http-equiv="Conten ...