本次我们将模拟 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. WordPress 主题开发 - (八) Head模板 待翻译

    THE WORDPRESS THEME HEADER TEMPLATE Now we get into the nitty-gritty: building up your header.php an ...

  2. Ubuntu通过APT配置开发环境

    apt-get install vim apt-get install ssh apt-get install apache2 apt-get install redis-server apt-get ...

  3. pandas聚合和分组运算——GroupBy技术(1)

    数据聚合与分组运算——GroupBy技术(1),有需要的朋友可以参考下. pandas提供了一个灵活高效的groupby功能,它使你能以一种自然的方式对数据集进行切片.切块.摘要等操作.根据一个或多个 ...

  4. Ruby on Rail学习笔记

    说明:只针对Windows8.1 Windows下,上rubyinstaller上下载最新的railsinstaller包含Ruby2.1的,然后更新gem 用命令: gem update --sys ...

  5. WPF自定义控件(一)——Button

    接触WPF也有两个多月了,有了一定的理论基础和项目经验,现在打算写一个系列,做出来一个WPF的控件库.一方面可以加强自己的水平,另一方面可以给正在学习WPF的同行一个参考.本人水平有限,难免有一些错误 ...

  6. C# WinForm自定义控件响应键盘事件

    自己定义的winform控件,用其他键盘事件都无法响应,只有用ProcessCmdKey事件可以达到目的(别忘了主窗体的KeyPreview属性要设置为true),写法如下:         prot ...

  7. hdu 3836 Equivalent Sets

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3836 Equivalent Sets Description To prove two sets A ...

  8. hdu 1250 Hat's Fibonacci

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Description A Fibonacci sequence ...

  9. ElasticSearch版本升级备忘录(1.5.2至2.3.1)

    discovery机制默认为单播,需配置discovery.zen.ping.unicast.hosts:,如果各节点使用默认端口,则只配IP即可(["172.17.4.47", ...

  10. Effective Objective-C 2.0之Note.02

    1.多用类型常量,少用#define预处理指令 不要用预处理指令定义常量.这样定义出来的常量不含类型信息,编译器只是会在编译前据此执行查找与替换操作.即使有人重新定义了常量值,编译器也不会产生警告信息 ...