[Articles] ASP.NET Core architecture based on updated dynamically configurable hot Consul

background

Typically, in the configuration file, e.g. App.config, Web.config appsettings.json configuration storage or .Net applications. Starting ASP.Net Core, there is a new configuration of the extensible framework that allows the outside of the profile, and store the command line configuration, environment variables, etc. are retrieved.

In traditional project, modify the configuration file, you need to log in to modify the production environment, when the project more time, not easy to maintain and manage.

We therefore Consul-line real-time configuration, it reached only change the configuration does not restart the service provides real-time response purposes. Consul realization of ideas based on the Key / value, Consul between multiple nodes can automatically synchronize configurations, our program is to achieve ConfigurationProvider, listening Consul change.

Consul Configuration

About download and install, you can refer to:

[Articles] architecture .net / c # based Key Consul implement a distributed configuration of / Value store

https://down.itsvse.com/k/zdxr0g.html

ASP.NET Core Configuration

First, the installation package nuget command is as follows:

Install-Package Consul
Install-Package Winton.Extensions.Configuration.Consul

Modify Program.cs file, the code is as follows:

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using System;
using Winton.Extensions.Configuration.Consul;

namespace WebApplication1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    var env = hostingContext.HostingEnvironment;
                    hostingContext.Configuration = config.Build();
                    string consul_url = hostingContext.Configuration["Consul_Url"];
                    Console.WriteLine(consul_url);
                    Console.WriteLine(env.ApplicationName);
                    Console.WriteLine(env.EnvironmentName);
                    config.AddConsul(
                                $"{env.ApplicationName}/appsettings.{env.EnvironmentName}.json",
                                options =>
                                {
                                    options.Optional = true;
                                    options.ReloadOnChange = true;
                                    options.OnLoadException = exceptionContext => { exceptionContext.Ignore = true; };
                                    options.ConsulConfigurationOptions = cco => { cco.Address = new Uri(consul_url); };
                                }
                                );

                    hostingContext.Configuration = config.Build();
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

More on Configuration configuration, you can refer to:

https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1

Consul configure the service center address, modify appsettings.json files, configuration is as follows:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "Consul_Url": "http://127.0.0.1:8500"
}

The controller code is as follows:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public readonly IConfiguration _configuration;

        public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
        {
            _logger = logger;
            _configuration = configuration;
        }

        public IActionResult Index()
        {
            return Ok("获取test值:" + _configuration["test"]);
        }
    }
}

Start the project, as shown below:

Because, Consul and we did not test the configuration values, so the acquisition is empty.

Open http://127.0.0.1:8500/ui/dc1/kv file configuration needed for the project.

Project profile address reads as follows:

“/WebApplication1/appsettings.Development.json”

Therefore, we need to create "WebApplication1" folder and the "appsettings.Development.json" file, as shown below:

Value as follows:

{
    "test": "itsvse"
}

Once saved, refresh our website, as shown below:

Test Chinese characters, there is no problem, as shown below:

It can be seen successfully get to the value of the test, which enables dynamic configuration capabilities, without having to re-site, it achieves a thermal profile updates.

Original link: https://down.itsvse.com/k/of9b7k.html

Guess you like

Origin www.cnblogs.com/itsvse/p/12191299.html