asp.net core web api 使用apollo配置更改回调监听

  1. 安装依赖包
bash 复制代码
   > Com.Ctrip.Framework.Apollo                         2.10.0   2.10.0
   > Com.Ctrip.Framework.Apollo.ConfigAdapter.Yaml      2.9.0    2.9.0 
   > Com.Ctrip.Framework.Apollo.Configuration           2.10.2   2.10.2
   > Com.Ctrip.Framework.Apollo.ExtensionsHosting       2.10.1   2.10.1
  1. 修改appsettings.json
bash 复制代码
  "Apollo": {
    "AppId": "ellis",
    "Env": "DEV",
    "MetaServer": "http://192.168.214.133:30080",
    "ConfigServer": [ "http://192.168.214.133:30080" ],
  }
  1. 添加配置类
csharp 复制代码
using Com.Ctrip.Framework.Apollo.Internals;
using Microsoft.Extensions.Primitives;

namespace CoreApollo
{
    public class MyConfigService
    {
        private readonly IConfiguration _configuration;

        public static Dictionary<string,string> configDictionary = new Dictionary<string, string>();

        public static object lockobj = new object();

        public MyConfigService(IConfiguration configuration)
        {
            _configuration = configuration;

            // 监听配置变更
            ChangeToken.OnChange(() => _configuration.GetReloadToken(), OnConfigChanged);
            
        }

        public string GetConfig(string key)
        {
            return  _configuration[key];
        }

        private void OnConfigChanged()
        {
            Console.WriteLine("Apollo configuration has changed!");

            lock(lockobj)
            {
                foreach (var section in _configuration.AsEnumerable())
                {
                    configDictionary[section.Key] = section.Value;
                }
            }

            Console.WriteLine("re init");
        }
    }
}
  1. 依赖注入
csharp 复制代码
using Com.Ctrip.Framework.Apollo;
using CoreApollo;


//YamlConfigAdapter.Register();

var builder = WebApplication.CreateBuilder(args);

builder.Configuration
    .AddApollo(builder.Configuration.GetSection("Apollo"))
    .AddDefault();

builder.Services.AddScoped<MyConfigService>();
  1. controller
csharp 复制代码
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CoreApollo.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ApolloController : ControllerBase
    {
        private readonly IConfiguration _configuration;
        private readonly MyConfigService _myConfigService;

        public ApolloController(IConfiguration configuration, MyConfigService myConfigService)
        {
            _configuration = configuration;
            _myConfigService = myConfigService;
        }

        [HttpGet]
        public IActionResult GetConfig(string key)
        {
            var myConfigValue = _myConfigService.GetConfig(key);
            return Ok(myConfigValue);
        }
    }
}

参考
https://learn.microsoft.com/zh-cn/aspnet/core/fundamentals/change-tokens?view=aspnetcore-8.0#simple-startup-change-token

https://www.apolloconfig.com/#/zh/client/dotnet-sdk-user-guide

相关推荐
shepherd枸杞泡茶3 天前
第4章 4.4 EF Core数据库迁移 Add-Migration UpDate-Database
数据库·c#·asp.net·.net·.netcore
火一线4 天前
【ASP .NET Core】ASP .NET Core介绍
服务器·游戏·.netcore
GISer_Qing4 天前
ASP.NET Core 8.0学习笔记(二十七)——数据迁移:Migrations深入与其他迁移命令
数据库·c#·.netcore·entityframework
shepherd枸杞泡茶5 天前
第3章 3.3日志 .NET Core日志 NLog使用教程
c#·asp.net·.net·.netcore
csdn_aspnet5 天前
ASP.NET Core 简单文件上传
asp.net·.netcore
亦世凡华、7 天前
掌握.NET Core后端发布流程,如何部署后端应用?
经验分享·.netcore·docker部署·程序发布
contact977 天前
.NET Core中的五种过滤器详解
.netcore·过滤器
以为不会掉头发的詹同学7 天前
【 Avalonia UI 语言国际化 I18n】图文结合教学,保姆级教学,语言国际化就是这么简单(.Net C#)
开发语言·前端·c#·.netcore·用户界面
爱吃香蕉的阿豪10 天前
在c#中虚方法和抽象类的区别
深度学习·c#·.netcore
shepherd枸杞泡茶10 天前
第3章 .NETCore核心基础组件:3.1 .NET Core依赖注入
开发语言·c#·.net·.netcore