It has been six months since the first release of the AgileConfig Lightweight Configuration Center. I have received 250 stars without any promotion, which is very encouraging to me. Besides, many students tried it out and gave their valuable opinions. Thank you very much. Some of these ideas are very good, but have never been developed. Mainly because the second half of the year is busy (lazy), and I don’t want to make AgileConfig too complicated. But one requirement that was mentioned by many students was to support inheritance (association) between applications, similar to the concept of common namespace at Apollo. For example, there are many common configuration items among microservice applications, which can be configured in one application and then inherited by other applications, so that each application does not have to configure the common configuration repeatedly. I thought about this configuration and decided it was a really useful feature, so I spent some time implementing it. Github address: github.com/kklldog/Agi… Strives for the star. The following example briefly demonstrates how to use AgileConfig to read the configuration and use inheritance

Start an AgileConfig instance with Docker

sudo docker run --name agile_config -e adminConsole=true -e db:provider=sqlserver -e db:conn="Persist Security Info = False; User ID =dev; Password =dev@123,; Initial Catalog =agile_config_test; Server =." -p 5000:5000 kklldog/agile_config:latest

Copy the code

The easiest way is to run an AgileConfig instance using the Docker command. Of course, you can pull the source code down and compile and distribute it using IIS to run it. Configure environment variables: adminConsole=true Enable console functions DB :provider= SQLServer Database: SQLServer DB :conn=”Persist Security Info = False; User ID =dev; Password =dev@123,; Initial Catalog =agile_config_test; -p 55:5000 Map port 5000 of the container to port 5000 of the local database

Configuration AgileConfig

The management password needs to be configured for the first run

After the password is configured, log in to the system again and configure the node



The node was designed separately from the console, but was eventually implemented together for ease of deployment. Therefore, in single-node deployment, the instance is both a node and a console, so the address of the local node needs to be added to the node list so that the console can manage it.

Add application

Now that the AgileConfig initialization is complete, we are ready to add the application.

Adding Public Applications



Add the application name, application ID, and select Inheritable. Click OK to complete the public creation. The system supports only one layer of inheritance. Inheritable applications cannot inherit other applications.

Add configuration items for common applications



Add a configuration item for public applications. The key is public_key_01 and the value is 0001.

Adding Private ApplicationsTo add a private application, do not check inheritable. Click the plus sign in the Inherited Applications bar to bring up a list of applications that can be inherited. Select “Public Applications”. Click OK to complete the creation.

Create a configuration item for a private application



Add a configuration item for private applications: the key is private_KEY_01 and the value is 0002.

Note: Put all configurations online, otherwise the client cannot read the configuration.

The client reads the configuration

Create an Asp.net Core WebApi project

Let’s create a WebApi project as a client to show how to read the configuration

Use nuget to reference AgileConfig.Client

Install - Package AgileConfig. Client - Version 1.1.0Copy the code

Integrated AgileConfig. Client

After a successful installation using Nuget, switch to Program.cs and start integrating AgileConfigClient.

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((ctx,cfg)=> { var appId = "private_01"; var secret = ""; var nodes = "http://localhost:5000"; Var configClient = new configClient (appId, secret, Nodes); // Use AddAgileConfig to configure a new IConfigurationSource cfg.AddAgileConfig(configClient); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });Copy the code

Using IHostBuilder ConfigureAppConfiguration our ConfigClient injected into it. Here our ConfigClient configures the private application ID: private_01.

Reading configuration

With all the preliminary work done, we can now start writing the code to read the configuration. Create a new ReadConfigController:

[ApiController] [Route("[controller]")] public class ReadConfigController : ControllerBase { private readonly IConfiguration _IConfiguration; public ReadConfigController(IConfiguration configuration) { _IConfiguration = configuration; } [HttpGet] public String Get() { var publicConfig = _IConfiguration["public_key_01"]; var privateConfig = _IConfiguration["private_key_01"]; return $"publicConfig:{publicConfig} , privateConfig:{privateConfig}"; }}Copy the code

An IConfiguration is injected via the constructor, which then reads the public configuration directly, the private configuration directly, and returns the string directly.

The default port 5000 conflicts with the port occupied by the AgileConfig instance.

Run the

Run the client project and type in the browserhttp://localhost:51605/readconfig



You can see that both our public and private configurations are correctly read.

conclusion

With the simple example above, I demonstrated how to use AgileConfig to read configurations and inherit configurations from application to application. The examples above don’t show everything, but there are a few things to be aware of when using inheritance:

  1. If the configuration of the private application is the same as that of the inherited application, the configuration of the private application overwrites that of the inherited application
  2. If an application is marked “inheritable”, the application itself cannot inherit from other applications
  3. A private application can inherit multiple inheritable applications. If repeated configurations occur among the inherited applications, the subsequent applications override the previous applications in the inherited sequence.

If you like this project, please give me star. thank you