示例#1
0
        /// <summary>
        /// 同步分布式缓存键值对
        /// </summary>
        /// <returns></returns>
        private int SyncKeyValues()
        {
            var kvs = new List <Tuple <string, string> >();

            //刷新键值
            try
            {
                foreach (var key in ConsulCache.Instance.GetKeys())
                {
                    var value = ConsulLoader.GetKeyValue(key)
                                .GetAwaiter().GetResult();
                    if (value != null)
                    {
                        kvs.Add(new Tuple <string, string>(key, value));
                    }
                }
            }
            catch { }

            int count = ConsulCache.Instance.SetKeyValue(kvs.ToArray());

            //刷新Tags
            foreach (var sName in ConsulCache.Instance.GetServiceNames())
            {
                //var serviceTagKey = string.Format(ServiceTagsKeyFormat, serviceName, sName, Dns.GetHostName());
                var serviceTagKey = GetServiceTagsKey(sName);
                ConsulCache.Instance.SetServiceTag(sName, ConsulCache.Instance.GetKeyValue(serviceTagKey));
            }

            return(count);
        }
示例#2
0
 /// <summary>
 /// 服务心跳发送
 /// </summary>
 /// <returns>是否发送成功</returns>
 private bool HeartBreakService()
 {
     try
     {
         return(ConsulLoader.HeartBreak("service:" + serviceName).GetAwaiter().GetResult());
     }
     catch
     {
         return(false);
     }
 }
示例#3
0
 /// <summary>
 /// 注册服务
 /// </summary>
 /// <returns>是否注册成功</returns>
 private bool RegsterService()
 {
     try
     {
         return(ConsulLoader.RegsterService(serviceName,
                                            serviceTags,
                                            servicePort,
                                            heartBreak,
                                            httpCheck,
                                            tcpCheck).GetAwaiter().GetResult());
     }
     catch { }
     return(false);
 }
示例#4
0
        /// <summary>
        /// 同步可用服务信息
        /// </summary>
        /// <param name="serviceNames">服务名集合</param>
        /// <returns>同步更新可用服务负载点数量</returns>
        private int SyncServices()
        {
            var count = 0;

            try
            {
                foreach (var serviceName in ConsulCache.Instance.GetServiceNames())
                {
                    var services = ConsulLoader.AvaliableServices(serviceName,
                                                                  ConsulCache.Instance.GetServiceTags(serviceName))
                                   .GetAwaiter().GetResult();
                    count += ConsulCache.Instance.SetServiceInfo(serviceName, services);
                }
            }
            catch { }
            return(count);
        }
示例#5
0
        /// <summary>
        /// 初始化
        /// </summary>
        public void Initialize()
        {
            //string basePath = null;
#if !CORECLR
            //basePath = System.Web.HttpRuntime.AppDomainAppPath;
#else
            basePath = Directory.GetCurrentDirectory();
#endif

            //读取配置
            // var consulConfig = new ConfigurationBuilder()
            //     .SetBasePath(basePath)
            //     .AddJsonFile("Config/consulplugin.json", true, false)
            //     .Build().Get<ConsulConfigSection>();
            // serviceName = consulConfig.ServiceName;
            // refreshBreak = consulConfig.RefreshBreak;
            // heartBreak = consulConfig.HeartBreak;
            // serviceTags = consulConfig.ServiceTags;
            // servicePort = consulConfig.ServicePort;
            // httpCheck = consulConfig.HttpCheck;
            // tcpCheck = consulConfig.TcpCheck;


            //集中控制服务注册标签覆盖
            try
            {
                var remoteServiceTags = ConsulLoader.GetKeyValue(GetRegisterTagKey()).GetAwaiter().GetResult();
                if (!string.IsNullOrEmpty(remoteServiceTags))
                {
                    ConsulCache.Instance.SetKeyValue(new Tuple <string, string>(GetRegisterTagKey(), remoteServiceTags));
                    serviceTags = remoteServiceTags;
                }
                else if (!string.IsNullOrEmpty(serviceTags))
                {
                    //注册可控服务标签
                    ConsulLoader.SetKeyValue(GetRegisterTagKey(), serviceTags).GetAwaiter().GetResult();
                    ConsulCache.Instance.SetKeyValue(new Tuple <string, string>(GetRegisterTagKey(), serviceTags));
                }
            }
            catch { }

            //注册服务
            if (RegsterService())
            {
                //注册服务标签变更回调
                ConsulCache.Instance.AddKvHook(GetRegisterTagKey(),
                                               (k, v) =>
                {
                    if (!string.IsNullOrEmpty(v) &&
                        v != this.serviceTags)
                    {
                        this.serviceTags = v;
                        RegsterService();
                    }
                });
            }

            //若不存在HttpCheck且Interval存在时发送初始心跳
            if (string.IsNullOrEmpty(httpCheck) &&
                string.IsNullOrEmpty(tcpCheck) &&
                heartBreak > 0)
            {
                HeartBreakService();
            }

            // //读取缓存键值对配置信息
            // if (consulConfig.KeyValues != null)
            // {
            //     foreach (KeyValueElement kvConfig in consulConfig.KeyValues)
            //     {
            //         ConsulCache.Instance.SetKeyValue(new Tuple<string, string>(kvConfig.Name, kvConfig.Value));
            //     }
            // }


            // //读取依赖服务配置信息
            // if (consulConfig.Services != null)
            // {
            //     foreach (ServiceElement serviceConfig in consulConfig.Services)
            //     {
            //         ConsulCache.Instance.SetServiceTag(serviceConfig.Name, serviceConfig.ServiceTags);
            //         var serviceTagKey = GetServiceTagsKey(serviceConfig.Name);
            //         try
            //         {
            //             if (string.IsNullOrEmpty(ConsulLoader.GetKeyValue(serviceTagKey).GetAwaiter().GetResult()))
            //             {
            //                 ConsulLoader.SetKeyValue(serviceTagKey, serviceConfig.ServiceTags).GetAwaiter().GetResult();
            //             }
            //         }
            //         catch { }
            //         ConsulCache.Instance.SetKeyValue(new Tuple<string, string>(serviceTagKey, serviceConfig.ServiceTags));
            //     }
            // }

            //初次同步依赖服务信息及缓存信息
            SyncKeyValues();
            SyncServices();

            //若不存在HttpCheck且Interval存在时开启心跳线程
            if (string.IsNullOrEmpty(httpCheck) &&
                string.IsNullOrEmpty(tcpCheck) &&
                heartBreak > 0)
            {
                new Thread(HeartProcess)
                {
                    IsBackground = true
                }.Start();
            }

            //同步线程开启
            new Thread(SyncProcess)
            {
                IsBackground = true
            }.Start();
        }