『AgileConfig』.NET lightweight configuration center AgileConfig

description

The lightweight configuration center developed based on NetCore is simple to deploy, simple to configure, simple to use, and can be adopted according to individual or company needs.

  • Deployment short answer, at least only one data node is required to support docker deployment
  • Support multi-node distributed deployment to ensure high availability
  • Configuration support according to application isolation, in-app configuration supports group isolation
  • Using long link technology, configuration information is pushed to the client in real time
  • Support IConfiguration, IOptions mode to read configuration, almost no need to modify the original program
  • Configuration modification supports version records, and configuration can be rolled back at any time
  • All nodes fail, and the client supports reading the configuration from the local cache
GitHub address: https://github.com/kklldog/AgileConfig You can give this big guy a star

Architecture

Insert picture description here


AgileConfigThe structure of is relatively simple, mainly divided into 3 parts:

Client

The client program is a class library developed using netstandard2.0, which is convenient for .net core program access, and can be installed by searching agileconfig.client on nuget. You can configure the addresses of multiple nodes when you start the client. The client will randomly choose one to connect to. After the connection is successful, it will maintain a websocket long connection. If the connected node fails and the connection is interrupted, the client will continue to connect to a random node until the connection is successful.

Node, management program

Node is a service developed using asp.net core. In order to simplify the deployment, the management program and the node service are directly combined into one. Any node can configure environment variables to enable the management program function at startup.

database

Use a database to store data, currently supports Sqlserver, Mysql, Sqlite three databases. Because the server uses the EF Core framework to access data, in principle, as long as the database supported by EF Core, the node can easily support it.

Deploy the server

Through docker deployment, it currently supports five databases: sqlserver, mysql, sqlite, PostgreSql, and Oracle. This example uses lightweight sqlite as data storage

sudo docker run --name agile_config -e adminConsole=true -e db:provider=sqlite -e db:conn="Data Source=agile_config.db" -p 5000:5000 kklldog/agile_config:latest
  1. Create an agile_config instance through docker, in which there are 3 environment variables that need to be configured:
  2. adminConsole configures whether the program is an administrative console. If true, the console function is enabled, and the management interface will appear when accessing the instance.
    db:provider configures the database type of the program. Currently the program supports three databases: sqlite, mysql, and sqlserver.
  3. db:conn configuration database connection string

enter the system

Visit our address through a browser:http://localhost:5000/

Insert picture description here

The interface is still simple and beautiful, the first time you log in, you need to initialize the administrator password, and then log in to enter the system

Insert picture description here


Through the main interface, we see the following menus

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

Insert picture description here


Application : AgileConfig supports multi-application access. Need to configure the name, ID, secret key and other information for each application. Each application can be set whether it can be inherited, and applications that can be inherited are similar to the concept of apollo's public namespace. The public configuration can be extracted into the inheritable application, and other applications can get all the configuration as long as it inherits it. If the configuration key between the sub-application and the inherited application is duplicated, the configuration of the sub-application will overwrite the configuration of the inherited application. Sub-applications can inherit multiple applications. If duplicate keys occur between multiple applications, in the order of inheritance, the configuration of the later inherited application overwrites the previous application.

Insert picture description here


After the application is created, we can click on the list to configure the configuration items of the application.

Insert picture description here


Insert picture description here


The newly added configuration will not be perceived by the client, and it will be pushed to the client after manually clicking "Go Online".

Insert picture description here


If the online configuration is modified, deleted, or rolled back, it will be pushed to the client in real time. The version history records the historical information of the configuration and can be rolled back to any version.

Insert picture description here


Client : You can view the connected clients

Log : A log that records some key information

Create a client

Install-Package AgileConfig.Client

Connect to our configuration center and configure in the 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) =>
                {
                    //读取本地配置
                    var localconfig = new ConfigurationBuilder()
                                     .SetBasePath(Directory.GetCurrentDirectory())
                                     .AddJsonFile("appsettings.json").Build();
                    //从本地配置里读取AgileConfig的相关信息
                    var appId = localconfig["AgileConfig:appId"];
                    var secret = localconfig["AgileConfig:secret"];
                    var nodes = localconfig["AgileConfig:nodes"];

                    //new一个client实例
                    var configClient = new ConfigClient(appId, secret, nodes);
                    //使用AddAgileConfig配置一个新的IConfigurationSource
                    config.AddAgileConfig(configClient);
                    //找一个变量挂载client实例,以便其他地方可以直接使用实例访问配置
                    ConfigClient = configClient;
                    //注册配置项修改事件
                    configClient.ConfigChanged += ConfigClient_ConfigChanged;
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup();
                });

        /// 

        /// 此事件会在配置项目发生新增、修改、删除的时候触发
        /// 
        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;
            }
        }
    }

appsettings add

 "AgileConfig": {
    "appId": "test",
    "secret": "123",//密钥
    "nodes": "http://localhost:5000" //多个节点使用逗号分隔
  }