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

相关推荐
小兜全糖(xdqt)10 小时前
.netCore WebAPI中字符串加密与解密
.netcore
沪上百卉10 小时前
.NET Core 常用的三个生命周期
.netcore
时光追逐者1 天前
C#/.NET/.NET Core学习路线集合,学习不迷路!
开发语言·学习·c#·asp.net·.net·.netcore·微软技术
Jeffrey侠客2 天前
.Net Core 6.0 WebApi在Centos中部署
linux·centos·.netcore
技术拾荒者3 天前
.net core mvc 控制器中页面跳转
后端·c#·asp.net·mvc·.netcore
时光追逐者3 天前
Visual Studio 2022:一个功能全面且强大的IDE
ide·c#·.net·.netcore·visual studio
.Net Core 爱好者5 天前
ASP .NET CORE 6 在项目中集成WatchDog开源项目
c#·.net·.netcore
想起你的日子6 天前
.net core 接口,动态接收各类型请求的参数
.netcore
qq_383139846 天前
Quartz实现定时调用接口(.net core2.0)
.netcore
时光追逐者6 天前
一个.NET开源、轻量级的运行耗时统计库 - MethodTimer
开源·c#·asp.net·.net·.netcore·微软技术