Exemple #1
0
        /// <summary>
        /// 添加Consul配置中心
        /// 需要在Program程序启动时,在配置web主机时调用,如IHostBuilder.ConfigureWebHostDefaults
        /// 关于key的前辍说明优先顺序说明:
        /// 1、从传过来的ServiceName查找
        /// 2、从appsetting里查找ServiceName的key
        /// 3、使用当前应用程序名
        /// </summary>
        /// <param name="webBuilder">web主机生成器</param>
        /// <param name="options">配置回调</param>
        /// <returns>web主机生成器</returns>
        public static IWebHostBuilder AddConsulConfigCenter(this IWebHostBuilder webBuilder, Action <ConfigCenterOptions> options)
        {
            var configOptions = new ConfigCenterOptions();

            if (options != null)
            {
                options(configOptions);
            }

            webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
            {
                var configuration = config.Build();
                if (string.IsNullOrWhiteSpace(configOptions.ServiceName))
                {
                    configOptions.ServiceName = configuration["ServiceName"];
                }
                if (string.IsNullOrWhiteSpace(configOptions.ConsulAddress))
                {
                    configOptions.ConsulAddress = configuration["ConsulAddress"];
                    if (string.IsNullOrWhiteSpace(configOptions.ConsulAddress))
                    {
                        throw new ArgumentNullException("Consul地址不能为空");
                    }
                }

                var envi = hostingContext.HostingEnvironment;
                UtilTool.CurrServiceName     = configOptions.ServiceName;
                UtilTool.CurrApplicationName = envi.ApplicationName;

                var defaultKey = new List <string>();
                if (configOptions.AutoLoadCommonKey)
                {
                    defaultKey.Add("common.json");
                }
                defaultKey.Add(ConfigCenterUtil.GetKeyPath("appsettings.json", configOptions.ServiceName));
                defaultKey.Add(ConfigCenterUtil.GetKeyPath($"appsettings.{envi.EnvironmentName}.json", configOptions.ServiceName));

                foreach (var k in defaultKey)
                {
                    if (configOptions.Keys.Contains(k))
                    {
                        continue;
                    }

                    configOptions.Keys.Add(k);
                }

                hostingContext.Configuration = config.AddConsulConfigCenter((o) =>
                {
                    o.FillFrom(configOptions);
                })
                                               .Build();
            });

            return(webBuilder);
        }
Exemple #2
0
        /// <summary>
        /// 添加发现Consul配置中心
        /// </summary>
        /// <param name="services">服务</param>
        /// <param name="options">配置回调</param>
        /// <returns>服务</returns>
        public static IServiceCollection AddDiscoveryConsulConfigCenter(this IServiceCollection services, Action <UnityConsulOptions> options = null)
        {
            var unityConsulOptions = new UnityConsulOptions();

            if (options != null)
            {
                options(unityConsulOptions);
            }
            if (unityConsulOptions.ConsulBasicOption == null)
            {
                var config = new ConfigurationBuilder().AddJsonFile(unityConsulOptions.ConsulBasicOptionJsonFile).Build();
                services.Configure <ConsulBasicOption>(config);

                unityConsulOptions.ConsulBasicOption = config.Get <ConsulBasicOption>();
            }

            if (unityConsulOptions.ConsulBasicOption != null)
            {
                services.AddBasicConsul(unityConsulOptions.ConsulBasicOption);
            }
            else if (unityConsulOptions.ConsulBasicOptionJsonFile == null)
            {
                services.AddBasicConsul();
            }
            else
            {
                services.AddBasicConsul(unityConsulOptions.ConsulBasicOptionJsonFile);
            }

            services.AddUnityServicesBuilderConfigure(builder =>
            {
                if (unityConsulOptions.UnityServicesOptionsJsonFile != null)
                {
                    builder.ServiceBuilderConfigJsonFile = unityConsulOptions.UnityServicesOptionsJsonFile;
                }

                switch (unityConsulOptions.CacheType)
                {
                case ServiceProviderCacheType.TIMER_REFRESH:
                    builder.ServiceProvider = new ConsulServiceProviderAgg(unityConsulOptions.ConsulBasicOption.IntervalMillseconds, unityConsulOptions.ConsulBasicOption);

                    break;

                case ServiceProviderCacheType.DALAY_REFRESH:
                    services.AddMemoryCache(options =>
                    {
                        options.Clock = new LocalSystemClock();
                    });
                    services.AddMemoryCache(options =>
                    {
                        options.Clock = new LocalSystemClock();
                    });

                    builder.ServiceProvider = new ConsulServicesProviderMemory(Options.Create <ConsulBasicOption>(unityConsulOptions.ConsulBasicOption));

                    break;

                default:
                    builder.ServiceProvider = new ConsulServicesProvider(unityConsulOptions.ConsulBasicOption);

                    break;
                }
            }, (builder, file, data) =>
            {
                if (string.IsNullOrWhiteSpace(file))
                {
                    return;
                }

                builder.AddConsulConfigCenter(options: op =>
                {
                    if (unityConsulOptions.ConsulBasicOption == null)
                    {
                        if (string.IsNullOrWhiteSpace(unityConsulOptions.ConsulBasicOptionJsonFile))
                        {
                            throw new ArgumentNullException("Consul基本配置Json文件不能为空");
                        }
                        else
                        {
                            var centerOptions = JsonUtil.DeserializeFromFile <ConfigCenterOptions>(unityConsulOptions.ConsulBasicOptionJsonFile);
                            op.FillFrom(centerOptions);
                        }
                    }
                    else
                    {
                        op.FillFrom(unityConsulOptions.ConsulBasicOption);
                    }

                    var key = ConfigCenterUtil.GetKeyPath(file, op.ServiceName);
                    op.Keys.Add(key);
                });
            });

            return(services);
        }