Beispiel #1
0
        /// <summary>
        /// 替换服务注册标签,旧标签必须存在,新标签必须有效
        /// </summary>
        /// <param name="oldTag">旧标签,如main</param>
        /// <param name="newTag">新标签,如backup</param>
        /// <returns>是否替换成功</returns>
        public bool ReplaceServiceRegisteredTag(string oldTag, string newTag)
        {
            var tagStr = processor.GetServiceTags();

            if (string.IsNullOrEmpty(tagStr) ||
                string.IsNullOrEmpty(oldTag) ||
                string.IsNullOrEmpty(newTag))
            {
                return(false);
            }
            var tags = tagStr.Split(',').Select(tag => tag.Trim()).ToArray();
            var find = Array.FindIndex(tags, tag => tag == oldTag);

            if (find < 0)
            {
                return(false);
            }
            tags[find] = newTag;
            try
            {
                return(ConsulLoader.SetKeyValue(processor.GetRegisterTagKey(), string.Join(",", tags))
                       .GetAwaiter().GetResult());
            }
            catch
            {
                return(false);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 保存分布式键值对
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <returns>是否成功</returns>
        public bool SetKeyValue(string key, string value)
        {
            try
            {
                ConsulLoader.SetKeyValue(key, value).GetAwaiter().GetResult();
            }
            catch { }

            return(ConsulCache.Instance.SetKeyValue(new Tuple <string, string>(key, value)) > 0);
        }
Beispiel #3
0
 /// <summary>
 /// 向Consul直接保存分布式键值对,不缓存
 /// </summary>
 /// <param name="key">键</param>
 /// <param name="value">值</param>
 /// <returns>是否成功</returns>
 public bool SetKeyValueNoCache(string key, string value)
 {
     return(ConsulLoader.SetKeyValue(key, value).GetAwaiter().GetResult());
 }
        /// <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;

            //注册服务
            try
            {
                ConsulLoader.RegsterService(serviceName,
                                            consulConfig.ServiceTags,
                                            consulConfig.ServicePort,
                                            consulConfig.HeartBreak,
                                            consulConfig.HttpCheck,
                                            consulConfig.TcpCheck).GetAwaiter().GetResult();
            }
            catch { throw; }

            //若不存在HttpCheck且Interval存在时发送初始心跳
            if (string.IsNullOrEmpty(consulConfig.HttpCheck) &&
                string.IsNullOrEmpty(consulConfig.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(consulConfig.HttpCheck) &&
                string.IsNullOrEmpty(consulConfig.TcpCheck) &&
                heartBreak > 0)
            {
                new Thread(HeartProcess)
                {
                    IsBackground = true
                }.Start();
            }

            //同步线程开启
            new Thread(SyncProcess)
            {
                IsBackground = true
            }.Start();
        }
Beispiel #5
0
        /// <summary>
        /// 初始化
        /// </summary>
        public void Initialize()
        {
            //读取配置
            var consulConfig = (ConsulConfigSection)ConfigurationManager.GetSection("consulplugin");

            serviceName  = consulConfig.ServiceName;
            refreshBreak = consulConfig.RefreshBreak;
            heartBreak   = consulConfig.HeartBreak;

            //注册服务
            try
            {
                ConsulLoader.RegsterService(serviceName,
                                            consulConfig.ServiceTags,
                                            consulConfig.ServicePort,
                                            consulConfig.HeartBreak,
                                            consulConfig.HttpCheck,
                                            consulConfig.TcpCheck).GetAwaiter().GetResult();
            }
            catch { }

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

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


            //读取依赖服务配置信息
            foreach (ServiceElement serviceConfig in consulConfig.Services)
            {
                ConsulCache.Instance.SetServiceTag(serviceConfig.Name, serviceConfig.ServiceTags);
                //var serviceTagKey = string.Format(ServiceTagsKeyFormat, serviceName, serviceConfig.Name, Dns.GetHostName());
                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));
            }

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

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

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