예제 #1
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="host">连接主机信息</param>
 /// <param name="owner">连接所有者</param>
 /// <param name="config">连接配置</param>
 public ThriftClient(string hostInfo,
                     SerPool owner)
 {
     this.owner   = owner;
     this.config  = owner.Config;
     this.version = owner.Version;
     transport    = new TSocket(hostInfo.Split(':')[0],
                                int.Parse(hostInfo.Split(':')[1]));
 }
예제 #2
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="host">连接主机信息</param>
        /// <param name="owner">连接所有者</param>
        /// <param name="config">连接配置</param>
        public GrpcClient(string hostInfo,
                          SerPool owner)
        {
            this.owner   = owner;
            this.config  = owner.Config;
            this.version = owner.Version;
            var options = new List <ChannelOption> {
                new ChannelOption(ChannelOptions.MaxMessageLength, int.MaxValue)
            };

            transport = new Channel(hostInfo, ChannelCredentials.Insecure, options);
        }
예제 #3
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="host">连接主机信息</param>
 /// <param name="owner">连接所有者</param>
 /// <param name="config">连接配置</param>
 public ThriftClient(string hostInfo,
                     SerPool owner)
 {
     this.owner   = owner;
     this.config  = owner.Config;
     this.version = owner.Version;
     transport    = new TSocket(hostInfo.Split(':')[0],
                                int.Parse(hostInfo.Split(':')[1]))
     {
         Timeout = owner.Config.GetIntValue("ClientTimeout", 5000)
     };;
 }
예제 #4
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="host">连接主机信息</param>
 /// <param name="owner">连接所有者</param>
 /// <param name="config">连接配置</param>
 public HttpClient(string hostInfo,
                   SerPool owner)
 {
     this.owner   = owner;
     this.config  = owner.Config;
     this.version = owner.Version;
     this.baseUrl = this.config.GetStringValue("Protocol", "http://")
                    + hostInfo;
     transport = new System.Net.Http.HttpClient()
     {
         BaseAddress = new Uri(baseUrl)
     };
 }
예제 #5
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="host">连接主机信息</param>
 /// <param name="owner">连接所有者</param>
 /// <param name="config">连接配置</param>
 public WcfClient(ChannelFactory factory,
                  SerPool owner)
 {
     this.owner   = owner;
     this.config  = owner.Config;
     this.version = owner.Version;
     if (factory.State != CommunicationState.Opened)
     {
         factory.Open();
     }
     transport = factory.GetType().GetMethod("CreateChannel", new Type[] { })
                 .Invoke(factory, null) as ICommunicationObject;
 }
예제 #6
0
        private void SerForm_Load(object sender, EventArgs e)
        {
            // Load configuration from File
            string errMsg;

            configFileName = SerConfig.GetFileName(appDirs.ConfigDir, kpNum);
            if (File.Exists(configFileName) && !config.Load(configFileName, out errMsg))
            {
                ScadaUiUtils.ShowError(errMsg);
            }

            ConfigToControls();
        }
예제 #7
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="host">连接主机信息</param>
 /// <param name="owner">连接所有者</param>
 /// <param name="config">连接配置</param>
 public HttpClient(string hostInfo,
                   SerPool owner)
 {
     this.owner   = owner;
     this.config  = owner.Config;
     this.version = owner.Version;
     this.baseUrl = this.config.GetStringValue("Protocol", "http://")
                    + hostInfo;
     transport = new System.Net.Http.HttpClient()
     {
         BaseAddress = new Uri(baseUrl),
         Timeout     = TimeSpan.FromMilliseconds(owner.Config.GetIntValue("ClientTimeout", 5000))
     };
 }
예제 #8
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="hosts">服务器地址及端口</param>
 /// <param name="config">配置字典</param>
 public ThriftPool(string[] hosts, SerConfig config)
     : base(hosts, config)
 {
 }
예제 #9
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());
        }
예제 #10
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="hosts">服务器地址及端口</param>
 /// <param name="config">配置字典</param>
 public WcfPool(string[] hosts, SerConfig config)
     : base(hosts, config)
 {
 }
예제 #11
0
        private int kpNum;             // number of customizable control unit

        public SerForm()
        {
            InitializeComponent();

            config = new SerConfig();
        }