. Net Core&Agile Config configuration center

Posted by kurtsu on Thu, 30 Dec 2021 18:13:49 +0100

As the number of services increases, the configuration management of each service becomes more and more important. The lightweight configuration center is much simpler to start or build, based on The lightweight configuration center AgileConfig developed by. net core is powerful and easy to use.

https://github.com/dotnetcore/AgileConfig

AgileConfig architecture diagram

AgileConfig configuration side setup

1. Create a new folder for configuration storage

mkdir agileconfig

2. Newly opened Database & configure account name
3. Download Image & create container

AgileConfig supports five databases: sqlserver, mysql, sqlite, PostgreSql and Oracle. Select according to the actual database type. I use MySQL here.

sudo docker run \
 --name StarCityAgileConfig \
 -e TZ=Asia/Shanghai \
 -e adminConsole=true \
 -e db:provider=mysql \
 -e db:conn="Server=xxx; Database=xxx;Port=xxx;charset=utf8;uid=xxx;pwd=xxx;" \
 -p 9527:5000 \
 -v /agileconfig:/app/db \
 -d \
 kklldog/agile_config:latest
  • Name: container name, given a container name.
  • TZ: Specifies the time zone.
  • adminConsole: configures whether the program uses the administrative console.
    • If true, the console function will be enabled, and the management interface will appear when accessing the instance.
    • Each instance can choose to use the management interface and share a set of data sources, but the rendering ports are different
    • The default account is admin. The password needs to be set for the first login. After setting, multiple management interfaces can be common
  • db:provider: the database type of the configurator.
    • At present, the program supports five databases: sqlite, mysql, sqlserver, npgsql and Oracle.
    • Use the database allowed in the project.
    • After the first node starts, a data table will be created (quite good ~).
    • db:env:{env}:provider. You can specify a database to use in a specific environment, such as db:env:PROD.provider=sqlserver, DB: env: development provider=mysql
  • db:conn: configure database connection string.
    • Set different database connection strings according to different database types.
    • The database uses the library created in step 2.
    • By default, four common environments, dev, test, staging and prod, are built in. If not enough, you can directly operate agc_setting table to add a custom environment.
    • db:env:{env}:conn, you can specify a database to be used in a specific environment, such as db:env:PROD.conn=xxx, DB: env: development conn=xxx
  • p: Specify the external port for the user to connect to the client.
    • Set the allowed external port.
  • v: Data volume mount for node
    • Mount here to the folder path set in step 1. You can set the mounting path according to actual needs or do not set the - v parameter.
  • -d: Background operation

4. Open the address and configure the password of the default account admin

5. Node management

After the first node enters the management interface, it can add itself to the node list.

Nodes can be added to the node list, so that all nodes can be managed uniformly.

Client settings

1. Add Nuget package to the project requiring management configuration to access the AgileConfig configuration side

Install-Package AgileConfig.Client

2. Register the client information on the AgileConfig configuration side

3. At Appsettings JSON adds nodes connected to AgileConfig, fills the ID and key in the second step into the configuration, and sets the connected configuration end node. You can set multiple nodes and randomly connect one node.

{
  "AgileConfig": {
    "appId": "app",
    "secret": "xxx",
    "nodes": "http://localhost:9527,http://localhost:9528 "/ / multiple nodes are separated by commas,
    "name": "client_name",
    "tag": "tag1",
    "env": "DEV"
  }
}

When a node is unavailable, the client will switch to other nodes. If all nodes are unavailable, use the memory cache configuration or read the local file cache configuration.

4. Use AgileConfig in Program settings. After adding an environment in this way, AgileConfigProvider will start from the corresponding environment Appsettings JSON to read the above configuration

.ConfigureAppConfiguration((context, config) =>
{
    var envName = context.HostingEnvironment.EnvironmentName;
    var configClient = new ConfigClient($"appsettings.{envName}.json");
    config.AddAgileConfig(configClient, arg => Console.WriteLine($"config changed , action:{arg.Action} key:{arg.Key}"));
})

Adding services in Startup

services.AddAgileConfig();

5. Set the code that needs to read the configuration. Start the client according to the way AspNetCore reads the configuration

[Route("api/[controller]")]
[ApiController]
public class HealthController : ControllerBase
{
    private readonly ILogger<HealthController> _logger;
    private readonly IConfiguration _configuration;

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

    [HttpGet]
    [Route("Index")]
    public IActionResult Index()
    {
        _logger.LogWarning($"This is HealthController {_configuration["Port"]}.");
        return Ok();
    }
}

6. AgileConfig configures the client configuration. It can be set according to the json format, create or edit the existing configuration, or import json files or data. Click publish to distribute it to each connected client.

After publishing, the re request is the latest configuration information. As a hot load provided by the configuration center, it provides more flexibility.

7. There are also many functions on the configuration side, such as client management, client connection viewing, configuration item history, version rollback, etc. the system log is convenient to track multi terminal connection problems, as well as user management on the configuration side.

Configure read priority

If there is one in AgileConfig, the default value is taken from there. Instead of looking for confidential files, go to Appsettings {env}. JSON, and finally Appsettings JSON, of course, the highest priority is the configuration of environment variables and command line.

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0#default-configuration

As Net, which I like very much. It is very convenient to build and understand, powerful and easy to operate. At the same time, it is also very easy to deploy, with less resources and beautiful skin (compared with Apollo).

AgileConfig boss address: https://www.cnblogs.com/kklldog/p/agile-config.html
AgileConfig address: https://github.com/dotnetcore/AgileConfig

On December 27, 2021, I hope you can come back and see your steps after your technology is successful

Topics: .NET