设为首页 收藏本站
查看: 1943|回复: 0

[经验分享] Redis Master/Slave 实践

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-7-21 09:36:24 | 显示全部楼层 |阅读模式
  本次我们将模拟 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(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().ForMember(s=>s.UserAge,d=>d.MapFrom(e=>e.Age));
});
}
}

采用StackExchange.Redis 作为Redis的Client,其中(6379为Master,提供写操作),(6380~6382为Slave,提供查询操作)


public  class RedisService 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(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();
container.Register();
}
}

  



public class PersonRepository : IRepository
{
public List GetAll()
{
return RedisService.Get("persons",string.Empty);
}
public Person GetById(int id)
{
return RedisService.Get("persons",id.ToString());
}
public bool Add(Person reqDto)
{
return RedisService.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 存储目录及配置文件
DSC0000.png
  
  redis_2.config~ redis_4.config配置说明:
  port:6380~6381
  dir:./data_2/~./data_4/
  slaveof localhost 6379
DSC0001.png
DSC0002.png
  
  Redis Desktop Manager 监控:
DSC0003.png

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-88943-1-1.html 上篇帖子: Redis实战经验及使用场景 下篇帖子: 【转】 NoSQL初探之人人都爱Redis:(4)Redis主从复制架构初步探索
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表