Пример #1
0
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app     = (HttpApplication)sender;
            HttpContext     context = app.Context;
            Uri             uri     = context.Request.Url;

            #region 微服务检测与启动
            string urlAbs  = uri.AbsoluteUri;
            string urlPath = uri.PathAndQuery;
            string host    = urlAbs.Substring(0, urlAbs.Length - urlPath.Length);
            MicroService.Run.Start(host);//微服务检测、启动。
            if (!QueryTool.IsCallMicroServiceReg(uri) && MicroService.Run.Proxy(context, true))
            {
                QueryTool.SetRunProxySuccess(context);
                try
                {
                    context.Response.End();
                }
                catch (ThreadAbortException)
                {
                }

                return;
            }
            #endregion

            if (QueryTool.IsCallMvc(uri))
            {
                if (context.Request.Url.LocalPath == "/")//设置默认首页
                {
                    string defaultUrl = QueryTool.GetDefaultUrl();
                    if (!string.IsNullOrEmpty(defaultUrl))
                    {
                        context.RewritePath(defaultUrl);
                        return;
                    }
                }
                if (QueryTool.IsTaurusSuffix(uri))
                {
                    MethodInfo routeMapInvoke = MethodCollector.RouteMapInvokeMethod;
                    if (routeMapInvoke != null)
                    {
                        string url = Convert.ToString(routeMapInvoke.Invoke(null, new object[] { context.Request }));
                        if (!string.IsNullOrEmpty(url))
                        {
                            context.RewritePath(url);
                        }
                    }
                }
            }
        }
Пример #2
0
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;

            context = app.Context;
            if (context.Request.Url.LocalPath == "/")//设置默认首页
            {
                string defaultUrl = QueryTool.GetDefaultUrl();
                if (!string.IsNullOrEmpty(defaultUrl))
                {
                    context.RewritePath(defaultUrl, false);
                }
            }
        }
Пример #3
0
            /// <summary>
            /// 网关代理转发方法
            /// </summary>
            internal static bool Proxy(HttpContext context, bool isServerCall)
            {
                if ((isServerCall && !Server.IsServer) || (!isServerCall && !Client.IsClient))
                {
                    return(false);
                }
                List <HostInfo> infoList = new List <HostInfo>();
                string          module   = string.Empty;
                IPAddress       iPAddress;
                List <HostInfo> domainList = null;

                if (context.Request.Url.Host != "localhost" && !IPAddress.TryParse(context.Request.Url.Host, out iPAddress))
                {
                    module     = context.Request.Url.Host;//域名转发优先。
                    domainList = isServerCall ? MicroService.Server.GetHostList(module) : MicroService.Client.GetHostList(module);
                    if (domainList == null || domainList.Count == 0)
                    {
                        return(false);
                    }
                }

                if (context.Request.Url.LocalPath == "/")
                {
                    module = QueryTool.GetDefaultUrl().TrimStart('/').Split('/')[0];
                }
                else
                {
                    module = context.Request.Url.LocalPath.TrimStart('/').Split('/')[0];
                }
                List <HostInfo> moduleList = isServerCall ? MicroService.Server.GetHostList(module) : MicroService.Client.GetHostList(module);

                if (domainList == null || domainList.Count == 0)
                {
                    infoList = moduleList;
                }
                else if (moduleList == null || moduleList.Count == 0)
                {
                    infoList = domainList;
                }
                else
                {
                    foreach (var item in domainList)//过滤掉不在域名下的主机
                    {
                        foreach (var keyValue in moduleList)
                        {
                            if (item.Host == keyValue.Host)
                            {
                                infoList.Add(item);
                                break;
                            }
                        }
                    }
                }

                if (infoList == null || infoList.Count == 0)
                {
                    return(false);
                }
                else
                {
                    int      max         = 3;//最多循环3个节点,避免长时间循环卡机。
                    bool     isRegCenter = Server.IsRegCenterOfMaster;
                    HostInfo firstInfo   = infoList[0];
                    if (firstInfo.CallIndex >= infoList.Count)
                    {
                        firstInfo.CallIndex = 0;//处理节点移除后,CallIndex最大值的问题。
                    }
                    for (int i = 0; i < infoList.Count; i++)
                    {
                        int callIndex = firstInfo.CallIndex + i;
                        if (callIndex >= infoList.Count)
                        {
                            callIndex = callIndex - infoList.Count;
                        }
                        HostInfo info = infoList[callIndex];

                        if (info.Version < 0 || (info.CallTime > DateTime.Now && infoList.Count > 0) || (isRegCenter && info.RegTime < DateTime.Now.AddSeconds(-10))) //正常5-10秒注册1次。
                        {
                            continue;                                                                                                                                 //已经断开服务的。
                        }

                        if (Proxy(context, info.Host, isServerCall))
                        {
                            firstInfo.CallIndex = callIndex + 1;//指向下一个。
                            return(true);
                        }
                        else
                        {
                            info.CallTime = DateTime.Now.AddSeconds(10);//网络异常的,延时10s检测。
                            max--;
                            if (max == 0)
                            {
                                return(false);
                            }
                        }
                    }
                    return(false);
                }
            }