DotNetCore跨平臺~服(fu)務(wu)總(zong)線(xian)_事件總(zong)線(xian)的重新設計
理論閑話
之前在.netFramework平臺用的(de)好好的(de),可升級(ji)到.net core平臺之后,由于不再需要二進制(zhi)序(xu)列化(hua),導致(zhi)咱們的(de)事(shi)(shi)(shi)件(jian)機制(zhi)遇到了(le)問題,之前大(da)叔(shu)的(de)事(shi)(shi)(shi)件(jian)一直是將處理(li)程序(xu)序(xu)列化(hua)后進行(xing)存(cun)儲(chu)的(de),處理(li)存(cun)儲(chu)的(de)參數為事(shi)(shi)(shi)件(jian)源,一個(ge)事(shi)(shi)(shi)件(jian)源可以由多個(ge)處理(li)程序(xu)訂閱,當(dang)事(shi)(shi)(shi)件(jian)源被發布時,這些被序(xu)列化(hua)的(de)代(dai)(dai)碼(ma)段會被回(hui)調(diao)執行(xing),這是大(da)叔(shu)之前的(de)思路,在RedisBus和MemoryBus里已經得到了(le)實(shi)現,讀(du)過(guo)大(da)叔(shu)源代(dai)(dai)碼(ma)的(de)同學應該(gai)有所了(le)解了(le)。
事件源和處理程序
/// <summary> /// 事件源(yuan) /// </summary> public class CreateUserCommand : BusData { public string UserName { get; set; } } /// <summary> /// 事件處理程序 /// </summary> public class CreateUserCommandHandler : IBusHandler<CreateUserCommand> { public void Handle(CreateUserCommand evt) { LoggerFactory.CreateLog().Logger_Debug(evt.UserName); Console.WriteLine("CreateUserCommandHandler"); } }
關于服務總線的實現方式
- RedisBus基于redis進行存儲,事件發布后,所有相關處理程序被回調,要求事件和處理程序是可序列化的
- MemoryBus基于應用服務器緩存進行存儲,所有相關處理程序被回調,集群環境不是很適合
- IoCBus基于redis作為事件字典,處理程序由IoC容器進行注入,使用場合更廣
IoCBus實現思想與組成
- 應該有一個存儲事件與處理程序對應關系的字典
- 字典應該被持久化到中間件里
- 應該有個容器,去管理字典值與處理程序的關系
代碼實現
數據變更的定義
/// <summary> /// redis key /// </summary> const string ESBKEY = "IoCESBBus"; /// <summary> /// redis事件字典(dian) /// </summary> IDatabase redis = RedisManager.Instance.GetDatabase(); /// <summary> /// 模式鎖 /// </summary> private static object _objLock = new object(); /// <summary> /// 對于事(shi)件(jian)數據的存(cun)儲,目前采用內存(cun)字典 /// </summary> private readonly IContainer container = new AutofacContainer();
事件的統一訂閱
public void SubscribeAll() { var types = AssemblyHelper.GetTypesByInterfaces(typeof(IBusHandler<>)); Dictionary<string, List<string>> keyDic = new Dictionary<string, List<string>>(); foreach (var item in types) { if (!item.IsGenericParameter) { TypeInfo typeInfo = IntrospectionExtensions.GetTypeInfo(item); foreach (var t in typeInfo.GetMethods().Where(i => i.Name == "Handle")) { //ioc name key var eventKey = t.GetParameters().First().ParameterType.Name; var key = t.GetParameters().First().ParameterType.Name + "_" + item.Name; //eventhandler var inter = typeof(IBusHandler<>).MakeGenericType(t.GetParameters().First().ParameterType); container.Register(inter, item, key); if (keyDic.ContainsKey(eventKey)) { var oldEvent = keyDic[eventKey]; oldEvent.Add(key); } else { var newEvent = new List<string>(); newEvent.Add(key); keyDic.Add(eventKey, newEvent); } } } //redis存儲事件與處理程序的(de)映射關(guan)系 foreach (var hash in keyDic) redis.HashSet( ESBKEY, hash.Key.ToString(), JsonConvert.SerializeObject(hash.Value)); } }
事件的發(fa)布(bu),相關處理程序會從容器(qi)中(zhong)取(qu)出,并執行它們的Handler方(fang)法
public void Publish<TEvent>(TEvent @event) where TEvent : class, IBusData { var keyArr = JsonConvert.DeserializeObject<List<string>>(redis.HashGet(ESBKEY, typeof(TEvent).Name)); foreach (var key in keyArr) { var item = container.ResolveNamed<IBusHandler<TEvent>>(key); item.Handle(@event); } }
說到這里,大(da)叔(shu)的服(fu)務總線的IoC實現(xian)方式(shi)就算是完成了,經(jing)過測試(shi)后,在.net core上表現(xian)也很不錯!
自己也驕傲一次,呵呵!