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 监控:

时间: 2024-08-14 10:32:32

Redis Master/Slave 实践的相关文章

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

现在互联网项目中大量使用了redis,本文著主要分析下redis 单点,master/slave,sentinel模式.cluster的一些特点. 一.单节点模式 单节点实例还是比较简单的,平时做个测试,写个小程序如果需要用到缓存的话还是和方便的,现实生产环境中基本不会使用单节点模式. 二.主从模式(master/slaver)  2.1从模式特点: 主从模式的特点以及自己的一些理解. 主从模式的一个作用是备份数据,这样当一个节点损坏(指不可恢复的硬件损坏)时,数据因为有备份,可以方便恢复. 另

Redis 的 master/slave 复制

Redis 的 master/slave 复制:    Redis 的 master/slave 数据复制方式可以是一主一从或者是一主多从的方式,Redis 在 master 是非阻塞模式,也就是说在 slave 执行数据同步的时候,master 是可以接受客户端的 请求的,并不影响同步数据的一致性,然而在 slave 端是阻塞模式的,slave 在同步 master 数据时,并不能够响应客户端的查询  Redis 的 master/slave 模式下,master 提供数据读写服务,而 sla

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

类似mysql的master-slave模式一样,redis的master-slave可以提升系统的可用性,master节点写入cache后,会自动同步到slave上. 环境: master node: 10.6.144.155:7030 slave node: 10.6.144.156:7031 一.配置 仅需要在slave node上修改配置: 找到slaveof这行,参考下面的修改(填上master node的Ip和端口就完事了) slaveof 10.6.144.155 7030 另外注

Redis主从复制(Master&amp;Slave)

什么是Redis主从赋值(Master&Slave)? 1.主从复制:主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制,Master以写为 主,Slave以读为主. 2.主要作用: 1).读写分离 2).容灾恢复 Redis主从复制的配置使用(Windwos下) 1.安装主服务器,打开排至文件绑定ip 2.安装从服务器,并配置从服务器指定主服务器 3.下图是Redis同步时间的配置,900s有一个key发生改变时数据同步,300s有10个key发生改变时同步,60s有

Redis实现主从复制(Master&amp;Slave)

由于前段时间公司项目比较赶,一直抽不出时间写博客,今天偷空写一篇吧.前面给大家讲解了单机版redis的基本操作,现在继续给大家讲解一下Redis的进阶部分,主从复制和读写分离. 一.Master&Slave是什么? 也就是我们所说的主从复制,主机数据更新后根据配置和策略,自动同步到备机 的master/slaver机制,Master以写为主,Slave以读为主. 二.它能干嘛? 1.读写分离: 2.容灾恢复. 三.怎么玩? 1.配从(库)不配主(库): 2.从库配置:slaveof [主库IP]

Redis 的主从复制(Master/Slave)

1. 是什么 行话:也就是我们所说的主从复制,主机数据更新后根据配置和策略自动同步到备机的 master/slave 机制,Master以写为主,Slave 以读为主 2. 能干嘛 数据冗余:主从复制实现了数据的热备份,是持久化之外的一种数据冗余方式 故障恢复:当主节点出现问题时,可以由从节点提供服务,实现快速的故障恢复:实际上是一种服务的冗余 负载均衡:在主从复制的基础上,配合读写分离,可以由主节点提供写服务,由从节点提供读服务(即写Redis数据时应用连接主节点,读Redis数据时应用连接从

redis之master.slave主从复制

简介 主机数据更新后根据配置和策略,自动同步到备机的master/slave机制,master以写为主,slave以读为主 从库配置 配置从库,不配主库 配置从库: 格式: slaveof 主库ip 主库port 注意: 每次与master断开之后,都需要重新连接,除非配置到redis.conf文件 配置文件细节: 主从同步一--一主多从(同一台机器上同一个redis运行在三个port上) 1.复制redis配置文件三份 [[email protected] redis-5.0.3]# mkdi

redis的复制(Master/Slave)

1.是什么? 就是主从复制,主机数据更新后根据配置和策略自动同步到备机的master/slave机制,Master以写为主,Slave以读为主 2.能干嘛? • 读写分离:只有主机可以进行写操作,从机不能进行写操作(从机如果执行写操作压根就写不进去) • 容灾恢复 3.怎么玩 • 配从(库)不配主(库) • 从库配置:slave of 主库ip 主库端口 :每次与Master断开,都需要重新连接,除非你配置进redis.conf文件中 • 修改配置文件细节操作:怎么配置可以找百度 • 常用三招:

知乎技术分享:从单机到2000万QPS并发的Redis高性能缓存实践之路

本文来自知乎官方技术团队的"知乎技术专栏",感谢原作者陈鹏的无私分享. 1.引言 知乎存储平台团队基于开源Redis 组件打造的知乎 Redis 平台,经过不断的研发迭代,目前已经形成了一整套完整自动化运维服务体系,提供很多强大的功能.本文作者陈鹏是该系统的负责人,本次文章深入介绍了该系统的方方面面,值得互联网后端程序员仔细研究. (本文同步发布于:http://www.52im.net/thread-1968-1-1.html) 2.关于作者 陈鹏:现任知乎存储平台组 Redis 平