//本地数据请求 private static string LocalDataRequest(string plugin, string controller, string method, string jsondata, HeaderParameter para) { string retJson = null; try { if (WcfGlobal.IsToken == true)//非调试模式下才验证 { //验证身份,创建连接的时候验证,请求不验证 IsAuth(plugin, controller, method, para.token); } //begintime(); //超时计时器 Stopwatch sw = new Stopwatch(); sw.Start(); //验证本地执行还是远程执行服务 MNodePlugin localPlugin = RemotePluginManage.GetLocalPlugin(); if (localPlugin.LocalPlugin.ToList().FindIndex(x => x == plugin) != -1)//本地插件 { //执行本地数据请求 retJson = ExecuteService(plugin, controller, method, jsondata, para); } else { throw new Exception("本地插件中没找到此插件:" + plugin); } //double outtime = endtime(); //记录超时的方法 if (WcfGlobal.IsOverTime == true) { if (sw.Elapsed >= new TimeSpan(WcfGlobal.OverTime * 1000 * 10000)) { WriterOverTimeLog(sw.Elapsed.TotalMilliseconds, controller + "." + method + "(" + jsondata + ")"); } } //显示调试信息 if (WcfGlobal.IsDebug == true) { ShowHostMsg(Color.Green, DateTime.Now, "服务" + controller + "." + method + "(耗时[" + sw.Elapsed.TotalMilliseconds + "])"); } //更新客户端信息 //ClientManage.UpdateRequestClient(clientId, jsondata == null ? 0 : jsondata.Length, retJson == null ? 0 : retJson.Length); if (retJson == null) { throw new Exception("插件执行未返回有效数据"); } return(retJson); } catch (Exception err) { throw err; } }
/// <summary> /// 获取本地的插件服务配置 /// ?还需要增加显示远程的服务配置 /// </summary> /// <returns></returns> public static string GetServiceConfig() { MNodePlugin mp = RemotePluginManage.GetLocalPlugin(); List <dwPlugin> pluginlist = new List <dwPlugin>(); foreach (var pname in mp.LocalPlugin) { CoreFrame.Plugin.ModulePlugin item = CoreFrame.Init.AppPluginManage.PluginDic[pname]; dwPlugin p = new dwPlugin(); p.pluginname = pname; p.controllerlist = new List <dwController>(); List <WcfControllerAttributeInfo> cmdControllerList = (List <WcfControllerAttributeInfo>)item.cache.GetData(pname + "@" + "wcfControllerAttributeList"); foreach (var cmd in cmdControllerList) { dwController c = new dwController(); c.controllername = cmd.controllerName; c.methodlist = new List <string>(); foreach (var m in cmd.MethodList) { c.methodlist.Add(m.methodName); } p.controllerlist.Add(c); } pluginlist.Add(p); } return(JsonConvert.SerializeObject(pluginlist)); }
//路径首次执行 private static MNodePath FirstGetNodePath(string plugin, MNodePlugin localPlugin) { try { List <MNodePath> pathlist = new List <MNodePath>(); string[] remoteNodeId = localPlugin.RemotePlugin.Find(x => x.PluginName == plugin).MNodeIdentify.ToArray(); MNodeTree mtree = new MNodeTree(); mtree.LoadCache(); foreach (string Id in remoteNodeId) { pathlist.Add(mtree.CalculateMNodePath(WcfGlobal.Identify, Id)); } MNodePath nodePath = null; if (localPlugin.PathStrategy == 0)//随机策略 { Random ro = new Random(); int index = ro.Next(0, pathlist.Count); nodePath = pathlist[index]; } else//最短路径? { nodePath = pathlist[0]; } return(nodePath); } catch (Exception err) { throw err; } }
/// <summary> /// 获取本地插件 /// </summary> /// <returns></returns> public static MNodePlugin GetLocalPlugin() { MNodePlugin localPlugin = GetCachePlugin(); if (localPlugin == null) { localPlugin = new MNodePlugin(); localPlugin.ServerIdentify = WcfGlobal.Identify; localPlugin.PathStrategy = 0; localPlugin.LocalPlugin = CoreFrame.Init.AppPluginManage.PluginDic.Keys.ToList(); localPlugin.RemotePlugin = new List <RemotePlugin>(); } return(localPlugin); }
/// <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 } }
//处理请求 public static string ProcessRequest(string clientId, string plugin, string controller, string method, string jsondata, HeaderParameter para) { string retJson = null; try { //显示调试信息 if (WcfGlobal.IsDebug == true) { ShowHostMsg(Color.Black, DateTime.Now, "服务正在执行:" + controller + "." + method + "(" + jsondata + ")"); } if (para.NodePath == null) { // 首次请求验证 FirstVerification(clientId, plugin, controller, method, jsondata, para); if (string.IsNullOrEmpty(para.endidentify))//计算远程节点路径 { //验证本地执行还是远程执行服务 MNodePlugin localPlugin = RemotePluginManage.GetLocalPlugin(); if (localPlugin.LocalPlugin.ToList().FindIndex(x => x == plugin) != -1)//本地插件 { //执行本地数据请求 retJson = LocalDataRequest(plugin, controller, method, jsondata, para); } else if (localPlugin.RemotePlugin.FindIndex(x => x.PluginName == plugin) != -1)//远程插件 { para.NodePath = FirstGetNodePath(plugin, localPlugin); retJson = PathNextRequest(plugin, controller, method, jsondata, para); } else { throw new Exception("本中间件节点中没有配置此插件:" + plugin); } } else//计算指定节点的路径 { MNodeTree mtree = new MNodeTree(); mtree.LoadCache(); para.NodePath = mtree.CalculateMNodePath(WcfGlobal.Identify, para.endidentify); retJson = PathNextRequest(plugin, controller, method, jsondata, para); } } else//由远程执行服务发送请求 { retJson = PathNextRequest(plugin, controller, method, jsondata, para); } //显示调试信息 if (WcfGlobal.IsDebug == true) { ShowHostMsg(Color.Green, DateTime.Now, "服务" + controller + "." + method + "执行完成:" + retJson); } //更新客户端信息 ClientManage.UpdateRequestClient(clientId, jsondata == null ? 0 : jsondata.Length, retJson == null ? 0 : retJson.Length); return(retJson); } catch (Exception err) { //记录错误日志 if (err.InnerException == null) { retJson = "{\"flag\":1,\"msg\":" + "\"" + err.Message + "\"" + "}"; if (para.iscompressjson) { retJson = ZipComporessor.Compress(retJson); } ShowHostMsg(Color.Red, DateTime.Now, "服务" + controller + "." + method + "执行失败:" + err.Message); return(retJson); } else { retJson = "{\"flag\":1,\"msg\":" + "\"" + err.InnerException.Message + "\"" + "}"; if (para.iscompressjson) { retJson = ZipComporessor.Compress(retJson); } ShowHostMsg(Color.Red, DateTime.Now, "服务" + controller + "." + method + "执行失败:" + err.InnerException.Message); return(retJson); } } }
//下载插件包 public static void DownLoadPlugin(ClientLink _clientLink) { try { if (EFWCoreLib.CoreFrame.Init.HostSettingConfig.GetValue("autoupdater") == "1")//是否启动自动升级程序 { //1.节点新增了本地插件 //2.节点本地插件更新了版本 //3.节点本地插件卸载 MNodePlugin mnplugin = RemotePluginManage.GetLocalPlugin(); if (mnplugin == null || mnplugin.LocalPinfoList == null) { return; } List <string> addplugin = new List <string>(); //新增插件 List <string> updateplugin = new List <string>(); //更新插件 List <string> deleteplugin = new List <string>(); //删除插件 //本地插件新增 foreach (string p in mnplugin.LocalPlugin) { if (AppPluginManage.PluginDic.Keys.ToList().FindIndex(x => x == p) == -1) { //新增插件 addplugin.Add(p); } } //本地插件更新 foreach (string p in mnplugin.LocalPlugin) { if (AppPluginManage.PluginDic.ContainsKey(p)) { //版本比较,是对比中心与本地插件的版本号 Version local = new Version(AppPluginManage.PluginDic[p].plugin.version); Version remote = new Version(mnplugin.LocalPinfoList.Find(x => x.pluginname == p).versions); int tm = local.CompareTo(remote); if (tm < 0)//本地版本小 { updateplugin.Add(p); //downloadRemotePlugin(pluginupgrade + p + ".zip", _clientLink); //下载之后触发安装,重启基础服务 } } } //本地插件卸载 foreach (var p in AppPluginManage.PluginDic) { if (mnplugin.LocalPlugin.FindIndex(x => x == p.Key) == -1) { //已移除插件 deleteplugin.Add(p.Key); } } //下载插件包 foreach (string p in addplugin) { downloadRemotePlugin(pluginupgrade + p + ".zip", _clientLink); } foreach (string p in updateplugin) { downloadRemotePlugin(pluginupgrade + p + ".zip", _clientLink); } //重启服务 if (addplugin.Count > 0 || updateplugin.Count > 0 || deleteplugin.Count > 0) { using (StreamWriter sw = new StreamWriter(AppGlobal.AppRootPath + WcfGlobal.pluginUpgradeFile, false)) { sw.WriteLine("addplugin:" + string.Join(",", addplugin)); sw.WriteLine("updateplugin:" + string.Join(",", updateplugin)); sw.WriteLine("deleteplugin:" + string.Join(",", deleteplugin)); } WcfGlobal.normalIPC.CallCmd(IPCName.GetProcessName(IPCType.efwplusServer), "upgradeplugin", null); } } } catch (Exception err) { CoreFrame.Common.MiddlewareLogHelper.WriterLog(err.Message + err.StackTrace); } }