Initial experience of. NET open source configuration component AgileConfig

Posted by xtopolis on Fri, 04 Mar 2022 21:38:06 +0100

Turn:

Initial experience of. NET open source configuration component AgileConfig

introduce

Today, with the popularity of microservices, the system will be divided into multiple modules and run as separate services. At the same time, in order to centralize management, we also need log center, configuration center, etc. many developers may be more familiar with Apollo config. The function of this component is also very perfect. The clients of dotnet are mainly maintained and developed by Chinese people, In the past, the company selected Apollo and operated stably in the production environment, but the server is Java, and there is some trouble in deploying the server.

Today, we mainly introduce AgileConfig, which is based on The lightweight configuration center developed by. net core has also communicated with the author before and is very enthusiastic to help solve problems. It is characterized by simple deployment, simple configuration, simple use and simple learning. At the same time, it also supports high availability (multi node) and docker deployment. It uses FreeSql and supports a variety of database storage, sqlserver, mysql, SQLite, PostgreSQL and Oracle, FreeSql method is good!

Architecture diagram

Deployment server

First of all, we need to deploy the server, including the console UI and node services. The node services use a long connection. After the configuration changes, it will be pushed to the client in real time. docker deployment is recommended for deployment, because the project is open source and net core. Another way is to download the source code from github, compile it, release it and run it.

The configuration information is recorded in the database. We need to create an empty database, and then run the command. The program will automatically help us initialize the table structure and data.

docker run --name agile_config -e adminConsole=true -e db:provider=mysql -e db:conn="DataBase=configdb;Data Source=host.docker.internal;User Id=root;Password=123456;" -p 5000:5000 -d kklldog/agile_config:latest

Parameter introduction:

  • Whether the adminConsole configurator is a management console. If it is true, the console function will be enabled, and the management interface will appear when accessing the instance. For multi node deployment, only one console needs to be opened.
  • DB: the database type of the provider configurator. Options include sqlserver, mysql, sqlite, npgsql and oracle.
  • db:conn configuration database connection string

visit http://localhost:5000/ , the first time we start, the program will ask us to initialize the administrator password. After setting, we will enter the home page. In addition, the author has recently reconstructed the UI with React and will update it recently. You can also try it.

Then you need to configure nodes. Multiple nodes ensure high availability. If all nodes hang up, the program will read the local configuration cache to ensure normal operation. You don't have to worry about this. We need to manually add nodes on the node management page. Here I started a node with the address http://192.168.100.103:5000 Note that localhost cannot be used in the docker environment.

Next, you need to configure the application, click Add application, and fill in the application name, application Id and application key.

Use in client programs

The asp net core web project example is used here. First, you need to install the client components, execute the command or install through Nuget.

Install-Package AgileConfig.Client

Then modify appsetting JSON file

{ 
  "AgileConfig": {
    "appId": "LogService",
    "secret": "123456",
    "nodes": "http://localhost:5000,http://localhost:5001 "/ / multiple nodes are separated by commas
  }
}

Then modify the program cs

 public static IHostBuilder CreateHostBuilder(string[] args) =>
       Host.CreateDefaultBuilder(args)
       .ConfigureAppConfiguration((context, config) =>
        {
           var configClient = new ConfigClient();
           config.AddAgileConfig(configClient);
       })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup
  
  
   
   ();
        });

  
  

After the preparation work is completed, we will start to use it. We will add the configuration on the configuration item page, and then select it and click go online.

Then you can use the injected IConfiguration in the code to obtain the configuration

 [Route("[controller]/[action]")]
    public class HomeController : ControllerBase
    {    
        private readonly IConfiguration _config;

        public HomeController(IConfiguration config)
        {
            _config = config; 
        }

        [HttpGet]
        public IActionResult Index()
        {
            var value = _config["AgileKey"];

            return Ok(new { value });
        }

Then start the program and you can enjoy using AgileConfig. If you modify the configuration on the page, our client configuration is also modified in real time.

The configurations on the configuration page are string key value pairs. What should we do with the Json string? We can use tuhu Extensions. Configuration. ValueBinder. Json extension can be installed through Nuget, and then modify startup CS file

public void ConfigureServices(IServiceCollection services)
{ 
      services.ConfigureJsonValue
  
  
   
   ("", Configuration.GetSection("LogOptions"));  
}

  
  

LogOptions:

public class LogOptions : IOptions
  
  
   
   
{
        public string Level { get; set; }

        public int Count { get; set; }

        public LogOptions Value => this;
}

  
  

In this way, we can use the injected IOptions in the code To get the read configuration.

summary

AgileConfig uses The configuration components developed by. net core are simple to deploy and use, but there are still some deficiencies, such as multi account permission management and multi environment support. They are generally developed, grayscale and formal, but it doesn't matter. The projects are open source. We are interested in building together to improve the insufficient functions. Now NET community is getting better and better. If it is helpful to you, you can support it!

https://github.com/kklldog/AgileConfig

Turn:

Initial experience of. NET open source configuration component AgileConfig


--Posted from Rpc