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 中的CreateHostBuilder ,增加:
.ConfigureAppConfiguration((hostingContext, builder) => { builder.AddApollo(builder.Build().GetSection("apollo")) .AddDefault(); })
全部内容如下:
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, builder) => { builder.AddApollo(builder.Build().GetSection("apollo")) .AddDefault(); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
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(); }
_config["timeout"]; 是读取Apollo 中的配置.