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

相关推荐
全栈小54 天前
【C#】.net core,静态方法线程安全解析,面试时经常被问的线程安全就藏在里面
安全·c#·.netcore
清风与日月6 天前
Yitter.IdGenerator:高性能分布式唯一ID生成器详解
分布式·c#·.net·.netcore
Lost of 程序猿9 天前
ASP.NET Core 后台任务全景:从 BackgroundService 到 Channel 队列,再到分布式调度
后端·asp.net·.netcore
宝桥南山9 天前
Blazor Web Assembly - 体验一下Authentication State从Server共享给Client
microsoft·微软·c#·asp.net·.net·.netcore
宝桥南山19 天前
Microsoft Agent Framework(.NET) - 尝试一下从MCP Server上获取Agent Skills
ai·微软·c#·aigc·.net·.netcore
海盗12341 个月前
微软技术周报 ——2026-08-03
后端·python·microsoft·c#·.netcore
啊这啊这 六弦之首1 个月前
.NET Core跨平台的奥秘[中篇]:复用之殇
.netcore
心念枕惊1 个月前
.NET CORE 授权进阶-角色、策略与动态权限实现
java·前端·.netcore
完美火龙篇 四月的友1 个月前
通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?
.net·.netcore
时代的狂2 个月前
如何理解 C# 的 async 和 await
c#·.netcore·async·await