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

 

_config["timeout"]; 是读取Apollo 中的配置.

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM