.NET6 .NET CORE 使用Apollo

Apollo默认有一个“SampleApp”应用,“DEV”环境 和 “timeout” KEY。

 

nuget 中下载 “Com.Ctrip.Framework.Apollo.Configuration”。

 

1.修改appsettings.json

增加:

"apollo": {
"AppId": "SampleApp",
"Env": "DEV",
"MetaServer": "http://192.168.2.133:8080"
}

 

2.修改Program.cs

2.1 增加:

IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();

 

2.2 增加:

// 添加Apollo配置中心
builder.Host.ConfigureAppConfiguration((context, b) =>
{
b.AddApollo(configuration.GetSection("apollo"))
.AddDefault();
});

或者直接:

// 添加Apollo配置中心
builder.Host.ConfigureAppConfiguration((context, b) =>
{
    b.AddApollo(b.Build().GetSection("apollo"))
    //b.AddApollo(configuration.GetSection("apollo"))
     .AddDefault();
});

 

 不用写:IConfiguration configuration = new ConfigurationBuilder() 这一段。

 

 

3.修改 Controller

private readonly ILogger<HomeController> _logger;

        IConfiguration _config;

        public HomeController(ILogger<HomeController> logger, IConfiguration config)
        {
            _logger = logger;
            _config = config;
        }

        public IActionResult Index()
        {           
            string AppVer = _config["timeout"];
            ViewBag.AppVer = AppVer;
            return View();
        }

.

上一篇:Python - 类(class)的构造器(__init__) 详解 及 代码


下一篇:apollo客户端实现原理