コード例 #1
0
 /// <summary>
 /// 注册服务
 /// </summary>
 /// <returns>是否注册成功</returns>
 private bool RegsterService()
 {
     try
     {
         return(ConsulLoader.RegsterService(serviceName,
                                            serviceTags,
                                            servicePort,
                                            heartBreak,
                                            httpCheck,
                                            tcpCheck).GetAwaiter().GetResult());
     }
     catch { }
     return(false);
 }
コード例 #2
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;

            //注册服务
            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();
        }
コード例 #3
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();
        }