describe

The lightweight configuration center based on NetCore is easy to deploy, configure, and use. It can be used according to the needs of individuals or companies.

  • The deployment is simple, requiring at least one data node and supporting docker deployment
  • Supports distributed deployment of multiple nodes to ensure high availability
  • The configuration supports application isolation and intra-application isolation by group
  • Using the long link technology, the configuration information is pushed to the client in real time
  • Support IConfiguration, IOptions mode read configuration, the original program almost without transformation
  • Configuration modification support version record, roll back configuration at any time
  • All nodes fail and the client supports reading configuration from the local cache

Making the address: https://github.com/kklldog/AgileConfig can give the bosses point of a star

Using Apollo for deployment as a configuration center would be too cumbersome for some companies, so I personally recommend it. Thanks again to KKLLDog for their open source project.

The deployment of

Docker deployment, currently support sqlserver, mysql, sqlite, PostgreSql, Oracle five databases. This example uses lightweight SQLite as the data store

docker run -d --name agile_config -e adminConsole=true -e db:provider=sqlite -e db:conn="Data Source=agile_config.db" -p 8011:5000 kklldog/agile_config:latest 
Copy the code
  1. Whether the adminConsole configurator is the administrative console. If true, console functionality is enabled and an administrative interface appears when accessing the instance.
  2. Db: database type of the provider configuration program. Current program support: SQLite, mysql, SQLServer three kinds of databases.
  3. Db :conn Configures the database connection string

Enter the system

Visit us in your browser at http://localhost:8011/

image.png

The interface is simple and beautiful. After the first login, you need to initialize the administrator password and log in to the system

image.png

Through the main interface we see the following several menus

Nodes: AgileConfig supports multi-node deployment, where all nodes are parallel. To simplify deployment, AgileConfig does not have a separate console program, so use any node as the console.

image.png

Application: AgileConfig supports multiple application access. You need to configure the name, ID, secret key, and so on for each application. Each application can set whether or not it can be inherited, which is similar to Apollo’s concept of a common namespace. A common configuration can be extracted from an inheritable application, and other applications can obtain all configurations simply by inheriting it. If the configuration keys of the child application and the inherited application duplicate, the configuration of the child application overwrites that of the inherited application. Subapplications can inherit multiple applications. If duplicate keys occur between multiple applications, the configuration of the inherited application overrides the previous application.

image.png

Once you’ve created the application you can click on the list of Settings for that application.

image.png

The newly added configuration is not detected by the client. You need to manually click Online to push the configuration to the client.

image.png

If online configurations are modified, deleted, or rolled back, they are pushed to the client in real time. Version history records the historical information about configurations and can be rolled back to any version.

image.png

Client: You can view connected clients

Log: Logs that record key information

Creating a Client

Create one using VS2019. Net5 WebAPI application, install components

  <ItemGroup>
    <PackageReference Include="AgileConfig.Client" Version="1.1.8.5" />
  </ItemGroup>
Copy the code

Connect to our configuration center and configure in program

    public class Program
    {
        public static IConfigClient ConfigClient;

        public static void Main(string[] args)
 {  CreateHostBuilder(args).Build().Run();  }   public static IHostBuilder CreateHostBuilder(string[] args) =>  Host.CreateDefaultBuilder(args)  .ConfigureAppConfiguration((context, config) =>  { // Read the local configuration var localconfig = new ConfigurationBuilder()  .SetBasePath(Directory.GetCurrentDirectory())  .AddJsonFile("appsettings.json").Build(); // Read AgileConfig information from the local configuration var appId = localconfig["AgileConfig:appId"];  var secret = localconfig["AgileConfig:secret"];  var nodes = localconfig["AgileConfig:nodes"];  // New a client instance var configClient = new ConfigClient(appId, secret, nodes); // Configure a new IConfigurationSource using AddAgileConfig config.AddAgileConfig(configClient); // Find a variable to mount the client instance so that other places can access the configuration directly using the instance ConfigClient = configClient; // Register a configuration item change event configClient.ConfigChanged += ConfigClient_ConfigChanged;  })  .ConfigureWebHostDefaults(webBuilder =>  {  webBuilder.UseStartup<Startup>();  });   /// <summary> // This event is triggered when a configuration item is added, modified, or deleted /// </summary>  private static void ConfigClient_ConfigChanged(ConfigChangedArg obj)  {  Console.WriteLine($"action:{obj.Action} key:{obj.Key}");   switch (obj.Action)  {  case ActionConst.Add:  break;   case ActionConst.Update:  break;   case ActionConst.Remove:  break;   default:  break;  }  }  } Copy the code

Appsettings add

  "AgileConfig": {
    "appId": "001".    "secret": "454551215781234"/ / key    "nodes": "http://localhost:8011"// Separate multiple nodes with commas  }
Copy the code

Reading configuration

AgileConfig supports asp.net core standard IConfiguration and reads configuration with IOptions mode. It also supports reading directly from the AgileConfigClient instance. This article directly injects an IConfiguration to get the database connection information we just configured.

Obtain the configuration in startup

image.png

Add an interface to get configuration information

    private readonly IConfiguration _configuration;

    public HomeController(IConfiguration configuration)
    {
        _configuration = configuration;
 }   [HttpGet]  public string Get(a) {  return _configuration["db:ConnectionString"];  } Copy the code

Access interface output configuration

Server=localhost; Database=test; Port=3306; charset=utf8; uid=root; pwd=123456;

Modify the configuration without closing the program to test whether the configuration is updated

image.png

Request the interface to retrieve the latest configuration (not real-time update, need to wait a while, but meet our actual needs)

The resources

Development of the author’s article: https://www.cnblogs.com/kklldog/p/agile-config.html

Making Chinese document: https://github.com/kklldog/AgileConfig/blob/master/README_CN.md

This article is formatted using MDNICE