public SchedulerOption(ConsulClient consulClient, Util.Env.EnvModel _EnvModel)
 {
     this.consulKVHelper = new ConsulKVHelper(consulClient);
     this.QueueList      = new List <string>()
     {
         "share"
     };
     this.BackgroundJobServerOptions = new BackgroundJobServerOptions()
     {
         HeartbeatInterval       = TimeSpan.FromSeconds(5),
         ServerCheckInterval     = TimeSpan.FromSeconds(2),
         ServerName              = _EnvModel.APP_BUILD_NAME,
         SchedulePollingInterval = TimeSpan.FromSeconds(1),
         WorkerCount             = this.WorkerCount
     };
 }
        /// <summary>
        /// 使用RabbitMQ Logger 组件服务
        /// </summary>
        /// <param name="app"></param>
        /// <param name="loggerFactory"></param>
        /// <param name="strConsulKey"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseRabbitMQLoggerService(this IApplicationBuilder app, ILoggerFactory loggerFactory, string strConsulKey = "comm/logger/rabbitmq")
        {
            DefaultLoggerFactory = loggerFactory;
            IConsulKVHelper _ConsulKVHelper = app.ApplicationServices.GetService <IConsulKVHelper>();

            try
            {
                // 从 Consul 中获取RabbitMQ 位置
                RabbitMQLoggerOption conf = _ConsulKVHelper.GetJsonModelAsync <RabbitMQLoggerOption>(strConsulKey).Result;
                // 添加LoggerFactory
                loggerFactory.AddRabbitMQLogger(conf);
            }
            catch (Exception ex)
            {
                _ConsulKVHelper.SetValueAsync(strConsulKey, "{}");
                throw new ArgumentException($"注册中心配置[{strConsulKey}]不存在或无法转化为json", ex);
            }

            return(app);
        }
 public ConsulHealthChecker(IConsulKVHelper consulKVHelper)
 {
     this.consulKVHelper = consulKVHelper;
 }
Exemplo n.º 4
0
 public RabbitMQLoggerProvider(IConsulKVHelper consulKVHelper)
 {
     this.consulKVHelper = consulKVHelper;
 }
Exemplo n.º 5
0
        /// <summary>
        /// 使用 系统启动可以自动注册以及关闭时候反注册服务
        /// </summary>
        /// <param name="app"></param>
        /// <param name="lifetime"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseMicroService(this IApplicationBuilder app, IApplicationLifetime lifetime)
        {
            // 从管道中获取 环境变量配置
            EnvModel envModel = app.ApplicationServices.GetService <EnvModel>();

            // 从管道中获取 Consul客户端
            var consulClient = app.ApplicationServices.GetService <ConsulClient>();

            // 从管道中获取 获取 微服务配置
            AgentServiceOption _agentServiceOption = app.ApplicationServices.GetService <AgentServiceOption>();

            // 从管道中获取 获取服务注册的服务实例
            IAgentServiceHelper _AgentServiceBuilder = app.ApplicationServices.GetService <IAgentServiceHelper>();
            IConsulKVHelper     _ConsulKVHelper      = app.ApplicationServices.GetService <IConsulKVHelper>();


            // 中间件1 : 健康检查
            app.UseHealthChecks(_agentServiceOption.Service_HealthCheck_Route, new HealthCheckOptions()
            {
                ResultStatusCodes =
                {
                    [MsHealthStatus.Healthy]   = StatusCodes.Status200OK,
                    [MsHealthStatus.Degraded]  = StatusCodes.Status200OK,
                    [MsHealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable
                },
                AllowCachingResponses = false,
                ResponseWriter        = WriteResponse
            });


            #region  务注册

            // 构建 服务注册 信息
            AgentServiceRegistration serviceRegistration = _AgentServiceBuilder.BuildMicroServiceRegisterInfo(_agentServiceOption);


            // 取消注册(使用生命周期)
            lifetime.ApplicationStopping.Register(() =>
            {
                consulClient.Agent.ServiceDeregister(serviceRegistration.ID).Wait();
                Console.WriteLine("--------------------------------------------------------------------------------");
                Console.WriteLine($"---------- [{DateTime.Now}] 服务[{serviceRegistration.ID}]:反注册成功");
                Console.WriteLine("--------------------------------------------------------------------------------");
                Console.WriteLine();
            });

            // 添加 注册(先反注册再注册)
            consulClient.Agent.ServiceRegister(serviceRegistration).Wait();

            Console.WriteLine("--------------------------------------------------------------------------------");
            Console.WriteLine($"---------- [{DateTime.Now}]  服务[{serviceRegistration.ID}]:注册成功");
            Console.WriteLine("--------------------------------------------------------------------------------");
            Console.WriteLine();


            // 添加健康检查 管道
            app.UseMiddleware <HealthCheckMiddleware>();
            Console.WriteLine("--------------------------------------------------------------------------------");
            Console.WriteLine($"---------- [{DateTime.Now}] 注册:健康检查中间件启动");
            Console.WriteLine("--------------------------------------------------------------------------------");
            Console.WriteLine();

            #endregion



            #region 添加HangFire组件
            SchedulerOption schedulerOption = app.ApplicationServices.GetService <SchedulerOption>();
            if (schedulerOption.IsEnable)
            {
                Console.WriteLine("--------------------------------------------------------------------------------");
                Console.WriteLine($"---------- [{DateTime.Now}] 启动调度器服务 ----------");

                app.UseHangfireServer(options: schedulerOption.BackgroundJobServerOptions);//启动Hangfire服务

                if (schedulerOption.UseDashboard)
                {
                    Console.WriteLine($"---------- [{DateTime.Now}] 启动调度器管理页面 ----------");
                    app.UseHangfireDashboard();
                }

                Console.WriteLine("--------------------------------------------------------------------------------");
            }

            #endregion

            return(app);
        }