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

相关推荐
.Net Core 爱好者4 小时前
ASP .NET CORE 6 项目实现WebSocket通信实践
网络·websocket·网络协议·c#·.net·.netcore·visual studio
MoFe113 小时前
【.net core】线程的创建和方法调用
服务器·数据库·.netcore
-心铭-13 小时前
有关C# .NET Core 过滤器的使用
c#·.netcore
IT规划师3 天前
C#|.net core 基础 - 扩展数组添加删除性能最好的方法
c#·.netcore·数组
时光追逐者3 天前
分享6个.NET开源的AI和LLM相关项目框架
人工智能·microsoft·ai·c#·.net·.netcore
Lingbug3 天前
.Net日志组件之NLog的使用和配置
后端·c#·.net·.netcore
IT规划师4 天前
C#|.net core 基础 - 值传递 vs 引用传递
c#·.netcore·值传递·引用传递