This is a demo of a configuration center solution for microservices based on Nacos.
Nacos is open source. At the same time, Alibaba Cloud also has fee-based services about it. The company happens to rely on Alibaba Cloud's service system, so it is still very likely to use it as a configuration center. Therefore, based on it, an example is here.
1. How to build the environment
Its environment is relatively complex, requiring Docker services and test demo services, and it also requires a corresponding Mysql database:
- Docker provides Nacos services
- WebDemo
- Specified database required by Mysql
2. Get the table structure of the official website
The official example address for C#is: www.example.com
The official address is here: https://www.example.com
SQL will be found in nacos\distribution\conf\nacos-mysql.sql
My project will provide the required sql.

I will insert the specified script here, and it will be OK, provided that you have this library.
Finally, I saw the following tables

3. Launch Docker service
I use Docker Desktop by default, and I can do it directly by typing in the commands.
If you also use this Docker, then you can refer to my previous article on Docker.
docker run --name nacos -d -p 8848:8848 ^
-e MODE=standalone ^
-e MYSQL_SERVICE_HOST=192.168.1.8 ^
-e MYSQL_SERVICE_DB_NAME=nacos_config ^
-e MYSQL_SERVICE_PORT=3306 ^
-e MYSQL_SERVICE_USER=root ^
-e MYSQL_SERVICE_PASSWORD=123456 ^
nacos/nacos-server
How to determine whether the service is OK
You can visit the http://localhost:8848/nacos/#/login address on your browser

That way, we can log on to the platform and see what we have.
The initial username and password are both ** nacos **
4. Add corresponding configuration information

- The first is the configuration menu we want to add
- The second is the corresponding namespace
- The third is the specific configuration required
Among them, I added a new test namespace

Then, two configurations were added to each as follows


5. Create a new WebAPi project
Create a new default webapi project and introduce the following nuget package
nacos-sdk-csharp.AspNetCore
In addition, you need to modify the default Program. This place is configured as follows
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureAppConfiguration((context, builder) =>
{
var c = builder.Build();
// read configuration from config files
// it will use default json parser to parse the configuration store in nacos server.
builder.AddNacosV2Configuration(c.GetSection("NacosConfig"));
// you also can specify ini or yaml parser as well.
// builder.AddNacosV2Configuration(c.GetSection("NacosConfig"), Nacos.IniParser.IniConfigurationStringParser.Instance);
// builder.AddNacosV2Configuration(c.GetSection("NacosConfig"), Nacos.YamlParser.YamlConfigurationStringParser.Instance);
});
The other thing to note is that the most important configuration files (appsettings.json) are as follows:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"NacosConfig": {
"Listeners": [
{
"Optional": false,
"DataId": "conn",
"Group": "DEFAULT_GROUP"
},
{
"Optional": false,
"DataId": "other",
"Group": "DEFAULT_GROUP"
}
],
"Tenant": "1806893a-7997-4657-9325-d4294fbf0f4a",
"ServerAddresses": ["http://192.168.1.8:8848/"],
"UserName": "nacos",
"Password": "nacos",
"ConfigUseRpc": false,
"NamingUseRpc": false
}
}
Tenant is the ID of the specified configuration center namespace, and Listeners is the Data Id of the configuration under this namespace.
There must be two parameters ConfigUseRpc and NamingUseRpc. If the http protocol is used, both are false, and if the grpc protocol is used, they are true. (If you don't write, you will report an error)
To increase the demonstration effect, I have modified the default controller method here to read the specified configuration
private readonly IConfiguration _configuration;
public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
public IActionResult Index(string key)
{
if (string.IsNullOrWhiteSpace(key))
{
return Content("key is empty!");
}
return Content(_configuration[key]);
}
Post-startup effect
Visit http://localhost:38889/home? key = mysql address is as follows

Visit http://localhost:38889/home? key = other address is as follows

Visit http://localhost:38889/home? key = redis address as follows

It can be seen that all the corresponding configuration information can be found
At this time, I dynamically modify the configuration information on Nacos

Then, check again to see if it is the latest one
The discovery is already up to date, and the console is also directly updated with the latest configuration


It can be seen that this configuration center is also very useful
6. Finally, give me the github address
- github :https://github.com/kesshei/NacosConfigDemo.git
- gitee : https://gitee.com/kesshei/NacosConfigDemo.git