其他分享
首页 > 其他分享> > .NET6 .NET CORE 使用Apollo

.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();
        }

.

标签:configuration,apollo,IConfiguration,NET6,NET,Apollo,config,logger
来源: https://www.cnblogs.com/runliuv/p/15912766.html