NacosDemo.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #pragma warning disable CS0162 // 检测到无法访问的代码
  2. using Apq.Cfg;
  3. using Apq.Cfg.Nacos;
  4. namespace Apq.Cfg.Samples.Demos;
  5. /// <summary>
  6. /// 示例 13: Nacos 配置中心
  7. /// 演示如何使用 Nacos 作为配置源
  8. /// 注意:需要运行 Nacos 服务才能执行完整示例
  9. /// 快速启动:docker run -d -p 8848:8848 -e MODE=standalone nacos/nacos-server:latest
  10. /// </summary>
  11. public static class NacosDemo
  12. {
  13. // 是否启用实际连接测试(设为 true 需要 Nacos 服务运行)
  14. private const bool EnableLiveTest = false;
  15. public static async Task RunAsync(string baseDir)
  16. {
  17. Console.WriteLine("═══════════════════════════════════════════════════════════════");
  18. Console.WriteLine("示例 13: Nacos 配置中心");
  19. Console.WriteLine("═══════════════════════════════════════════════════════════════\n");
  20. Console.WriteLine("【说明】Nacos 是阿里巴巴开源的服务发现和配置管理平台。");
  21. Console.WriteLine("【启动】docker run -d -p 8848:8848 -e MODE=standalone nacos/nacos-server\n");
  22. // 示例 13.1: 基本配置
  23. Demo_BasicConfiguration();
  24. // 示例 13.2: 命名空间和分组
  25. Demo_NamespaceConfiguration();
  26. // 示例 13.3: 认证配置
  27. Demo_AuthConfiguration();
  28. // 示例 13.4: 阿里云 MSE 配置
  29. Demo_AliyunMseConfiguration();
  30. // 示例 13.5: 多配置源
  31. Demo_MultiSourceConfiguration();
  32. // 示例 13.6: 不同数据格式
  33. Demo_DataFormatConfiguration();
  34. // 示例 13.7: 实际连接测试
  35. if (EnableLiveTest)
  36. {
  37. await Demo_LiveTestAsync();
  38. }
  39. else
  40. {
  41. Console.WriteLine("--- 示例 13.7: 实际连接测试 ---");
  42. Console.WriteLine(" [跳过] 设置 EnableLiveTest = true 并启动 Nacos 服务后可运行\n");
  43. }
  44. Console.WriteLine("[示例 13 完成]\n");
  45. }
  46. /// <summary>
  47. /// 示例 13.1: 基本配置
  48. /// </summary>
  49. private static void Demo_BasicConfiguration()
  50. {
  51. Console.WriteLine("--- 示例 13.1: 基本配置 ---");
  52. // 方式1: 使用 Action 配置
  53. var builder1 = new CfgBuilder()
  54. .AddNacos(options =>
  55. {
  56. options.ServerAddresses = "localhost:8848"; // Nacos 服务地址
  57. options.DataId = "myapp-config"; // 配置 DataId
  58. options.Group = "DEFAULT_GROUP"; // 配置分组
  59. }, level: 0);
  60. Console.WriteLine(" 方式1: AddNacos(options => { ... })");
  61. Console.WriteLine(" options.ServerAddresses = \"localhost:8848\"");
  62. Console.WriteLine(" options.DataId = \"myapp-config\"");
  63. Console.WriteLine(" options.Group = \"DEFAULT_GROUP\"");
  64. // 方式2: 使用简化方法
  65. var builder2 = new CfgBuilder()
  66. .AddNacos("localhost:8848", dataId: "myapp-config", level: 0);
  67. Console.WriteLine(" 方式2: AddNacos(\"localhost:8848\", dataId: \"myapp-config\")");
  68. Console.WriteLine();
  69. }
  70. /// <summary>
  71. /// 示例 13.2: 命名空间和分组配置
  72. /// </summary>
  73. private static void Demo_NamespaceConfiguration()
  74. {
  75. Console.WriteLine("--- 示例 13.2: 命名空间和分组 ---");
  76. // Nacos 支持命名空间隔离不同环境的配置
  77. var builder = new CfgBuilder()
  78. .AddNacos(options =>
  79. {
  80. options.ServerAddresses = "localhost:8848";
  81. options.Namespace = "dev"; // 命名空间 ID(开发环境)
  82. options.DataId = "myapp-config";
  83. options.Group = "MYAPP_GROUP"; // 自定义分组
  84. }, level: 0);
  85. Console.WriteLine(" options.Namespace = \"dev\" // 命名空间 ID");
  86. Console.WriteLine(" options.Group = \"MYAPP_GROUP\" // 自定义分组");
  87. Console.WriteLine();
  88. }
  89. /// <summary>
  90. /// 示例 13.3: 用户名密码认证
  91. /// </summary>
  92. private static void Demo_AuthConfiguration()
  93. {
  94. Console.WriteLine("--- 示例 13.3: 用户名密码认证 ---");
  95. var builder = new CfgBuilder()
  96. .AddNacos(options =>
  97. {
  98. options.ServerAddresses = "localhost:8848";
  99. options.DataId = "myapp-config";
  100. options.Group = "DEFAULT_GROUP";
  101. options.Username = "nacos"; // 用户名
  102. options.Password = "nacos"; // 密码
  103. options.ConnectTimeoutMs = 10000; // 连接超时(毫秒)
  104. }, level: 0);
  105. Console.WriteLine(" options.Username = \"nacos\"");
  106. Console.WriteLine(" options.Password = \"nacos\"");
  107. Console.WriteLine(" options.ConnectTimeoutMs = 10000");
  108. Console.WriteLine();
  109. }
  110. /// <summary>
  111. /// 示例 13.4: 阿里云 MSE(微服务引擎)配置
  112. /// </summary>
  113. private static void Demo_AliyunMseConfiguration()
  114. {
  115. Console.WriteLine("--- 示例 13.4: 阿里云 MSE 配置 ---");
  116. // 使用阿里云 MSE 托管的 Nacos
  117. var builder = new CfgBuilder()
  118. .AddNacos(options =>
  119. {
  120. options.ServerAddresses = "mse-xxx.nacos.mse.aliyuncs.com:8848";
  121. options.Namespace = "your-namespace-id";
  122. options.DataId = "myapp-config";
  123. options.Group = "DEFAULT_GROUP";
  124. options.AccessKey = "your-access-key"; // 阿里云 AccessKey
  125. options.SecretKey = "your-secret-key"; // 阿里云 SecretKey
  126. }, level: 0);
  127. Console.WriteLine(" options.ServerAddresses = \"mse-xxx.nacos.mse.aliyuncs.com:8848\"");
  128. Console.WriteLine(" options.AccessKey = \"your-access-key\"");
  129. Console.WriteLine(" options.SecretKey = \"your-secret-key\"");
  130. Console.WriteLine();
  131. }
  132. /// <summary>
  133. /// 示例 13.5: 多配置源(多 DataId)
  134. /// </summary>
  135. private static void Demo_MultiSourceConfiguration()
  136. {
  137. Console.WriteLine("--- 示例 13.5: 多配置源 ---");
  138. // 可以加载多个配置,实现配置的层级覆盖
  139. var builder = new CfgBuilder()
  140. // 公共配置(最低优先级)
  141. .AddNacos(options =>
  142. {
  143. options.ServerAddresses = "localhost:8848";
  144. options.DataId = "common-config";
  145. options.Group = "DEFAULT_GROUP";
  146. }, level: 0)
  147. // 应用配置(中等优先级)
  148. .AddNacos(options =>
  149. {
  150. options.ServerAddresses = "localhost:8848";
  151. options.DataId = "myapp-config";
  152. options.Group = "DEFAULT_GROUP";
  153. }, level: 1)
  154. // 环境特定配置(最高优先级)
  155. .AddNacos(options =>
  156. {
  157. options.ServerAddresses = "localhost:8848";
  158. options.DataId = "myapp-config-dev";
  159. options.Group = "DEFAULT_GROUP";
  160. }, level: 2);
  161. Console.WriteLine(" level: 0 - common-config (公共配置)");
  162. Console.WriteLine(" level: 1 - myapp-config (应用配置)");
  163. Console.WriteLine(" level: 2 - myapp-config-dev (环境配置) - 最高优先级");
  164. Console.WriteLine();
  165. }
  166. /// <summary>
  167. /// 示例 13.6: 不同数据格式
  168. /// </summary>
  169. private static void Demo_DataFormatConfiguration()
  170. {
  171. Console.WriteLine("--- 示例 13.6: 不同数据格式 ---");
  172. // JSON 格式(默认)
  173. var builder1 = new CfgBuilder()
  174. .AddNacos(options =>
  175. {
  176. options.ServerAddresses = "localhost:8848";
  177. options.DataId = "myapp-config.json";
  178. options.DataFormat = NacosDataFormat.Json;
  179. }, level: 0);
  180. Console.WriteLine(" JSON: options.DataFormat = NacosDataFormat.Json");
  181. // YAML 格式
  182. var builder2 = new CfgBuilder()
  183. .AddNacos(options =>
  184. {
  185. options.ServerAddresses = "localhost:8848";
  186. options.DataId = "myapp-config.yaml";
  187. options.DataFormat = NacosDataFormat.Yaml;
  188. }, level: 0);
  189. Console.WriteLine(" YAML: options.DataFormat = NacosDataFormat.Yaml");
  190. // Properties 格式(key=value)
  191. var builder3 = new CfgBuilder()
  192. .AddNacos(options =>
  193. {
  194. options.ServerAddresses = "localhost:8848";
  195. options.DataId = "myapp-config.properties";
  196. options.DataFormat = NacosDataFormat.Properties;
  197. }, level: 0);
  198. Console.WriteLine(" Properties: options.DataFormat = NacosDataFormat.Properties");
  199. Console.WriteLine();
  200. }
  201. /// <summary>
  202. /// 示例 13.7: 实际连接测试
  203. /// </summary>
  204. private static async Task Demo_LiveTestAsync()
  205. {
  206. Console.WriteLine("--- 示例 13.7: 实际连接测试 ---");
  207. try
  208. {
  209. // 创建配置
  210. var cfg = new CfgBuilder()
  211. .AddNacos(options =>
  212. {
  213. options.ServerAddresses = "localhost:8848";
  214. options.DataId = "apq-cfg-demo";
  215. options.Group = "DEFAULT_GROUP";
  216. options.ConnectTimeoutMs = 3000;
  217. }, level: 0, isPrimaryWriter: true)
  218. .Build();
  219. Console.WriteLine(" ✓ 连接 Nacos 成功");
  220. // 写入测试配置
  221. cfg.Set("App:Name", "NacosDemo");
  222. cfg.Set("App:Version", "1.0.0");
  223. cfg.Set("Database:Host", "localhost");
  224. cfg.Set("Database:Port", "5432");
  225. await cfg.SaveAsync();
  226. Console.WriteLine(" ✓ 写入配置成功");
  227. // 读取配置
  228. Console.WriteLine($" App:Name = {cfg.Get("App:Name")}");
  229. Console.WriteLine($" App:Version = {cfg.Get("App:Version")}");
  230. Console.WriteLine($" Database:Host = {cfg.Get("Database:Host")}");
  231. Console.WriteLine($" Database:Port = {cfg.Get<int>("Database:Port")}");
  232. // 枚举所有键
  233. var keys = cfg.GetChildKeys();
  234. Console.WriteLine($" 配置键数量: {keys.Count()}");
  235. cfg.Dispose();
  236. }
  237. catch (Exception ex)
  238. {
  239. Console.WriteLine($" ✗ 连接失败: {ex.Message}");
  240. Console.WriteLine(" 提示: 请确保 Nacos 服务正在运行");
  241. }
  242. Console.WriteLine();
  243. }
  244. }