Agileconfig lightweight configuration center 1.1.0 is released to support inter application configuration inheritance

Time:2022-5-11

It has been half a year since the first version of agileconfig lightweight configuration center was released. Without any promotion, I received 250 stars, which was very encouraging to me, and many students tried it and gave valuable opinions. Thank them very much. Some of them are very good, but they have not been developed. First, I’m busy (lazy) in the second half of the year. Second, I don’t want to make agileconfig too complicated. However, one of the requirements mentioned by many students is to support inheritance (Association) between applications, similar to the concept of Apollo’s public namespace. For example, there are many public configuration items between microservice applications, which can be configured in one application, and then other applications inherit it, so that each application does not need to configure the public configuration repeatedly. After thinking about it, this configuration is really a very useful function, so I spent some time implementing it.
GitHub address:https://github.com/kklldog/AgileConfigPlease, star.
The following example briefly demonstrates how to use agileconfig to read the configuration and use the inheritance function

Start an agileconfig instance using 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 [email protected],; Initial Catalog =agile_config_test; Server =." -p 5000:5000 kklldog/agile_config:latest

Using the docker command to run an agileconfig instance is the simplest way. Of course, you can also pull down the source code, compile and publish it, and use IIS to run it.
Configure environment variables:
Adminconsole = true enable console functions
DB: provider = SQLSERVER database is sqlserver
db:conn=”Persist Security Info = False; User ID =dev; Password = [email protected] ,; Initial Catalog =agile_ config_ test; Server =. “Configure database connections
-P 5000: 5000 ports of 5000 containers are mapped to 5000 ports locally

Configuring agileconfig

You need to configure the management password for the first run
Agileconfig lightweight configuration center 1.1.0 is released to support inter application configuration inheritance
After the password configuration is completed, log in to the system again and start configuring the node
Agileconfig lightweight configuration center 1.1.0 is released to support inter application configuration inheritance
In the design, the node and the console are separated, but in order to deploy simply, the node and the console are implemented together. Therefore, when using single node deployment, the instance is both a node and a console, so you also need to add the address of this node to the node list so that the console can manage it.

Add app

The initialization of agileconfig is complete. Now let’s start adding applications.
Add “public app”
Agileconfig lightweight configuration center 1.1.0 is released to support inter application configuration inheritance
Add application name and application ID, and check “inheritable”. Click OK to complete the creation of public. The system only supports one layer of inheritance. Applications that can be inherited cannot inherit other applications.
Add configuration items for public applications after creation
Agileconfig lightweight configuration center 1.1.0 is released to support inter application configuration inheritance
Add a configuration item for the public application: the key is public_ key_ The value of 01 is 0001.

Add “private app”
Agileconfig lightweight configuration center 1.1.0 is released to support inter application configuration inheritance
Add a private application without selecting “inheritable”. Click the plus sign in the inheritance application column to pop up a list of applications that can be inherited, and select “public application”. Click “OK” to finish the creation.

Create configuration items for private applications
Agileconfig lightweight configuration center 1.1.0 is released to support inter application configuration inheritance
Add a configuration item for the private application: the key is private_ key_ The value of 01 is 0002.

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

Client read configuration

Create ASP Net core webapi project

We create a webapi project as a client to demonstrate how to read the configuration
Agileconfig lightweight configuration center 1.1.0 is released to support inter application configuration inheritance

Use nuget to reference agileconfig Client

Install-Package AgileConfig.Client -Version 1.1.0

Integrate agileconfig Client

After the nuget installation is successful, switch to program CS 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";
                //New a client instance
                var configClient = new ConfigClient(appId, secret, nodes);
                //Use addagileconfig to configure a new iconfigurationsource
                cfg.AddAgileConfig(configClient);
            })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

Use the configureappconfiguration of ihostbuilder to inject our configclient. Here, our configclient configures the ID of the private application: private_ 01 。

Read configuration

The preliminary work has been completed. Now we can 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}";
        }
    }

Inject iconfiguration through the constructor, then directly read the public configuration and private configuration through it, and directly return the string.

Note to modify the startup port of the client program. The default 5000 conflicts with the port occupied by the agileconfig instance above.

Run it

Run the client project and enter it in the browserhttp://localhost:51605/readconfig
Agileconfig lightweight configuration center 1.1.0 is released to support inter application configuration inheritance
You can see that our public configuration and private configuration are accurately read.

summary

Through the above simple example, it demonstrates how to use agileconfig to read the configuration and how to inherit the configuration between applications. The above example does not show all the contents. When using inheritance, you should pay attention to the following points:

  1. When the configuration of the private application is repeated with the inherited application, the configuration of the private application will overwrite the configuration of the inherited application
  2. If an application is marked as “inheritable”, the application itself cannot continue to inherit other applications
  3. A private application can inherit multiple “inheritable” applications. If there are duplicate configurations among multiple inherited applications, the order of inheritance will be followed, and the subsequent applications will overwrite the previous applications.

If you like this project, please give me star. thank you.
GitHub address:[https://github.com/kklldog/Ag…]

Recommended Today

Shardingsphere JDBC entry practice

preface Apache shardingsphere is an ecosystem composed of a set of open source distributed database solutions. It is composed of JDBC, proxy and sidecar (under planning), which can be deployed independently and support mixed deployment; The next few articles will focus on shardingsphere JDBC, which will be introduced from the aspects of data fragmentation, distributed […]