.net core使用nacos作為配置中心


拉取鏡像並創建容器

docker pull nacos/nacos-server
docker run --env MODE=standalone --name nacos -d -p 8848:8848 nacos/nacos-server

創建配置

image-20210611201316776

core里寫代碼讀取配置

安裝包 nacos-sdk-csharp-unofficial


//ConfigureServices

 services.AddNacos(configure =>
                   {
                       // default timeout
                       configure.DefaultTimeOut = 8;
                       // nacos's endpoint
                       configure.ServerAddresses = new System.Collections.Generic.List<string> { "http://192.168.114.131:8848" };
                       // namespace
                       configure.Namespace = "";
                       // listen interval
                       configure.ListenInterval = 1000;
                   });




//使用的地方可以注入
//        private readonly INacosConfigClient _configClient;

[HttpGet]
public async Task<string> Get()
{
    var res = await _configClient.GetConfigAsync(new GetConfigRequest
                                                 {
                                                     DataId = "redis",
                                                     Group = "DEFAULT_GROUP",
                                                     Tenant = "Public"
                                                 });

    return string.IsNullOrWhiteSpace(res) ? "Not Found:" + await _configClient.GetServerStatus() : res;

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM