日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

Redis Master/Slave 实践

發(fā)布時(shí)間:2024/8/24 数据库 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Redis Master/Slave 实践 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本次我們將模擬 Master(1) + Slave(4) 的場(chǎng)景,并通過(guò)ASP.NET WEB API進(jìn)行數(shù)據(jù)的提交及查詢,監(jiān)控 Redis Master/Slave 數(shù)據(jù)分發(fā)情況,只大致概述,不會(huì)按照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 自動(dòng)轉(zhuǎn)換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注冊(cè) 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,提供寫(xiě)操作),(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: 分別創(chuàng)建如下圖所示目錄 data_1~data_4,redis_1.config~redis_4.config

data_1,redis_1.config 為Master 存儲(chǔ)目錄及配置文件

data_2~data_4,redis_2.config~ redis_4.config為Slave 存儲(chǔ)目錄及配置文件

?

redis_2.config~ redis_4.config配置說(shuō)明:

port:6380~6381

dir:./data_2/~./data_4/

slaveof localhost 6379

?

Redis Desktop Manager 監(jiān)控:

轉(zhuǎn)載于:https://www.cnblogs.com/darjuan/p/4120670.html

與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的Redis Master/Slave 实践的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。