This article is a mirror article of machine translation, please click here to jump to the original article.

View: 28399|Reply: 1

[Source] Architecture: ASP.NET Core dynamically configures hot updates based on Consul

[Copy link]
Posted on 10/16/2020 6:25:34 PM | | | |
ASP.NET Core implements microservices, you must address the problem of the configuration center. Whether it is a microservice or not, it is very troublesome to change the configuration file one by one with more nodes, and today I will share a lightweight configuration center solution based on Consul. Hot updates to profiles are achieved without re-siteing.

background

Typically, configurations in .NET applications are stored in configuration files, such as App.config, Web.config, or appsettings.json. Starting with ASP.Net Core, a new extensible configuration framework emerged that allowed configurations to be stored outside of configuration files and retrieved from the command line, environment variables, and more.

In traditional projects, modifying configuration files requires logging in to the production environment for modification, which is not convenient for maintenance and management when there are many projects.

Therefore, we use Consul to configure online in real time, which achieves the goal of responding in real time by only changing the configuration without restarting the service. The implementation idea is based on the key/value of Consul, and multiple Consul nodes can be automatically synchronized and configured, and our program is to implement ConfigurationProvider and listen for Consul changes.

Consul configuration

For download and installation, you can refer to:

[Architecture]. net/c# implements distributed configuration of Key/Value store based on Consul
https://www.itsvse.com/thread-9420-1-1.html

ASP.NET Core configuration

First, install the nuget package with the following command:



Modify the Program.cs file with the following code:



For more information about Configuration, please refer to:

The hyperlink login is visible.

Configure the Consul service center address and modify the appsettings.json file as follows:



The controller code is as follows:



Start the project as shown below:





Since we at Consul are not configuring the value of test, the fetch is empty.

Open itThe hyperlink login is visible.Configure the files required for the project.

The profile address read by the project is:

“/WebApplication1/appsettings. Development.json”

Therefore, we need to create a new "WebApplication1" folder and "appsettings. Development.json" file as shown below:



The value is as follows:



After saving, we refresh the website as shown below:



Tested Chinese characters without any problems, as shown below:



It can be seen that the value of test is successfully obtained, which realizes the dynamic configuration function, and the configuration file is hot-updated without restarting the website.




Previous:[Architecture]. net/c# implements distributed configuration of Key/Value store based on Consul
Next:【Architecture】ASP.NET Core is highly available based on Consul services
Posted on 1/5/2022 2:42:56 PM |
Learn to learn
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com