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

相关推荐
[email protected]4 小时前
ASP.NET Core 性能优化:分布式缓存
分布式·缓存·性能优化·asp.net·.netcore
小费的部落4 小时前
.net core 使用 freesql 备份结构和数据
.netcore
[email protected]7 小时前
ASP.NET Core Web API 配置系统集成
后端·asp.net·.netcore
贰貮1 天前
使用Vue 3与.NET 8.0通过SignalR实现实时通信,并结合JWT身份验证
vue.js·websocket·.net·.netcore
江沉晚呤时3 天前
CAP 定理与 BASE 定理在 .NET Core 中的应用
java·服务器·开发语言·前端·.netcore
[email protected]5 天前
ASP.NET Core 反射动态发现类库服务
后端·asp.net·.netcore
江沉晚呤时5 天前
深入探析C#设计模式:访问者模式(Visitor Pattern)的原理与应用
java·服务器·开发语言·数据库·.netcore
盗理者5 天前
.net Core 和 .net freamwork 调用 deepseek api 使用流输出文本(对话补全)
.net·.netcore
江沉晚呤时7 天前
深入解析策略模式在C#中的应用与实现
java·服务器·开发语言·前端·.netcore
时光追逐者8 天前
一款基于 .NET 8 + Vue 开源的、企业级中后台权限管理系统
前端·vue.js·microsoft·开源·c#·.net·.netcore