Ejemplo n.º 1
0
 /// <summary>
 /// 初始化连接池
 /// </summary>
 /// <param name="isUseWcfPool">是否使用连接池</param>
 /// <param name="wcfMaxPoolSize">池子最大值</param>
 /// <param name="wcfOutTime">获取连接超时时间</param>
 /// <param name="WcfFailureTime">连接池回收时间</param>
 /// <param name="server_name">服务器名</param>
 public static void Init(bool isUseWcfPool, int wcfMaxPoolSize, long wcfOutTime, long WcfFailureTime, string server_name,int WcfPoolMonitorReapTime)
 {
     //装在连接池
     if (isUseWcfPool && !poolDic.ContainsKey(server_name))
     {
         lock (lockpool)
         {
             if (isUseWcfPool && !poolDic.ContainsKey(server_name))
             {
                 WcfPool pool = new WcfPool(wcfMaxPoolSize, wcfOutTime, WcfFailureTime, WcfPoolMonitorReapTime);
                 poolDic.Add(server_name, pool);
             }
         }
     }
     //开启监控线程
     if (isUseWcfPool && !thDic.ContainsKey(server_name))
     {
         lock (lockth)
         {
             if (!thDic.ContainsKey(server_name))
             {
                 Thread poolMonitorTh = new Thread(poolDic[server_name].MonitorExec);
                 poolMonitorTh.Start();
                 thDic.Add(server_name, poolMonitorTh);
             }
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取服务连接
        /// </summary>
        /// <param name="serviceName">服务名</param>
        /// <param name="protocolTags">协议标签,决定连接池类型,默认使用配置内指定标签</param>
        /// <returns>服务连接</returns>
        public ISerClient GetServiceClient(string serviceName, string protocolTags = null)
        {
            //参数检查
            ISerClient client = null;

            protocolTags = protocolTags ?? ConsulCache.Instance.GetServiceTags(serviceName);
            if (string.IsNullOrEmpty(serviceName) ||
                string.IsNullOrEmpty(protocolTags))
            {
                return(client);
            }

            //筛选有效协议标签
            string protocolTag = null;

            foreach (var tag in protocolTags.Split(','))
            {
                switch (tag)
                {
                case "http":
                case "thrift":
                case "wcf":
                case "grpc":
                    protocolTag = tag;
                    break;

                default:
                    continue;
                }
            }
            if (protocolTag == null)
            {
                return(client);
            }

            //获取或创建连接池
            var     poolKey = string.Join(":", serviceName, protocolTag);
            SerPool pool    = null;

            if (serverPools.ContainsKey(poolKey))
            {
                pool = serverPools[poolKey];
            }
            else
            {
                lock (serviceLock)
                {
                    if (serverPools.ContainsKey(poolKey))
                    {
                        pool = serverPools[poolKey];
                    }
                    else
                    {
                        //读取连接池配置值
                        var config = new SerConfig();
                        foreach (var key in ConsulCache.Instance.GetKeys())
                        {
                            if (key.Contains(poolKey))
                            {
                                var configKey = key.Split(':').Last();
                                config.Add(configKey, GetKeyValue(key));

                                //设置键值回调
                                AddKvHook(key, (k, v) =>
                                {
                                    config[k.Split(':').Last()] = v;
                                });
                            }
                        }
                        //配置加入服务名
                        config.Add("ServiceName", serviceName);

                        //创建连接池
                        switch (protocolTag)
                        {
                        case "http":
                            pool = new HttpPool(GetServiceHosts(serviceName, protocolTags),
                                                config);
                            break;

                        case "thrift":
                            pool = new ThriftPool(GetServiceHosts(serviceName, protocolTags),
                                                  config);
                            break;

                        case "wcf":
                            pool = new WcfPool(GetServiceHosts(serviceName, protocolTags),
                                               config);
                            break;

                        case "grpc":
                            pool = new GrpcPool(GetServiceHosts(serviceName, protocolTags),
                                                config);
                            break;

                        default:
                            return(client);
                        }

                        //设置连接池重置回调,负载信息变更
                        AddServiceHook(serviceName, (s) =>
                        {
                            pool.ResetPool(GetServiceHosts(s, ConsulCache.Instance.GetServiceTags(s)));
                        });

                        //添加连接池
                        serverPools.Add(poolKey, pool);
                    }
                }
            }

            //返回连接
            return(pool.BorrowClient());
        }