コード例 #1
0
        public static void AddConsulDiscovery(
            Action <ConsulOption> actAonsulOption
            , Action <List <Interceptor> > interceptorAct = null
            , Action <string, Exception> loggerWriter     = null
            , Func <IEndpointStrategy> strategyFunc       = null)
        {
            AddConsulOption(actAonsulOption);

            var serviceDiscovery = new ConsulServiceDiscovery(new ClientManager());

            if (strategyFunc == null)
            {
                strategyFunc = () => new StickyEndpointStrategy(serviceDiscovery);
            }
            var strategy = strategyFunc();
            List <Interceptor> interceptors = new List <Interceptor>();

            interceptorAct?.Invoke(interceptors);
            strategy.Interceptors = interceptors;  //加入拦截器

            CommonUtilsHelper.Configure(loggerWriter, strategy, serviceDiscovery);

            var blackTime = ConsulServerConfigWrapper.ConsulServiceConfig.BlackTime;

            if (blackTime > 0)
            {
                GlobalConfig.BlacklistPeriod = TimeSpan.FromSeconds(blackTime);
            } //更新黑名单间隔时间

            new ClientDaemon().Start();
        }
コード例 #2
0
        private static void AddConsulOption(Action <ConsulOption> actAonsulOption)
        {
            ConsulOption consulOption = new ConsulOption();

            if (actAonsulOption == null)
            {
                throw new ArgumentException("请配置ConsulOption");
            }
            actAonsulOption(consulOption);
            CommonUtilsHelper.Configure(consulOption.ConsulServerConfigPath
                                        , consulOption.GrpcServiceConfigPath
                                        , consulOption.GrpcServerRegisterPath);
        }
コード例 #3
0
        public static void AddConsulRegister(
            Action <ConsulOption> actAonsulOption
            , Action <string, Exception> loggerWriter = null
            , Func <IServiceRegister> registerFunc    = null)
        {
            AddConsulOption(actAonsulOption);

            if (registerFunc == null)
            {
                registerFunc = () => new ConsulServiceRegister(new ClientManager());
            }

            CommonUtilsHelper.Configure(loggerWriter, registerFunc());
        }
コード例 #4
0
        public static void UseConsulDiscovery(IServiceProvider serviceProvider, Action <string, Exception> loggerWriter = null)
        {
            CommonUtilsHelper.Configure(loggerWriter);

            var consulCfg = serviceProvider.GetService <IOptions <ConsulServerConfig> >();

            if (consulCfg != null && consulCfg.Value != null && consulCfg.Value.BlackTime > 0)
            {
                GlobalConfig.BlacklistPeriod = TimeSpan.FromSeconds(consulCfg.Value.BlackTime);
            }  //更新黑名单间隔时间

            var interceptors = serviceProvider.GetServices <Interceptor>()?.ToList();

            serviceProvider.GetRequiredService <IEndpointStrategy>().Interceptors = interceptors;
            serviceProvider.GetRequiredService <ClientDaemon>().Start();
        }