public static void ApolloConfig(HostBuilderContext hostingContext, IConfigurationBuilder builder, string[] args) { var environmentName = hostingContext.HostingEnvironment.EnvironmentName; builder.SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", true, true) .AddJsonFile($"appsettings.{environmentName}.json", true, true) .AddEnvironmentVariables() .AddCommandLine(args); // 添加环境变量 if (!builder.Build().GetValue <bool>("Apollo:Enabled", false)) { return; } var apolloSection = builder.Build().GetSection("Apollo"); var apolloBuilder = builder.AddApollo(apolloSection).AddDefault(); var apolloNamespaces = apolloSection.GetValue <string>("Namespaces"); if (string.IsNullOrWhiteSpace(apolloNamespaces)) { return; } var namespaces = apolloNamespaces.Split(','); foreach (var t in namespaces) { if (!string.IsNullOrWhiteSpace(t)) { apolloBuilder.AddNamespace(t, GetNameSpaceType(t)); } } }
/// <summary> /// apollo配置 /// 需要配置节apollo /// </summary> public static IConfigurationBuilder AddApolloExt <T>(this IConfigurationBuilder configurationBuilder) { var logger = SerilogFactory.CreateLog <T>(apiKey: ""); var config = configurationBuilder.Build().GetSection("apollo"); var appid = config.GetValue <string>("appid"); logger.Information($"apollo appid:{appid}"); if (!string.IsNullOrEmpty(appid)) { var apolloConfig = configurationBuilder.AddApollo(config); apolloConfig //.AddDefault(ConfigFileFormat.Xml) //.AddDefault(ConfigFileFormat.Json) //.AddDefault(ConfigFileFormat.Yml) //.AddDefault(ConfigFileFormat.Yaml) .AddDefault(); var ns = config.GetSection("Namespace").Get <List <string> >(); for (int i = 0; i < ns?.Count; i++) { apolloConfig.AddNamespace(ns[i]); } } logger.Dispose(); return(configurationBuilder); }
public static IConfigurationBuilder AddApolloConfiguration(this IConfigurationBuilder builder, string sectionKey, params string[] namespaces) { if (sectionKey.IsNullOrEmpty()) { throw new AngleExceptions("sectionKey cannot be null"); } builder = builder.AddApollo(builder.Build().GetSection(sectionKey)) .AddDefault() .BindNameSpace(namespaces); return(builder); }
public static IConfigurationBuilder AddApolloConfiguration(this IConfigurationBuilder builder, IConfiguration configuration, params string[] namespaces) { if (configuration.IsNull()) { throw new AngleExceptions("configuration value cannot be null"); } builder = builder.AddApollo(configuration) .AddDefault() .BindNameSpace(namespaces); return(builder); }
public static IConfigurationBuilder AddApolloConfiguration(this IConfigurationBuilder builder, ApolloOptions options) { if (options.IsNull()) { throw new AngleExceptions("apollo options value cannot be null"); } builder = builder.AddApollo(options) .AddDefault() .BindNameSpace(options.Namespace); return(builder); }
/// <summary> /// 接入Apollo /// </summary> /// <param name="builder"></param> /// <param name="jsonPath">apollo配置文件路径 如果写入appsettings.json中 则jsonPath传null即可</param> public static void AddConfigurationApollo(this IConfigurationBuilder builder, string jsonPath) { if (!string.IsNullOrEmpty(jsonPath)) { builder.AddJsonFile(jsonPath, true, false); } //阿波罗的日志级别调整 LogManager.UseConsoleLogging(LogLevel.Warn); var options = new ApolloOptions(); var root = builder.Build(); root.Bind("Apollo", options); if (options.Enable) { var apolloBuilder = builder.AddApollo(root.GetSection("Apollo:Config")); foreach (var item in options.Namespaces) { apolloBuilder.AddNamespace(item.Name, MatchConfigFileFormat(item.Format)); } //监听apollo配置 Monitor(builder.Build()); } }
public static IApolloConfigurationBuilder AddApollo(this IConfigurationBuilder builder, string appId, string metaServer) => builder.AddApollo(new ApolloOptions { AppId = appId, MetaServer = metaServer });
public static IApolloConfigurationBuilder AddApollo(this IConfigurationBuilder builder, IConfiguration apolloConfiguration) => builder.AddApollo(apolloConfiguration.Get <ApolloOptions>());
private static void ConfigureConfiguration(WebHostBuilderContext ctx, IConfigurationBuilder builder) => builder.AddApollo(builder.Build().GetSection("apollo")) .AddDefault() .AddNamespace(GlobalObject.Namespace_CAPDiscovery + ".json", GlobalObject.Namespace_CAPDiscovery) .AddNamespace(GlobalObject.Namespace_ServiceDiscovery + ".json", GlobalObject.Namespace_ServiceDiscovery);
public static IApolloConfigurationBuilder AddApollo(this IConfigurationBuilder builder) => builder.AddApollo(builder.Build().GetSection("apollo").Get <ApolloOptions>());