Giter Club home page Giter Club logo

Comments (6)

Choc13 avatar Choc13 commented on June 14, 2024

Hi @TechGeeky,

This library provides an extension method on IConfigurationBuilder, just like all the other configuration sources do. So anywhere you can write AddJson or AddEnvironmentVariables you can also write AddConsul to register that as a source of configuration too. In your specific case I think you want to add it like so (I've not seen this KestrelBooter pattern before though):

public KestrelBooter(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddConsul(
            "consul-testing",
            options =>
            {
                options.ConsulConfigurationOptions =
                    cco => { cco.Address = new Uri("http://consul.host.orcld.com/"); };
                options.Optional = true;
                options.PollWaitTime = TimeSpan.FromSeconds(5);
                options.ReloadOnChange = true;
            })
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

Note you're free to add other config sources as you wish, such as Json files, but I've assumed you just want consul and environment variables given your example above. The order in which you register these sources affects their precedence, see the Microsoft docs for more information.

In terms of change notifications you should look into the Options Pattern and in particular there is an OnChange method on IOptionsMonitor that you can use to register a delegate that is called when your configuration changes.

Let me know if that answer your question so I can close the issue.

from winton.extensions.configuration.consul.

TechGeeky avatar TechGeeky commented on June 14, 2024

Hi @Choc13 ,
Thanks for your response. KestrelBooter is just like Startup.cs if I am not wrong after reading out more on asp.net core fundamentals. So can I not add it in KestrelWebApiServer class in StartWebApiServer method? or does it needs to be added in KestrelBooter constructor only? Just making sure. Something like below inKestrelWebApiServer class? And I do need other config sources as already there along with this new consul config.

    public class KestrelWebApiServer : IWebApiServer
    {
        public void StartWebApiServer()
        {
            var host = new WebHostBuilder()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseKestrel(o =>
                {
                    o.Limits.MaxResponseBufferSize = null;
                    o.Limits.MinResponseDataRate = null;
                    o.Limits.MaxConcurrentConnections = 1000;
                    o.Limits.MaxConcurrentUpgradedConnections = 1000;
                    o.AllowSynchronousIO = true;
                })
                .ConfigureAppConfiguration(
                    builder =>
                    {
                        builder
                            .AddConsul(
                                "consul-testing",
                                options =>
                                {
                                    options.ConsulConfigurationOptions =
                                        cco => cco.Address = new Uri("http://consul.host.orcld.com/");
                                    options.Optional = true;
                                    options.PollWaitTime = TimeSpan.FromSeconds(5);
                                    options.ReloadOnChange = true;
                                    options.Parser = new SimpleConfigurationParser();
                                    options.OnLoadException = exceptionContext =>
                                    {
                                        // An error will be reported during startup, but it will not affect, and the follow-up is normal.
                                        Console.WriteLine("error:" + exceptionContext.Exception.Message);
                                        exceptionContext.Ignore = true;
                                    };
                                })
                            .AddEnvironmentVariables();
                    })
                .UseStartup<KestrelBooter>()
                .UseUrls("http://*:1335")
                .Build();
            host.Run();
        }
    }

And then for change notification, should I add it in ConfigureServices method in KestrelBooter class as shown below? Does this look right to you or it should not be added here? I added ChangeToken.OnChange line in ConfigureServices method.

    public KestrelBooter(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        // we doing some stuff here related to DI

        // does this looks right here?
        ChangeToken.OnChange(Configuration.GetReloadToken, _ => ConfigChanged(), new object());
    }

    public void Configure(IApplicationBuilder app, IHostApplicationLifetime lifetime)
    {
        // some stuff here
    }

from winton.extensions.configuration.consul.

TechGeeky avatar TechGeeky commented on June 14, 2024

@Choc13 any thoughts?

from winton.extensions.configuration.consul.

Choc13 avatar Choc13 commented on June 14, 2024

Hi, sorry been busy. I think it's difficult for me to give concrete advice here, because largely these are questions that depend on what you're trying to achieve with your application. On the first point about where you should register the Consul configuration source. What you have written looks technically correct, but usually I would opt to register all configuration sources in the same place during startup as it keeps things simpler. However, in some circumstances it can be the case that some configuration is required earlier in the bootstrapping process and so that might be one reason to split up their registrations. Nothing in your example makes me think this is the case for you though, so I would register the Consul configuration source in the KestrelBooter constructor like you do for the others.

Regarding the change token, this one is even hard for me to answer as it's even more dependent on your applications needs. Again, what you have written looks like it's probably technically correct, but whether it is right for your use case is up to you to decide. It really depends on what you intend to do when the configuration is changed and where you need to do that.

from winton.extensions.configuration.consul.

TechGeeky avatar TechGeeky commented on June 14, 2024

Thanks @Choc13 for your comment. I got it now. Also one more question - let's say my application started up fine without any issues and I am using this Consul library to keep a watch on the nodes in Consul server and after sometime my Consul server went down for a brief period of time but Consul server came back online after sometime then my app which is talking to consul and has a watch on some of the keys - will it still work or it will lose connection and won't be able to recover once Consul server went down? What I am trying to ask is if Consul server went down do I need to restart our apps as well to regain everything or it will work as it is without we doing anything on the app side.

Basically what I am trying to ask is - Does this library reconnect automatically and maintain all watch sessions to my Consul server that it held previously once Consul server come back online after a brief downtime?

from winton.extensions.configuration.consul.

Choc13 avatar Choc13 commented on June 14, 2024

You won't need to restart you app if the Consul server becomes unavailable. If you are using the watch functionality then it will keep polling the Consul server until it becomes available again. By default the library will wait for 5 seconds between retries when there are failures polling the Consul server. If you want finer control over that then you can set the OnWatchException when setting up the consul config provider. For example you can do:

.ConfigureAppConfiguration(
    builder =>
    {
        builder
            .AddConsul(
                "appsettings.json",
                options =>
                {
                    options.ConsulConfigurationOptions = cco => { cco.Address = new Uri("http://consul:8500"); };
                    options.Optional = true;
                    options.ReloadOnChange = true;
                    options.OnWatchException = ctx => TimeSpan.FromSeconds(ctx.ConsecutiveFailureCount)
                });
            });
}

That would increase the time between retries by 1 seconds for each consecutive failure, so that is an example of a linear backoff strategy. Of course you can do whatever you want in the OnWatchException callback. For example you could use an exponential backoff, or have no backoff at all, it's up to you.

from winton.extensions.configuration.consul.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.