Beispiel #1
0
        /// <summary>
        /// 同步本地缓存
        /// </summary>
        /// <param name="cache"></param>
        /// <returns></returns>
        private static bool SyncLocalCache(CacheObject cache)
        {
            lock (DistributedCacheManage.LocalCache)
            {
                bool isChanged = false;
                if (DistributedCacheManage.LocalCache.Contains(cache.cachename))
                {
                    CacheObject co = DistributedCacheManage.LocalCache.GetData(cache.cachename) as CacheObject;
                    if (cache.identify != co.identify)
                    {
                        //循环判断待同步的新增和修改,本地时间搓小于远程时间搓就修改
                        foreach (var t in cache.cacheValue)
                        {
                            //新增的
                            if (co.cacheValue.FindIndex(x => (x.key == t.key)) == -1)
                            {
                                co.cacheValue.Add(t);
                                isChanged = true;
                            }
                            //修改的
                            if (co.cacheValue.FindIndex(x => (x.key == t.key && t.timestamp > x.timestamp)) > -1)
                            {
                                CacheData cd = co.cacheValue.Find(x => x.key == t.key);
                                co.cacheValue.Remove(cd);
                                co.cacheValue.Add(t);
                                isChanged = true;
                            }
                        }
                    }
                }
                else
                {
                    cache.ServerIdentify = WcfGlobal.Identify;
                    DistributedCacheManage.LocalCache.Add(cache.cachename, cache);
                    if (DistributedCacheManage.CacheNameList.Contains(cache.cachename) == false)
                    {
                        DistributedCacheManage.CacheNameList.Add(cache.cachename);
                    }
                    isChanged = true;
                }

                if (isChanged == true)//同步缓存到下级中间件
                {
                    if (DistributedCacheManage.CacheChangeEvent != null)
                    {
                        DistributedCacheManage.CacheChangeEvent();
                    }
                    if (WcfGlobal.IsDebug)
                    {
                        ShowHostMsg(Color.Black, DateTime.Now, String.Format("分布式缓存同步完成,缓存名称:【{0}】,缓存记录:【{1}】", cache.cachename, (DistributedCacheManage.LocalCache.GetData(cache.cachename) as CacheObject).cacheValue.Count));
                    }
                }
            }
            return(true);
        }
        //从缓存获取节点插件
        private static MNodePlugin GetCachePlugin()
        {
            CacheObject cobj = DistributedCacheManage.GetLocalCache(cacheName);

            if (cobj != null)
            {
                List <CacheData> cdatalist = cobj.cacheValue;
                CacheData        data      = cdatalist.Find(x => x.key == WcfGlobal.Identify && x.deleteflag == false);
                if (data != null)
                {
                    //调试
                    //CoreFrame.Common.MiddlewareLogHelper.WriterLog(data.value);
                    return(JsonConvert.DeserializeObject <MNodePlugin>(data.value));
                }
            }
            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// 从缓存中加载中间件树
        /// </summary>
        public void LoadCache()
        {
            //初始化
            _allMNodeList = new List <MNodeObject>();

            CacheObject cobj = DistributedCacheManage.GetLocalCache(cacheName);

            if (cobj != null)
            {
                List <CacheData> cdatalist = cobj.cacheValue;
                foreach (var n in cdatalist)
                {
                    if (n.deleteflag == false && _allMNodeList.FindIndex(x => x.ServerIdentify == n.key) == -1)
                    {
                        MNodeObject mnodeobj = JsonConvert.DeserializeObject <MNodeObject>(n.value);
                        _allMNodeList.Add(mnodeobj);
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// 监视中间件节点树
        /// </summary>
        static void MonitorMNodeTree()
        {
            if (WcfGlobal.IsRootMNode)
            {
                #region 更新MNodeStateManage.MNodeList
                if (MNodeStateManage.MNodeList == null)
                {
                    return;
                }
                if (mnList == null)
                {
                    return;
                }

                List <MNodeObject> nodelist_run = MNodeStateManage.MNodeList;

                Dictionary <string, string> MNdic = new Dictionary <string, string>();
                foreach (var n in mnList)
                {
                    //未停用
                    if (string.IsNullOrEmpty(n.identify) == false && n.delflag == 0)
                    {
                        MNdic.Add(n.identify, n.nodename);
                    }
                }

                //初始化
                List <MNodeObject> nodelist_db = new List <MNodeObject>();
                //新增节点
                foreach (var n in MNdic)
                {
                    if (nodelist_db.FindIndex(x => x.ServerIdentify == n.Key) == -1)
                    {
                        MNodeObject mnodeobj = new MNodeObject();
                        mnodeobj.ServerIdentify = n.Key;
                        mnodeobj.ServerName     = n.Value;
                        mnodeobj.IsConnect      = false;                                  //默认未开启
                        if (WcfGlobal.IsRootMNode == true && WcfGlobal.Identify == n.Key) //根节点
                        {
                            mnodeobj.PointToMNode = n.Key;
                            mnodeobj.IsConnect    = true;//根节点默认开启
                        }
                        else
                        {
                            mnodeobj.PointToMNode = null;
                        }
                        nodelist_db.Add(mnodeobj);
                    }
                }



                //设置nodelist_db节点的状态
                foreach (var n in nodelist_db)
                {
                    MNodeObject mnodeobj = nodelist_run.Find(x => x.ServerIdentify == n.ServerIdentify);
                    if (mnodeobj != null)
                    {
                        n.IsConnect    = mnodeobj.IsConnect;
                        n.PointToMNode = mnodeobj.PointToMNode;
                    }
                }

                #endregion

                string cacheName = "mnodetree";

                #region MNodeStateManage.MNodeList同步到分布式缓存

                Dictionary <string, string> sync_adddata    = new Dictionary <string, string>(); //需要同步的数据
                Dictionary <string, string> sync_updatedata = new Dictionary <string, string>(); //需要同步的数据
                List <string> sync_deldata = new List <string>();                                //需要同步的数据

                CacheObject cobj = DistributedCacheManage.GetLocalCache(cacheName);
                if (cobj != null)
                {
                    List <CacheData> cdatalist = cobj.cacheValue;
                    //新增的
                    foreach (var n in nodelist_db)
                    {
                        if (cdatalist.FindIndex(x => x.key == n.ServerIdentify && x.deleteflag == false) == -1)
                        {
                            sync_adddata.Add(n.ServerIdentify, JsonConvert.SerializeObject(n));
                        }
                    }
                    //删除的
                    foreach (var o in cdatalist)
                    {
                        if (o.deleteflag == false && nodelist_db.FindIndex(x => x.ServerIdentify == o.key) == -1)
                        {
                            sync_deldata.Add(o.key);
                        }
                    }

                    //更新的
                    foreach (var o in cdatalist)
                    {
                        MNodeObject o1 = JsonConvert.DeserializeObject <MNodeObject>(o.value);
                        MNodeObject o2 = nodelist_db.Find(x => x.ServerIdentify == o.key);
                        if (o2 != null && o1.IsConnect != o2.IsConnect)
                        {
                            sync_updatedata.Add(o.key, JsonConvert.SerializeObject(o2));
                        }
                    }
                }
                else
                {
                    //新增的
                    foreach (var n in nodelist_db)
                    {
                        sync_adddata.Add(n.ServerIdentify, JsonConvert.SerializeObject(n));
                    }
                }

                DistributedCacheManage.SetCache(cacheName, sync_adddata, sync_updatedata, sync_deldata);

                #endregion
            }
        }
Beispiel #5
0
        /// <summary>
        /// 监视中间件节点配置的远程插件
        /// </summary>
        static void MonitorRemotePlugin()
        {
            if (WcfGlobal.IsRootMNode)
            {
                #region 加载mpList
                //从Mongodb加载节点服务配置
                List <MNodePlugin> mpList = new List <MNodePlugin>();
                foreach (var ps in npList)
                {
                    MNodePlugin mp = new MNodePlugin();
                    mp.ServerIdentify = ps.identify;
                    mp.PathStrategy   = ps.pathstrategy;
                    mp.LocalPlugin    = ps.localplugin;
                    mp.RemotePlugin   = new List <RemotePlugin>();
                    foreach (var r in ps.remoteplugin)
                    {
                        RemotePlugin rp = new RemotePlugin();
                        rp.PluginName    = r.pluginname;
                        rp.MNodeIdentify = r.mnodeidentify;
                        mp.RemotePlugin.Add(rp);
                    }
                    mp.LocalPinfoList = new List <MNPluginInfo>();
                    foreach (string p in mp.LocalPlugin)
                    {
                        PluginService pservice = psList.Find(x => x.pluginname == p);
                        if (pservice != null)
                        {
                            MNPluginInfo pinfo = new MNPluginInfo();
                            pinfo.pluginname  = pservice.pluginname;
                            pinfo.title       = pservice.title;
                            pinfo.versions    = pservice.versions;
                            pinfo.author      = pservice.author;
                            pinfo.introduce   = pservice.introduce;
                            pinfo.updatenotes = pservice.updatenotes;
                            pinfo.delflag     = pservice.delflag;
                            mp.LocalPinfoList.Add(pinfo);
                        }
                    }

                    mpList.Add(mp);
                }
                #endregion
                //CoreFrame.Common.MiddlewareLogHelper.WriterLog(JsonConvert.SerializeObject(mpList));
                #region  步mpList到分布式缓存
                Dictionary <string, string> sync_adddata    = new Dictionary <string, string>(); //需要同步的数据
                Dictionary <string, string> sync_updatedata = new Dictionary <string, string>(); //需要同步的数据
                List <string> sync_deldata = new List <string>();                                //需要同步的数据
                string        cacheName    = "mnodeplugin";

                CacheObject cobj = DistributedCacheManage.GetLocalCache(cacheName);
                if (cobj != null)
                {
                    List <CacheData> cdatalist = cobj.cacheValue;
                    //新增的
                    foreach (var n in mpList)
                    {
                        if (cdatalist.FindIndex(x => x.key == n.ServerIdentify) == -1)
                        {
                            sync_adddata.Add(n.ServerIdentify, JsonConvert.SerializeObject(n));
                        }
                    }
                    //删除的
                    foreach (var o in cdatalist)
                    {
                        if (mpList.FindIndex(x => x.ServerIdentify == o.key) == -1)
                        {
                            sync_deldata.Add(o.key);
                        }
                    }

                    //更新的
                    foreach (var o in cdatalist)
                    {
                        MNodePlugin mp = mpList.Find(x => x.ServerIdentify == o.key);
                        if (mp != null && JsonConvert.SerializeObject(mp) != o.value)
                        {
                            sync_updatedata.Add(o.key, JsonConvert.SerializeObject(mp));
                        }
                    }
                }
                else
                {
                    //新增的
                    foreach (var n in mpList)
                    {
                        sync_adddata.Add(n.ServerIdentify, JsonConvert.SerializeObject(n));
                    }
                }
                //调试
                //foreach(var i in sync_updatedata)
                //{
                //    CoreFrame.Common.MiddlewareLogHelper.WriterLog(i.Key);
                //    CoreFrame.Common.MiddlewareLogHelper.WriterLog(i.Value);
                //}
                DistributedCacheManage.SetCache(cacheName, sync_adddata, sync_updatedata, sync_deldata);
                #endregion
            }
        }