Exemple #1
0
        public Stream Execute(string typeName, string functionName, string args)
        {
            Stream stream = null;

            if (_isError)
            {
                ServerResponse response = new ServerResponse();
                response.IsError      = true;
                response.ErrorMessage = "系统初始化时报错!";
                stream = response.ToStream();
                return(stream);
            }
            //如果没有初始化结束,就休眠等待
            while (!_initialized)
            {
                Thread.Sleep(500);
            }
            Dictionary <string, object> dic = null;

            try
            {
                dic = JsonHelper.Deserialize <Dictionary <string, object> >(args);
            }
            catch (Exception ex)
            {
                ServerResponse response = new ServerResponse();
                response.IsError      = true;
                response.StackTrace   = ex.StackTrace;
                response.ErrorMessage = "反序列化传入json字符串时出错!Json:" + args;
                stream = response.ToStream();
                return(stream);
            }
            try
            {
                stream = _dispatcher.Execute(typeName, functionName, dic, _filterList, enableConsoleMonitor);
            }
            catch (Exception ex)
            {
                Exception     exinner    = ex;
                StringBuilder stacktrace = new StringBuilder();
                StringBuilder message    = new StringBuilder();
                while (exinner.InnerException != null)
                {
                    stacktrace.Append(exinner.StackTrace);
                    message.Append(exinner.Message);
                    exinner = exinner.InnerException;
                }
                StringBuilder log = new StringBuilder();
                log.AppendFormat("Message:{0} \r\n Stack Trace:{1}", message.ToString(), stacktrace.ToString());
                string logPath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\Logs\\";
                if (!Directory.Exists(logPath))
                {
                    Directory.CreateDirectory(logPath);
                }
                File.AppendAllText(logPath + DateTime.Now.ToString("yyMMddHH") + ".log", log.ToString());

                ServerResponse response = new ServerResponse();
                response.IsError      = true;
                response.StackTrace   = ex.StackTrace;
                response.ErrorMessage = ex.Message;
                stream = response.ToStream();
                return(stream);
            }
            return(stream);
        }
        Stream IDispatcher.Execute(string typeName, string functionName, Dictionary <string, object> args, List <BaseFilter> filterList,
                                   bool enableConsoleMonitor)
        {
            try
            {
                string interfaceName = ServicePool.Instance.GetIntefaceName(typeName, functionName);
                bool   callSuccess   = false;
                Stream stream        = null;
                while (!callSuccess)
                {
                    string url = ServicePool.Instance.GetMinCpuDispatcher();
                    //分配到自己
                    if (string.IsNullOrEmpty(url) || url == "Server")
                    {
                        stream      = ServicePool.Instance.Execute(typeName, functionName, args);
                        callSuccess = true;
                    }
                    else//分配到别的服务器
                    {
                        #region 分发到其他服务器
                        IDispatcherExecuter exec = DispatcherExecuterFactory.CreateExecuter(DispatcherExecuterType.Http);
                        string result            = null;
                        try
                        {
                            MonitorCache.GetInstance().PushMessage(
                                new CacheMessage {
                                Message = string.Format("接口:{0}开始分发到服务:{1}!", interfaceName, url), MessageType = enumMessageType.Info, TimeStamp = DateTime.Now
                            },
                                CacheEnum.LogMonitor);
                            result = exec.Execute(url, typeName, functionName, args);
                            MonitorCache.GetInstance().PushMessage(
                                new CacheMessage {
                                Message = string.Format("接口:{0}已经分发到服务:{1}并执行成功!", interfaceName, url), MessageType = enumMessageType.Info, TimeStamp = DateTime.Now
                            },
                                CacheEnum.LogMonitor);
                            stream      = new MemoryStream(Encoding.UTF8.GetBytes(result));
                            callSuccess = true;
                        }
                        catch (TimeoutException ex)
                        {
                            ServicePool.Instance.RemoveDispatcher(url);
                            MonitorCache.GetInstance().PushMessage(
                                new CacheMessage {
                                Message = string.Format("接口:{0}已经分发到服务:{1},连接服务器失败!", interfaceName, url), MessageType = enumMessageType.Info, TimeStamp = DateTime.Now
                            },
                                CacheEnum.LogMonitor);
                        }
                        #endregion
                    }
                }
                return(stream);
            }
            catch (Exception ex)
            {
                ServerResponse response = new ServerResponse();
                response.IsError      = true;
                response.ErrorMessage = ex.Message;

                return(response.ToStream());
            }
        }