CfgBuilderExtensions.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. namespace Apq.Cfg.Apollo;
  2. /// <summary>
  3. /// CfgBuilder 的 Apollo 扩展方法
  4. /// </summary>
  5. public static class CfgBuilderExtensions
  6. {
  7. /// <summary>
  8. /// 添加 Apollo 配置源
  9. /// </summary>
  10. /// <param name="builder">配置构建器</param>
  11. /// <param name="configure">配置选项</param>
  12. /// <param name="level">配置层级,数值越大优先级越高</param>
  13. /// <param name="isPrimaryWriter">是否为主写入源(Apollo 不支持写入)</param>
  14. /// <returns>配置构建器</returns>
  15. public static CfgBuilder AddApollo(
  16. this CfgBuilder builder,
  17. Action<ApolloCfgOptions> configure,
  18. int level,
  19. bool isPrimaryWriter = false)
  20. {
  21. var options = new ApolloCfgOptions();
  22. configure?.Invoke(options);
  23. builder.AddSource(new ApolloCfgSource(options, level, isPrimaryWriter));
  24. return builder;
  25. }
  26. /// <summary>
  27. /// 添加 Apollo 配置源(使用默认选项)
  28. /// </summary>
  29. /// <param name="builder">配置构建器</param>
  30. /// <param name="appId">Apollo 应用 ID</param>
  31. /// <param name="metaServer">Meta Server 地址</param>
  32. /// <param name="namespaces">命名空间列表</param>
  33. /// <param name="level">配置层级</param>
  34. /// <param name="enableHotReload">是否启用热重载</param>
  35. /// <returns>配置构建器</returns>
  36. public static CfgBuilder AddApollo(
  37. this CfgBuilder builder,
  38. string appId,
  39. string metaServer = "http://localhost:8080",
  40. string[]? namespaces = null,
  41. int level = 0,
  42. bool enableHotReload = true)
  43. {
  44. return builder.AddApollo(options =>
  45. {
  46. options.AppId = appId;
  47. options.MetaServer = metaServer;
  48. options.Namespaces = namespaces ?? ["application"];
  49. options.EnableHotReload = enableHotReload;
  50. }, level);
  51. }
  52. }