Пример #1
0
        /// <summary>
        /// Daemon工作状态的主方法
        /// </summary>
        /// <param name="args"></param>
        /// <param name="Notice">通知</param>
        /// <param name="ChangeNotice">变更通知</param>
        public static void StartUp(string[] args, Action <ServiceInfo, NoticeType> Notice = null, Action <ServiceInfo, ServiceInfo> ChangeNotice = null)
        {
            var tc = ThriftConfig.CreateInstance();

            OutputLogo(tc);
            AppDomain.CurrentDomain.ProcessExit += (s, e) =>
            {
                if (Monitor.State)
                {
                    Log.WriteLine("AnnoCenter Service is being stopped·····", ConsoleColor.DarkGreen);
                    Monitor.Stop();
                    Log.WriteLine("AnnoCenter The service has stopped!", ConsoleColor.DarkGreen);
                }
            };
            Monitor.Start();

            #region  务上线 下线 变更通知

            tc.ChangeNotice += (ServiceInfo newService, ServiceInfo oldService) =>
            {
                try
                {
                    tc.RefreshServiceMd5();
                    ChangeNotice?.Invoke(newService, oldService);
                }
                finally { }
            };
            tc.OnlineNotice += (ServiceInfo service, NoticeType noticeType) =>
            {
                try
                {
                    tc.RefreshServiceMd5();
                    Notice?.Invoke(service, noticeType);
                }
                finally { }
            };
            Distribute.CheckNotice += (ServiceInfo service, NoticeType noticeType) =>
            {
                try
                {
                    tc.RefreshServiceMd5();
                    Notice?.Invoke(service, noticeType);
                }
                finally { }
            };
            #endregion
            Log.WriteLine($"服务注册、发现、健康检查、KV存储、API文档、负载均衡中心,端口:{tc.Port}(AnnoCenter)已启动!", ConsoleColor.DarkGreen);
            CronDaemon.AddJob("*/5 * * * * ? *", () =>
            {
                Parallel.ForEach(
                    tc.ServiceInfoList.Distinct().Where(s => s.Checking == false)
                    , new ParallelOptions()
                {
                    MaxDegreeOfParallelism = Environment.ProcessorCount
                }
                    , service =>
                {
                    Task.Factory.StartNew(() =>
                    {
                        Distribute.HealthCheck(service);
                    }, TaskCreationOptions.LongRunning);
                });
            });
            CronDaemon.Start();
            //阻止daemon进程退出
            new AutoResetEvent(false).WaitOne();
        }
Пример #2
0
 /// <summary>
 /// 获取微服务地址
 /// </summary>
 /// <param name="channel"></param>
 /// <returns></returns>
 public List <Micro> GetMicro(string channel)
 {
     return(Distribute.GetMicro(channel));
 }